Update copyright.
[shibboleth/cpp-xmltooling.git] / xmltooling / Lockable.h
1 /*
2  *  Copyright 2001-2007 Internet2
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /**
18  * @file Lockable.h
19  * 
20  * Locking abstraction 
21  */
22
23 #ifndef __xmltooling_lockable_h__
24 #define __xmltooling_lockable_h__
25
26 #include <xmltooling/base.h>
27
28 namespace xmltooling {
29
30     /**
31      * Abstract mixin interface for interfaces that support locking
32      */
33     struct XMLTOOL_API Lockable
34     {
35         virtual ~Lockable() {}
36         
37         /**
38          * Lock the associated object for exclusive access.
39          * 
40          * @return a pointer to the object being locked
41          */
42         virtual Lockable* lock()=0;
43
44         /**
45          * Unlock the associated object from exclusive access.
46          */
47         virtual void unlock()=0;
48     };
49
50     /**
51      * RAII wrapper for lockable objects to ensure lock release
52      */
53     class XMLTOOL_API Locker
54     {
55     MAKE_NONCOPYABLE(Locker);
56     public:
57         /**
58          * Optionally locks an object and stores it for later release.
59          * 
60          * @param lockee    pointer to an object to hold, and optionally lock
61          * @param lock      true iff object is not yet locked
62          */
63         Locker(Lockable* lockee=NULL, bool lock=true) {
64             if (lockee && lock)
65                 m_lockee=lockee->lock();
66             else
67                 m_lockee=lockee;
68         }
69
70         /**
71          * Optionally locks an object and stores it for later release.
72          * If an object is already held, it is unlocked and detached.
73          * 
74          * @param lockee    pointer to an object to hold, and optionally lock
75          * @param lock      true iff object is not yet locked
76          */
77         void assign(Lockable* lockee=NULL, bool lock=true) {
78             if (m_lockee)
79                 m_lockee->unlock();
80             m_lockee=NULL;
81             if (lockee && lock)
82                 m_lockee=lockee->lock();
83             else
84                 m_lockee=lockee;
85         }
86         
87         /**
88          * Destructor releases lock on held pointer, if any.
89          */
90         ~Locker() {
91             if (m_lockee)
92                 m_lockee->unlock();
93          }
94         
95     private:
96         Lockable* m_lockee;
97     };
98
99 };
100
101 #endif /* __xmltooling_lockable_h__ */