Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
LockPtr.h
1 /*
2  * Lock and LockPtr classes
3  *
4  * This file is part of ELM
5  * Copyright (c) 2016, IRIT UPS.
6  *
7  * OTAWA is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * OTAWA is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with OTAWA; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 #ifndef ELM_UTIL_LOCKPTR_H
22 #define ELM_UTIL_LOCKPTR_H
23 
24 #include <elm/assert.h>
25 
26 namespace elm {
27 
28 // Lock class
29 class Lock {
30  int u;
31 public:
32  inline Lock(int usage = 0): u(usage) { }
33  inline void lock(void) { u++; }
34  inline void unlock(void) { u--; }
35  inline int usage(void) const { return u; }
36 };
37 
38 
39 // AutoPtr class
40 template <class T> class LockPtr {
41 public:
42  inline LockPtr(T *p = 0): ptr(p ? p : null_lock()) { ptr->lock(); }
43  inline LockPtr(const LockPtr& l): ptr(l.ptr) { ptr->lock(); }
44  inline ~LockPtr(void) { unlock(); }
45  static LockPtr<T> null;
46 
47  inline LockPtr& operator=(const LockPtr& lock)
48  { unlock(); ptr = lock.ptr; ptr->lock(); return *this;}
49  inline LockPtr& operator=(T *p)
50  { unlock(); ptr = p ? p : null_lock(); ptr->lock(); return *this; }
51 
52  inline T *operator->(void) const
53  { ASSERTP(!isNull(), "accessing null pointer"); return (T *)ptr; }
54  inline T& operator*(void) const
55  { ASSERTP(!isNull(), "accessing null pointer"); return *(T *)ptr; }
56  inline T *operator&(void) const
57  { return isNull() ? 0 : (T *)ptr; }
58 
59  inline bool isNull(void) const
60  { return ptr == null_lock(); }
61  inline operator bool(void) const { return !isNull(); };
62 
63  inline bool operator==(const LockPtr<T>& ap) const
64  { return ptr == ap.ptr; }
65  inline bool operator!=(const LockPtr<T>& ap) const
66  { return ptr != ap.ptr; }
67  inline bool operator>(const LockPtr<T>& ap) const
68  { return ptr > ap.ptr; }
69  inline bool operator>=(const LockPtr<T>& ap) const
70  { return ptr >= ap.ptr; }
71  inline bool operator<(const LockPtr<T>& ap) const
72  { return ptr < ap.ptr; }
73  inline bool operator<=(const LockPtr<T>& ap) const
74  { return ptr <= ap.ptr; }
75 
76 private:
77  void unlock(void) { ptr->unlock(); if(!ptr->usage()) { delete ptr; ptr = null_lock(); } }
78  T *ptr;
79  static T *null_lock(void) { static Lock n(1); return (T *)&n; }
80 };
81 
82 } // elm
83 
84 #endif // ELM_UTIL_AUTOPTR_H
elm::LockPtr::operator->
T * operator->(void) const
Definition: LockPtr.h:52
elm::io::p
Printable< T, M > p(const T &data, const M &man)
Definition: Output.h:302
elm::LockPtr::operator*
T & operator*(void) const
Definition: LockPtr.h:54
elm::LockPtr::operator&
T * operator&(void) const
Definition: LockPtr.h:56
elm::Lock::usage
int usage(void) const
Definition: LockPtr.h:35
elm::Lock
Definition: LockPtr.h:29
elm::LockPtr::operator>
bool operator>(const LockPtr< T > &ap) const
Definition: LockPtr.h:67
elm::Lock::unlock
void unlock(void)
Definition: LockPtr.h:34
elm::LockPtr::isNull
bool isNull(void) const
Definition: LockPtr.h:59
bool
elm::Lock::Lock
Lock(int usage=0)
Definition: LockPtr.h:32
elm::Lock::lock
void lock(void)
Definition: LockPtr.h:33
elm::LockPtr::LockPtr
LockPtr(T *p=0)
Definition: LockPtr.h:42
elm::LockPtr::LockPtr
LockPtr(const LockPtr &l)
Definition: LockPtr.h:43
elm
Definition: adapter.h:26
elm::LockPtr::operator>=
bool operator>=(const LockPtr< T > &ap) const
Definition: LockPtr.h:69
elm::LockPtr::operator<=
bool operator<=(const LockPtr< T > &ap) const
Definition: LockPtr.h:73
elm::LockPtr::operator=
LockPtr & operator=(const LockPtr &lock)
Definition: LockPtr.h:47
elm::LockPtr::operator!=
bool operator!=(const LockPtr< T > &ap) const
Definition: LockPtr.h:65
elm::LockPtr::~LockPtr
~LockPtr(void)
Definition: LockPtr.h:44
elm::LockPtr::operator=
LockPtr & operator=(T *p)
Definition: LockPtr.h:49
elm::LockPtr
Definition: LockPtr.h:40
elm::LockPtr::operator==
bool operator==(const LockPtr< T > &ap) const
Definition: LockPtr.h:63
elm::LockPtr::operator<
bool operator<(const LockPtr< T > &ap) const
Definition: LockPtr.h:71