xref: /aoo4110/main/package/inc/mutexholder.hxx (revision b1cdbd2c)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef __MUTEXHOLDER_HXX_
25 #define __MUTEXHOLDER_HXX_
26 
27 #include <osl/mutex.hxx>
28 
29 class SotMutexHolder
30 {
31 	::osl::Mutex m_aMutex;
32 	sal_Int32	 m_nRefCount;
33 
34 	public:
SotMutexHolder()35 	SotMutexHolder() : m_nRefCount( 0 ) {}
36 
AddRef()37 	void AddRef()
38 	{
39 		m_nRefCount++;
40 	}
41 
ReleaseRef()42 	void ReleaseRef()
43 	{
44 		if ( !--m_nRefCount )
45 			delete this;
46 	}
47 
GetMutex()48 	::osl::Mutex& GetMutex() { return m_aMutex; }
49 };
50 
51 class SotMutexHolderRef
52 {
53 	SotMutexHolder* m_pHolder;
54 
55 public:
SotMutexHolderRef()56 	SotMutexHolderRef()
57 	: m_pHolder( NULL )
58 	{}
59 
SotMutexHolderRef(SotMutexHolder * pHolder)60 	SotMutexHolderRef( SotMutexHolder* pHolder )
61 	: m_pHolder( pHolder )
62 	{
63 		if ( m_pHolder )
64 			m_pHolder->AddRef();
65 	}
66 
SotMutexHolderRef(const SotMutexHolderRef & rRef)67 	SotMutexHolderRef( const SotMutexHolderRef& rRef )
68 	: m_pHolder( rRef.m_pHolder )
69 	{
70 		if ( m_pHolder )
71 			m_pHolder->AddRef();
72 	}
73 
~SotMutexHolderRef()74 	~SotMutexHolderRef()
75 	{
76 		if ( m_pHolder )
77 			m_pHolder->ReleaseRef();
78 	}
79 
operator =(const SotMutexHolderRef & rRef)80 	SotMutexHolderRef& operator =( const SotMutexHolderRef& rRef )
81 	{
82 		if ( m_pHolder )
83 			m_pHolder->ReleaseRef();
84 
85 		m_pHolder = rRef.m_pHolder;
86 
87 		if ( m_pHolder )
88 			m_pHolder->AddRef();
89 
90 		return *this;
91 	}
92 
operator =(SotMutexHolder * pHolder)93 	SotMutexHolderRef& operator =( SotMutexHolder* pHolder )
94 	{
95 		if ( m_pHolder )
96 			m_pHolder->ReleaseRef();
97 
98 		m_pHolder = pHolder;
99 
100 		if ( m_pHolder )
101 			m_pHolder->AddRef();
102 		return *this;
103 	}
104 
operator ->() const105 	SotMutexHolder* operator ->() const
106 	{
107 		return m_pHolder;
108 	}
109 
operator *() const110 	SotMutexHolder& operator *() const
111 	{
112 		return *m_pHolder;
113 	}
114 
operator SotMutexHolder*() const115 	operator SotMutexHolder*() const
116 	{
117 		return m_pHolder;
118 	}
119 
Is() const120 	sal_Bool Is() const
121 	{
122 		return m_pHolder != NULL;
123 	}
124 };
125 
126 #endif
127 
128