Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
SharedPtr.h
1 /*
2  * SharedPtr class
3  *
4  * This file is part of OTAWA
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_SHAREDPTR_H_
22 #define ELM_UTIL_SHAREDPTR_H_
23 
24 #include <elm/io.h>
25 #include <elm/types.h>
26 
27 namespace elm {
28 
29 template <class T>
30 class SharedPtr {
31  typedef struct cell_t {
32  inline cell_t(T *p): cnt(1), ptr(p) { }
33  t::uint32 cnt;
34  T *ptr;
35  } cell_t;
36 
37 public:
38  inline SharedPtr(void): c(&null) { lock(); }
39  inline SharedPtr(T *p): c(new cell_t(p)) { }
40  inline SharedPtr(const SharedPtr<T>& p): c(p.c) { lock(); }
41  inline ~SharedPtr(void) { unlock(); }
42  inline bool isEmpty(void) const { return c->ptr; }
43  inline T *ptr(void) const { return c->ptr; }
44  inline T& ref(void) const { return *(c->ptr); }
45 
46  inline operator bool(void) const { return isEmpty(); }
47  inline operator T *(void) const { return ptr(); }
48  inline T *operator->(void) const { return ptr(); }
49  inline T& operator*(void) const { return ref(); }
50  inline T *operator&(void) const { return ptr(); }
51  inline SharedPtr<T>& operator=(const SharedPtr<T>& p) { unlock(); c = p.c; lock(); return *this; }
52  inline SharedPtr<T>& operator=(T *p) { unlock(); c = new cell_t(p); return *this; }
53 
54  inline bool operator==(const SharedPtr<T>& p) const { return c->ptr == p.c->ptr; }
55  inline bool operator!=(const SharedPtr<T>& p) const { return c->ptr != p.c->ptr; }
56  inline bool operator==(T *p) const { return c->ptr == p; }
57  inline bool operator!=(T *p) const { return c->ptr != p; }
58 
59 private:
60  inline void lock(void) { c->cnt++; }
61  inline void unlock(void) { c->cnt--; if(c->cnt == 0) clear(); }
62  void clear(void) { if(c->ptr) delete c->ptr; delete c; }
63 
64  cell_t *c;
65  static cell_t null;
66 };
67 
68 template <class T> typename SharedPtr<T>::cell_t SharedPtr<T>::null(0);
69 
70 } // elm
71 
72 #endif /* ELM_UTIL_SHAREDPTR_H_ */
elm::SharedPtr::operator->
T * operator->(void) const
Definition: SharedPtr.h:48
elm::SharedPtr::operator=
SharedPtr< T > & operator=(const SharedPtr< T > &p)
Definition: SharedPtr.h:51
elm::io::p
Printable< T, M > p(const T &data, const M &man)
Definition: Output.h:302
elm::SharedPtr::SharedPtr
SharedPtr(T *p)
Definition: SharedPtr.h:39
elm::SharedPtr::ptr
T * ptr(void) const
Definition: SharedPtr.h:43
elm::SharedPtr::operator==
bool operator==(T *p) const
Definition: SharedPtr.h:56
elm::SharedPtr::operator&
T * operator&(void) const
Definition: SharedPtr.h:50
void
bool
elm::SharedPtr::operator==
bool operator==(const SharedPtr< T > &p) const
Definition: SharedPtr.h:54
elm::SharedPtr::operator!=
bool operator!=(T *p) const
Definition: SharedPtr.h:57
elm
Definition: adapter.h:26
elm::SharedPtr::operator=
SharedPtr< T > & operator=(T *p)
Definition: SharedPtr.h:52
elm::SharedPtr::SharedPtr
SharedPtr(const SharedPtr< T > &p)
Definition: SharedPtr.h:40
elm::SharedPtr::SharedPtr
SharedPtr(void)
Definition: SharedPtr.h:38
elm::t::uint32
unsigned int uint32
Definition: arch.h:31
elm::SharedPtr::operator*
T & operator*(void) const
Definition: SharedPtr.h:49
elm::SharedPtr::isEmpty
bool isEmpty(void) const
Definition: SharedPtr.h:42
elm::SharedPtr::operator!=
bool operator!=(const SharedPtr< T > &p) const
Definition: SharedPtr.h:55
elm::SharedPtr::ref
T & ref(void) const
Definition: SharedPtr.h:44
elm::SharedPtr
Definition: SharedPtr.h:30
elm::SharedPtr::~SharedPtr
~SharedPtr(void)
Definition: SharedPtr.h:41