merge the formfield patch from ooo-build
[ooovba.git] / framework / inc / threadhelp / resetableguard.hxx
blob9fdaa0b765fa26c2caa073653208718c564d236c
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: resetableguard.hxx,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #ifndef __FRAMEWORK_THREADHELP_RESETABLEGUARD_HXX_
32 #define __FRAMEWORK_THREADHELP_RESETABLEGUARD_HXX_
34 //_________________________________________________________________________________________________________________
35 // my own includes
36 //_________________________________________________________________________________________________________________
38 #include <threadhelp/inoncopyable.h>
39 #include <threadhelp/imutex.h>
41 //#ifndef __FRAMEWORK_THREADHELP_THREADHELPBASE_HXX_
42 //#include <threadhelp/threadhelpbase.hxx>
43 //#endif
45 //_________________________________________________________________________________________________________________
46 // interface includes
47 //_________________________________________________________________________________________________________________
49 //_________________________________________________________________________________________________________________
50 // other includes
51 //_________________________________________________________________________________________________________________
52 #include <sal/types.h>
54 //_________________________________________________________________________________________________________________
55 // namespace
56 //_________________________________________________________________________________________________________________
58 namespace framework{
60 //_________________________________________________________________________________________________________________
61 // const
62 //_________________________________________________________________________________________________________________
64 //_________________________________________________________________________________________________________________
65 // declarations
66 //_________________________________________________________________________________________________________________
68 /*-************************************************************************************************************//**
69 @short implement a guard for implementing save thread access
70 @descr These guard has an additional feature to well known one ::osl::Guard.
71 You can lock() and unlock() it very often!
72 A set bool flag inside protect this implementation against multiple lock() calls
73 without any unlock()! So the increasing of guarded mutex couldn't be greater then 1 ...
75 @attention a) To prevent us against wrong using, the default ctor, copy ctor and the =operator are maked private!
76 b) Use interface "IMutex" of set LockHelper only - because we must support an exclusiv locking.
77 Interface "IRWLock" should be used by special guard implementations ... like "ReadGuard" or "WriteGuard"!
79 @implements -
80 @base INonCopyable
82 @devstatus ready to use
83 *//*-*************************************************************************************************************/
84 class ResetableGuard : private INonCopyable
86 //-------------------------------------------------------------------------------------------------------------
87 // public methods
88 //-------------------------------------------------------------------------------------------------------------
89 public:
91 /*-****************************************************************************************************//**
92 @short ctors
93 @descr Use these ctor methods to initialize the guard right.
94 Given lock reference must be valid - otherwise crashes could occure!
96 @seealso -
98 @param "pLock", pointer to lock helper of user
99 @param "rLock", reference to lock helper of user
100 @return -
102 @onerror -
103 *//*-*****************************************************************************************************/
104 inline ResetableGuard( IMutex* pLock )
105 : m_pLock ( pLock )
106 , m_bLocked ( sal_False )
108 lock();
111 //*********************************************************************************************************
112 inline ResetableGuard( IMutex& rLock )
113 : m_pLock ( &rLock )
114 , m_bLocked ( sal_False )
116 lock();
119 /*-****************************************************************************************************//**
120 @short dtor
121 @descr We must release set mutex if programmer forget it ...
123 @seealso -
125 @param -
126 @return -
128 @onerror -
129 *//*-*****************************************************************************************************/
130 inline ~ResetableGuard()
132 unlock();
135 /*-****************************************************************************************************//**
136 @short enable/disable the lock
137 @descr Use this methods to lock or unlock the mutex.
138 You can do it so often you wish to do that ...
140 @attention We use another member to prevent us against multiple acquire calls of the same guard
141 without suitable release calls!
142 You don't must protect access at these bool member by using an own mutex ....
143 because nobody use the same guard instance from different threads!
144 It will be a function-local object every time.
146 @seealso -
148 @param -
149 @return -
151 @onerror -
152 *//*-*****************************************************************************************************/
153 inline void lock()
155 if( m_bLocked == sal_False )
157 m_pLock->acquire();
158 m_bLocked = sal_True;
162 //*********************************************************************************************************
163 inline void unlock()
165 if( m_bLocked == sal_True )
167 m_pLock->release();
168 m_bLocked = sal_False;
172 //-------------------------------------------------------------------------------------------------------------
173 // private methods
174 //-------------------------------------------------------------------------------------------------------------
175 private:
177 /*-****************************************************************************************************//**
178 @short disable using of these functions!
179 @descr It's not allowed to use this methods. Different problem can occure otherwise.
180 Thats why we disable it by make it private.
182 @seealso other ctor
184 @param -
185 @return -
187 @onerror -
188 *//*-*****************************************************************************************************/
189 ResetableGuard();
191 //-------------------------------------------------------------------------------------------------------------
192 // private member
193 //-------------------------------------------------------------------------------------------------------------
194 private:
196 IMutex* m_pLock ; /// pointer to safed lock member of user
197 sal_Bool m_bLocked ; /// protection against multiple lock() calls without unlock()
199 }; // class ResetableGuard
201 } // namespace framework
203 #endif // #ifndef __FRAMEWORK_THREADHELP_RESETABLEGUARD_HXX_