aimstil  5.0.5
Ptr.h
Go to the documentation of this file.
1 #ifndef TIL_PTR_H
2 #define TIL_PTR_H
3 
4 // Includes from TIL library
5 #include "til/til_common.h"
6 #include "til/labelClasses.h"
7 #include "til/templateTools.h"
8 
9 // Ptr and ConstPtr replace standard C pointers in order to have
10 // automatic deallocation (garbage collection).
11 
12 // Ptr and ConstPtr are used to point at objects deriving from SmartObject.
13 // When pointing to a new SmartObject (i.e. via a constructor, or an operator=)
14 // they register to this object.
15 // When stopping to point to a SmartObject (i.e. via a destructor or an operator=)
16 // they unregister to this object. If they are the last to unregister, it means no
17 // pointer is pointing to this object anymore, and the object is destroyed.
18 
19 // Ptr allows the user to modify the object pointed at, ConstPtr only allows
20 // const-compatible function and members call. Ptr should not point to a const
21 // object: ConstPtr should be used in that case.
22 
23 // A Ptr can be used whenever a ConstPtr is needed.
24 
25 //#define SMARTOBJ_DEBUG
26 
27 // Namespace
28 
29 namespace til {
30 
31 
33 template < class T >
34 class ConstPtr : public SmartPtr_label
35 {
36 
37 public: // typedefs
38 
39  typedef ConstPtr<T> Self;
40  typedef T DataType;
41 
42 public: // constructors & destructor
43 
45  // TODO: Is it better to initialize with default constructor of T
46  // instead? It seems at least safer, because a call to a member of T
47  // via operator-> will be always safe.
49 
51  {
52 #ifdef SMARTOBJ_DEBUG
53  std::cout << "ConstPtr constr(ptr)" <<std::endl;
54 #endif
55  m_pointer->subscribe();
56  }
57 
58 
59  // Deals with ConstPtr<T> ptr = p
60  ConstPtr(const T *p) : m_pointer(const_cast<T*>(p))
61  {
62 #ifdef SMARTOBJ_DEBUG
63  std::cout << "ConstPtr constr(p)" << std::endl;
64 #endif
65  m_pointer->subscribe();
66  }
67 
68  template < class U >
69  ConstPtr(const U *p) : m_pointer(const_cast<U*>(p))
70  {
71 #ifdef SMARTOBJ_DEBUG
72  std::cout << "ConstPtr constr(p)" << std::endl;
73 #endif
74  m_pointer->subscribe();
75  }
76 
77  // Destructor
78  // NB: the single following 'virtual' is the only reason why sizeof(Ptr) is
79  // 8 and not 4. And thus we have to use &Ptr everywhere, instead of
80  // simple Ptr. Is that what we want? Is it bad to remove the virtual
81  // here if no data is added to inheriting classes?
82  virtual ~ConstPtr()
83  {
84  if (m_pointer)
85  {
86  m_pointer->unsubscribe();
87  m_pointer = 0;
88  }
89  }
90 
91 
92 
93 public: // functions
94 
96  operator const T*(void) const { return m_pointer; }
97 
98  // We can write *ptr to get the object
99  // Add an exception here if p==0;
100  const T& operator*(void) const { return *m_pointer; }
101 
102 
103  // We can access members via ptr->
104  const T* operator->(void) const { return m_pointer; }
105 
106  // We can write ptr1 = ptr2
108  {
109  return operator=((const T*)(p));
110  }
111 
118  ConstPtr& operator=(const T* p)
119  {
120  // First check that the new pointer is different from the old.
121  if (m_pointer != p)
122  {
123  // If we were pointing to an object, unsubscribe from it.
124  if (m_pointer)
125  {
126  m_pointer->unsubscribe();
127  }
128 
129  // Now point to the new object
130  m_pointer = const_cast<T*>(p);
131 
132  // If the new object is not NULL, subscribe to it
133  if (m_pointer)
134  {
135  m_pointer->subscribe();
136  }
137  }
138  return *this;
139  }
140 
141 
142  // NB: *apparently* the following operators are not needed and even
143  // worse should not be there. When we compare a Ptr with a T* the compiler
144  // apparently cannot decide which operator use, between those and the
145  // built-in one between two T*. So, without these, the Ptr are cast
146  // into T* and compared as such. Fine with me, but might not always
147  // be good. Dunno how to change that though.
148 
149 
151  bool operator==(const T* p) const { return m_pointer == p;}
152  bool operator==(const ConstPtr<T> &p) const { return this->operator==((const T*)p);}
153 
155  bool operator!=(const T* p) const { return !this->operator==(p); }
156  bool operator!=(const ConstPtr<T> &p) const { return !this->operator==(p); }
157 
158 
159  // Checks whether the pointer points to something
160  // depreciated: just type p != 0, just like ordinary pointers
161  // plus, isALlocated is a bad terminology: Ptr could point to
162  // an image object, that is not allocated...
163  // bool isAllocated() const { return m_pointer != 0; }
164 
165 
166 protected: // data
167 
169 };
170 
171 
172 
176 template < class T >
177 class Ptr : public ConstPtr<T>
178 {
179 public: // typedefs
180 
181  typedef Ptr<T> Self;
182 
183 public: // constructors & destructor
184 
185  Ptr() : ConstPtr<T>() {};
186  Ptr(T* p) : ConstPtr<T>(p) {};
187  template < class U >
188  Ptr(U* p) : ConstPtr<T>(p) {};
189 
190 public: // operators
191 
192  // Basically the same operators than ConstPtr's, except that they allow non-const
193  // operations on the data.
194 
195  operator T*(void) const { return this->m_pointer; }
196  T& operator*(void) const { return *(this->m_pointer); }
197  T* operator->(void) const { return this->m_pointer; }
198  Ptr& operator=(T* p)
199  {
200  ((ConstPtr<T>*)(this))->operator=(p);
201  return *this;
202  }
203 };
204 
205 
206 } // namespace
207 
208 
209 #endif
210 
ConstPtr & operator=(const ConstPtr< T > &p)
Definition: Ptr.h:107
T * m_pointer
Definition: Ptr.h:168
bool operator!=(const ConstPtr< T > &p) const
Definition: Ptr.h:156
T DataType
Definition: Ptr.h:40
bool operator==(const T *p) const
Checks whether two pointers point to the same object.
Definition: Ptr.h:151
T & operator*(void) const
Definition: Ptr.h:196
const T * operator->(void) const
Definition: Ptr.h:104
ConstPtr(const T *p)
Definition: Ptr.h:60
ConstPtr< T > Self
Definition: Ptr.h:39
bool operator!=(const T *p) const
Checks whether two pointers do not point to the same object.
Definition: Ptr.h:155
Belongs to package Box Do not include directly, include til/Box.h instead.
Definition: Accumulator.h:10
bool operator==(const ConstPtr< T > &p) const
Definition: Ptr.h:152
General macros, definitions and functions.
Ptr(T *p)
Definition: Ptr.h:186
Ptr & operator=(T *p)
Definition: Ptr.h:198
ConstPtr(const U *p)
Definition: Ptr.h:69
Smart pointer for objects that can be modified.
Definition: Ptr.h:177
Ptr< T > Self
Definition: Ptr.h:181
ConstPtr()
Default constructor, pointing to NULL.
Definition: Ptr.h:48
ConstPtr(const ConstPtr< T > &ptr)
Definition: Ptr.h:50
Ptr(U *p)
Definition: Ptr.h:188
T * operator->(void) const
Definition: Ptr.h:197
ConstPtr & operator=(const T *p)
Assignement to a pointer.
Definition: Ptr.h:118
Collects template tools used for library implementation.
Ptr()
Definition: Ptr.h:185
Smart pointer for constant objects deriving from SmartObject.
Definition: Ptr.h:34
const T & operator*(void) const
Definition: Ptr.h:100
virtual ~ConstPtr()
Definition: Ptr.h:82