xref: /aoo41x/main/package/inc/mutexholder.hxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef __MUTEXHOLDER_HXX_
29 #define __MUTEXHOLDER_HXX_
30 
31 #include <osl/mutex.hxx>
32 
33 class SotMutexHolder
34 {
35 	::osl::Mutex m_aMutex;
36 	sal_Int32	 m_nRefCount;
37 
38 	public:
39 	SotMutexHolder() : m_nRefCount( 0 ) {}
40 
41 	void AddRef()
42 	{
43 		m_nRefCount++;
44 	}
45 
46 	void ReleaseRef()
47 	{
48 		if ( !--m_nRefCount )
49 			delete this;
50 	}
51 
52 	::osl::Mutex& GetMutex() { return m_aMutex; }
53 };
54 
55 class SotMutexHolderRef
56 {
57 	SotMutexHolder* m_pHolder;
58 
59 public:
60 	SotMutexHolderRef()
61 	: m_pHolder( NULL )
62 	{}
63 
64 	SotMutexHolderRef( SotMutexHolder* pHolder )
65 	: m_pHolder( pHolder )
66 	{
67 		if ( m_pHolder )
68 			m_pHolder->AddRef();
69 	}
70 
71 	SotMutexHolderRef( const SotMutexHolderRef& rRef )
72 	: m_pHolder( rRef.m_pHolder )
73 	{
74 		if ( m_pHolder )
75 			m_pHolder->AddRef();
76 	}
77 
78 	~SotMutexHolderRef()
79 	{
80 		if ( m_pHolder )
81 			m_pHolder->ReleaseRef();
82 	}
83 
84 	SotMutexHolderRef& operator =( const SotMutexHolderRef& rRef )
85 	{
86 		if ( m_pHolder )
87 			m_pHolder->ReleaseRef();
88 
89 		m_pHolder = rRef.m_pHolder;
90 
91 		if ( m_pHolder )
92 			m_pHolder->AddRef();
93 
94 		return *this;
95 	}
96 
97 	SotMutexHolderRef& operator =( SotMutexHolder* pHolder )
98 	{
99 		if ( m_pHolder )
100 			m_pHolder->ReleaseRef();
101 
102 		m_pHolder = pHolder;
103 
104 		if ( m_pHolder )
105 			m_pHolder->AddRef();
106 		return *this;
107 	}
108 
109 	SotMutexHolder* operator ->() const
110 	{
111 		return m_pHolder;
112 	}
113 
114 	SotMutexHolder& operator *() const
115 	{
116 		return *m_pHolder;
117 	}
118 
119 	operator SotMutexHolder*() const
120 	{
121 		return m_pHolder;
122 	}
123 
124 	sal_Bool Is() const
125 	{
126 		return m_pHolder != NULL;
127 	}
128 };
129 
130 #endif
131 
132