1 /******************************************************************************
5 * Copyright (C) 1997-2012 by Dimitri van Heesch.
7 * Permission to use, copy, modify, and distribute this software and its
8 * documentation under the terms of the GNU General Public License is hereby
9 * granted. No representations are made about the suitability of this software
10 * for any purpose. It is provided "as is" without express or implied warranty.
11 * See the GNU General Public License for more details.
13 * Documents produced by Doxygen are derivative works derived from the
14 * input used in their production; they are not affected by this license.
21 /*! @brief Abstract interface for lockable objects.
23 * By implementing this interface, a smart pointer can be created which
24 * will lock this object. This is used to prevent that an internal pointer
25 * owned by a lockable object would become invalid when the object is removed from
26 * memory, leaving the client with an invalid pointer. By letting the client use
27 * a smart pointer instead of the real object the object will be locked into
28 * memory until the pointer is no longer used, at which point the owner object will be
29 * unlock and can be removed from memory.
34 LockableObj() : m_lockCount(0) {}
35 virtual ~LockableObj() {}
37 /*! Returns TRUE if the object is currently locked. */
38 bool isLocked() const { return m_lockCount>0; }
42 /*! Called when the object is locked. */
43 virtual void lock() const = 0;
45 /*! Called when the object is unlocked. */
46 virtual void unlock() const = 0;
50 // template<class T> friend class LockingPtr;
54 /*! @brief Smart pointer which keeps a lock on the owner of the pointer.
56 * With the pointer an owner object derived from LockableObj is associated.
57 * As long as the smart object exists it will keep a lock on the obj by calling
58 * LockableObj::lock(). Smart pointers can be copied and passed by value. As
59 * soon as there or no more smart pointer references to the object,
60 * LockableObj::unlock() will be called automatically.
62 template<class T> class LockingPtr
68 /*! Creates a smart pointer for pointer \a p owned by object \a o.
70 LockingPtr(const LockableObj *o,const T* p)
72 if (o->m_lockCount==0) o->lock();
73 m_owner = (LockableObj *)o;
74 m_owner->m_lockCount++;
78 /*! Copies the smart pointer \a lp
80 LockingPtr(const LockingPtr &lp)
84 m_owner->m_lockCount++;
87 /*! Assigns the smart pointer \a lp
89 LockingPtr &operator=(const LockingPtr &lp)
91 m_owner->m_lockCount--;
92 if (m_owner->m_lockCount==0) // no more references
98 m_owner->m_lockCount++;
102 /*! Destroys the smart pointer, will unlock the owner.
106 m_owner->m_lockCount--;
107 if (m_owner->m_lockCount==0) // no more references
118 bool operator!() const
123 bool operator==(T *p) const
128 bool operator==(const LockingPtr &lp) const
130 return m_ptr==lp.m_ptr;
133 bool operator!=(T *p) const
138 bool operator!=(const LockingPtr &lp) const
140 return m_ptr!=lp.m_ptr;
143 /*! Dereference operator */
144 const T& operator* () const
154 /*! Pointer operator */
155 T* operator-> () const
161 #endif // LOCKINGPTR_H