Nemiver  0.3
nmv-safe-ptr.h
Go to the documentation of this file.
1 /* -*- Mode: C++; indent-tabs-mode:nil; c-basic-offset:4; -*- */
2 
3 /*Copyright (c) 2005-2006 Dodji Seketeli
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of this
6  * software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute,
9  * sublicense, and/or sell copies of the Software, and to permit
10  * persons to whom the Software is furnished to do so,
11  * subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all copies
14  * or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS",
17  * WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
18  * INCLUDING BUT NOT LIMITED TO THE
19  * WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE
21  * AND NONINFRINGEMENT.
22  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
23  * HOLDERS BE LIABLE FOR ANY CLAIM,
24  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
25  * CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
27  * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28  *
29  */
30 #ifndef __NMV_SAFE_PTR_H__
31 #define __NMV_SAFE_PTR_H__
32 
33 #include <cstdlib>
34 
35 namespace nemiver {
36 namespace common {
37 
38 struct DefaultRef
39 {
40  void
41  operator () (const void*) {}
42 }
43 ; //end struct DefaultReference
44 
45 struct FreeUnref
46 {
47  void
48  operator () (const void* a_ptr)
49  {
50  if (a_ptr)
51  free (const_cast<void *> (a_ptr));
52  }
53 }
54 ;//end struct DefaultUnreference
55 
56 template <class PointerType>
58 {
59  void
60  operator () (const PointerType* a_ptr)
61  {
62  if (a_ptr)
63  delete (a_ptr);
64  }
65 };
66 
67 template<class PointerType,
68  class ReferenceFunctor = DefaultRef,
69  class UnreferenceFunctor = DeleteFunctor<PointerType>
70  >
71 class SafePtr
72 {
73 protected:
74  mutable PointerType *m_pointer;
75 
76 
77 public:
78  explicit SafePtr (const PointerType *a_pointer, bool a_do_ref=false) :
79  m_pointer (const_cast<PointerType*> (a_pointer))
80  {
81  if (a_do_ref) {
82  reference ();
83  }
84  }
85 
86  SafePtr () : m_pointer (0)
87  {
88  }
89 
90  SafePtr (const SafePtr<PointerType,
91  ReferenceFunctor,
92  UnreferenceFunctor> &a_safe_ptr) :
93  m_pointer (a_safe_ptr.m_pointer)
94  {
95  reference ();
96  }
97 
99  {
100  unreference ();
101  m_pointer = 0;
102  }
103 
105  operator= (const SafePtr<PointerType,
106  ReferenceFunctor,
107  UnreferenceFunctor> &a_safe_ptr)
108  {
109  SafePtr<PointerType,
110  ReferenceFunctor,
111  UnreferenceFunctor> temp (a_safe_ptr);
112  swap (temp);
113  return *this;
114  }
115 
116  /*
117  SafePtr<PointerType, ReferenceFunctor, UnreferenceFunctor>&
118  operator= (const PointerType *a_pointer)
119  {
120  reset (a_pointer);
121  return *this;
122  }
123  */
124 
125  PointerType&
126  operator* () const
127  {
128  return *(m_pointer);
129  }
130 
131  PointerType*
132  operator-> () const
133  {
134  return m_pointer;
135  }
136 
137 
138  bool operator== (const SafePtr<PointerType,
139  ReferenceFunctor,
140  UnreferenceFunctor> &a_safe_ptr) const
141  {
142  return m_pointer == a_safe_ptr.m_pointer;
143  }
144 
145  bool operator== (const PointerType *a_ptr) const
146  {
147  return m_pointer == a_ptr;
148  }
149 
150  bool operator! () const
151  {
152  if (m_pointer)
153  return false;
154  return true;
155  }
156 
157  operator bool () const
158  {
159  if (!m_pointer)
160  return false;
161  return true;
162  }
163 
164 
165  bool operator!= (const PointerType *a_pointer)
166  {
167  return !this->operator== (a_pointer);
168  }
169 
170  bool operator!= (const SafePtr<PointerType,
171  ReferenceFunctor,
172  UnreferenceFunctor> &a_safe_ptr)
173  {
174  return !this->operator== (a_safe_ptr);
175  }
176 
177  void
178  reset ()
179  {
180  reset (0);
181  }
182 
183  void
184  reset (const PointerType *a_pointer, bool a_do_ref=false)
185  {
186  if (a_pointer != m_pointer) {
187  unreference ();
188  m_pointer = const_cast<PointerType*> (a_pointer);
189  if (a_do_ref) {
190  reference ();
191  }
192  }
193  }
194 
195  PointerType*
196  get () const
197  {
198  return m_pointer;
199  }
200 
201  PointerType*
202  ref_and_get () const
203  {
204  const_cast<SafePtr<PointerType,
205  ReferenceFunctor,
206  UnreferenceFunctor>* > (this)->reference ();
207  return m_pointer;
208  }
209 
210  template <class T>
213  {
214  T *pointer = dynamic_cast<T*> (m_pointer);
216  if (result) {
217  result.reference ();
218  }
219  return result;
220  }
221 
222  PointerType*
224  {
225  PointerType* pointer = m_pointer;
226  m_pointer = 0;
227  return pointer;
228  }
229 
230  void
231  swap (SafePtr<PointerType,
232  ReferenceFunctor,
233  UnreferenceFunctor> &a_safe_ptr)
234  {
235  PointerType *const tmp (m_pointer);
236  m_pointer = a_safe_ptr.m_pointer;
237  a_safe_ptr.m_pointer = tmp;
238  }
239 
240  void
242  {
243  if (m_pointer) {
244  ReferenceFunctor do_ref;
245  do_ref (m_pointer);
246  }
247  }
248 
249  void
251  {
252  if (m_pointer) {
253  UnreferenceFunctor do_unref;
254  do_unref (m_pointer);
255  }
256  }
257 };//end class SafePtr
258 
259 }//end namespace common
260 }//end namespace nemiver
261 
262 #endif //__NMV_SAFR_PTR_H__
263 
nemiver::common::SafePtr::SafePtr
SafePtr(const PointerType *a_pointer, bool a_do_ref=false)
Definition: nmv-safe-ptr.h:78
nemiver::common::SafePtr::~SafePtr
~SafePtr()
Definition: nmv-safe-ptr.h:98
nemiver::common::SafePtr::operator=
SafePtr< PointerType, ReferenceFunctor, UnreferenceFunctor > & operator=(const SafePtr< PointerType, ReferenceFunctor, UnreferenceFunctor > &a_safe_ptr)
Definition: nmv-safe-ptr.h:105
nemiver
Definition: nmv-address.h:31
nemiver::common::SafePtr::operator!=
bool operator!=(const PointerType *a_pointer)
Definition: nmv-safe-ptr.h:165
nemiver::common::DefaultRef::operator()
void operator()(const void *)
Definition: nmv-safe-ptr.h:41
nemiver::common::SafePtr::unreference
void unreference()
Definition: nmv-safe-ptr.h:250
nemiver::common::SafePtr::SafePtr
SafePtr(const SafePtr< PointerType, ReferenceFunctor, UnreferenceFunctor > &a_safe_ptr)
Definition: nmv-safe-ptr.h:90
nemiver::common::SafePtr::m_pointer
PointerType * m_pointer
Definition: nmv-safe-ptr.h:74
nemiver::common::SafePtr::operator==
bool operator==(const SafePtr< PointerType, ReferenceFunctor, UnreferenceFunctor > &a_safe_ptr) const
Definition: nmv-safe-ptr.h:138
nemiver::common::SafePtr::swap
void swap(SafePtr< PointerType, ReferenceFunctor, UnreferenceFunctor > &a_safe_ptr)
Definition: nmv-safe-ptr.h:231
nemiver::common::SafePtr::reset
void reset(const PointerType *a_pointer, bool a_do_ref=false)
Definition: nmv-safe-ptr.h:184
nemiver::common::SafePtr::reset
void reset()
Definition: nmv-safe-ptr.h:178
nemiver::common::SafePtr::release
PointerType * release()
Definition: nmv-safe-ptr.h:223
nemiver::common::SafePtr::ref_and_get
PointerType * ref_and_get() const
Definition: nmv-safe-ptr.h:202
nemiver::common::DefaultRef
Definition: nmv-safe-ptr.h:38
nemiver::common::FreeUnref::operator()
void operator()(const void *a_ptr)
Definition: nmv-safe-ptr.h:48
nemiver::common::DeleteFunctor
Definition: nmv-safe-ptr.h:57
nemiver::common::SafePtr::reference
void reference()
Definition: nmv-safe-ptr.h:241
nemiver::common::SafePtr::get
PointerType * get() const
Definition: nmv-safe-ptr.h:196
nemiver::common::SafePtr::operator*
PointerType & operator*() const
Definition: nmv-safe-ptr.h:126
nemiver::common::SafePtr::SafePtr
SafePtr()
Definition: nmv-safe-ptr.h:86
nemiver::common::SafePtr::operator!
bool operator!() const
Definition: nmv-safe-ptr.h:150
nemiver::common::SafePtr
Definition: nmv-safe-ptr.h:71
nemiver::common::SafePtr::do_dynamic_cast
SafePtr< T, ReferenceFunctor, UnreferenceFunctor > do_dynamic_cast()
Definition: nmv-safe-ptr.h:212
nemiver::common::FreeUnref
Definition: nmv-safe-ptr.h:45
nemiver::common::SafePtr::operator->
PointerType * operator->() const
Definition: nmv-safe-ptr.h:132
nemiver::common::DeleteFunctor::operator()
void operator()(const PointerType *a_ptr)
Definition: nmv-safe-ptr.h:60
common
Definition: nmv-proc-list-dialog.h:32