2 * Copyright 2001-2007 Internet2
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file xmltooling/Lockable.h
23 #ifndef __xmltooling_lockable_h__
24 #define __xmltooling_lockable_h__
26 #include <xmltooling/base.h>
28 namespace xmltooling {
31 * Abstract mixin interface for interfaces that support locking
33 struct XMLTOOL_API Lockable
35 virtual ~Lockable() {}
38 * Lock the associated object for exclusive access.
40 * @return a pointer to the object being locked
42 virtual Lockable* lock()=0;
45 * Unlock the associated object from exclusive access.
47 virtual void unlock()=0;
51 * RAII wrapper for lockable objects to ensure lock release
53 class XMLTOOL_API Locker
55 MAKE_NONCOPYABLE(Locker);
58 * Optionally locks an object and stores it for later release.
60 * @param lockee pointer to an object to hold, and optionally lock
61 * @param lock true iff object is not yet locked
63 Locker(Lockable* lockee=NULL, bool lock=true) {
65 m_lockee=lockee->lock();
71 * Optionally locks an object and stores it for later release.
72 * If an object is already held, it is unlocked and detached.
74 * @param lockee pointer to an object to hold, and optionally lock
75 * @param lock true iff object is not yet locked
77 void assign(Lockable* lockee=NULL, bool lock=true) {
82 m_lockee=lockee->lock();
88 * Destructor releases lock on held pointer, if any.
101 #endif /* __xmltooling_lockable_h__ */