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