OpendTect-6_4  6.4
ptrman.h
Go to the documentation of this file.
1 #ifndef ptrman_h
2 #define ptrman_h
3 
4 /*+
5 ________________________________________________________________________
6 
7  (C) dGB Beheer B.V.; (LICENSE) http://opendtect.org/OpendTect_license.txt
8  Author: Kristofer Tingdahl
9  Date: 10-12-1999
10  RCS: $Id$
11 ________________________________________________________________________
12 
13 -*/
14 
15 #include "gendefs.h"
16 #include "atomic.h"
17 #include <stdlib.h>
18 
19 #ifdef __debug__
20 # include "debug.h"
21 #endif
22 
23 #define mImpPtrManPointerAccess( qual, type ) \
24  inline qual type* ptr() qual { return this->ptr_; } \
25  inline operator qual type*() qual { return this->ptr_; }\
26  inline qual type* operator ->() qual { return this->ptr_; } \
27  inline qual type& operator *() qual { return *this->ptr_; }
28 
31 template <class T>
32 void deleteAndZeroPtr( T*& ptr, bool isowner=true )
33 { if ( isowner ) delete ptr; ptr = 0; }
34 
35 
36 template <class T>
37 void deleteAndZeroArrPtr( T*& ptr, bool isowner=true )
38 { if ( isowner ) delete [] ptr; ptr = 0; }
39 
40 template <class T> T* createSingleObject() { return new T; }
41 template <class T> T* createObjectArray(od_int64 sz) { return new T[sz]; }
42 
46 template<class T>
48 {
49 public:
50  inline bool operator !() const { return !ptr_; }
51 
52  inline T* set(T* p, bool doerase=true);
54  inline T* release() { return set(0,false); }
56  inline void erase() { set( 0, true ); }
57 
58  inline bool setIfNull(T* p);
59 
60  typedef T* (*PointerCreator)();
61  inline T* createIfNull(PointerCreator=createSingleObject<T>);
64 protected:
65 
66  typedef void (*PtrFunc)(T*);
67  inline PtrManBase(PtrFunc setfunc,PtrFunc deletor,T*);
68  virtual ~PtrManBase() { set(0,true); }
69 
71 
72  PtrFunc setfunc_;
73  PtrFunc deletefunc_;
74 };
75 
76 
78 template <class T>
79 mClass(Basic) PtrMan : public PtrManBase<T>
80 {
81 public:
82  PtrMan(const PtrMan<T>&);
84  inline PtrMan(T* = 0);
85  PtrMan<T>& operator=( T* p );
86 
87  PtrMan<T>& operator=(const PtrMan<T>&);
91 
92 private:
93 
94  static void deleteFunc( T* p ) { delete p; }
95 
96 };
97 
98 
100 template <class T>
101 mClass(Basic) ConstPtrMan : public PtrManBase<T>
102 {
103 public:
104  ConstPtrMan(const ConstPtrMan<T>&);
105  //Don't use
106  inline ConstPtrMan(const T* = 0);
107  ConstPtrMan<T>& operator=(const T* p);
108  ConstPtrMan<T>& operator=(const ConstPtrMan<T>&);
111 private:
112 
113  static void deleteFunc( T* p ) { delete p; }
114 };
115 
116 
118 template <class T>
119 mClass(Basic) ArrPtrMan : public PtrManBase<T>
120 {
121 public:
122  ArrPtrMan(const ArrPtrMan<T>&);
124  inline ArrPtrMan(T* = 0);
125  ArrPtrMan<T>& operator=( T* p );
126  inline ArrPtrMan<T>& operator=(const ArrPtrMan<T>& p );
128 
131 #ifdef __debug__
132  T& operator[](int);
133  const T& operator[](int) const;
134  T& operator[](od_int64);
135  const T& operator[](od_int64) const;
136 
137 #endif
138  void setSize(od_int64 size) { size_=size; }
139 
140 private:
141 
142  static void deleteFunc( T* p ) { delete [] p; }
143 
145 };
146 
147 
149 template <class T>
150 mClass(Basic) ConstArrPtrMan : public PtrManBase<T>
151 {
152 public:
154  //Don't use
155  inline ConstArrPtrMan(const T* = 0);
156  ConstArrPtrMan<T>& operator=(const T* p);
157  ConstArrPtrMan<T>& operator=(const ConstArrPtrMan<T>&);
160 private:
161 
162  static void deleteFunc( T* p ) { delete p; }
163 };
164 
165 
167 template <class T>
168 mClass(Basic) RefMan : public PtrManBase<T>
169 {
170 public:
171 
172  inline RefMan(const RefMan<T>&);
173  inline RefMan(T* = 0);
174  inline RefMan<T>& operator=( T* p )
175  { this->set( p, true ); return *this; }
176  inline RefMan<T>& operator=(const RefMan<T>&);
179 
180 private:
181 
182  static void ref(T* p) { p->ref(); }
183  static void unRef(T* p) { if ( p ) p->unRef(); }
184 
185 };
186 
187 
189 template <class T>
190 mClass(Basic) ConstRefMan : public PtrManBase<T>
191 {
192 public:
193  inline ConstRefMan(const ConstRefMan<T>&);
194  inline ConstRefMan(const T* = 0);
195  ConstRefMan<T>& operator=(const T* p);
196  inline ConstRefMan<T>& operator=(const ConstRefMan<T>&);
197 
199 
200 private:
201  static void ref(T* p) { p->ref(); }
202  static void unRef(T* p) { if ( p ) p->unRef(); }
203 
204 };
205 
206 #undef mImpPtrManPointerAccess
207 
208 //Implementations below
209 
210 template <class T> inline
211 PtrManBase<T>::PtrManBase( PtrFunc setfunc, PtrFunc deletor, T* p )
212  : deletefunc_( deletor )
213  , setfunc_( setfunc )
214 {
215  this->set(p);
216 }
217 
218 
219 template <class T> inline
220 T* PtrManBase<T>::set( T* p, bool doerase )
221 {
222  if ( setfunc_ && p )
223  setfunc_(p);
224 
225  T* oldptr = ptr_.exchange(p);
226  if ( doerase )
227  {
228  deletefunc_( oldptr );
229  return 0;
230  }
231 
232  return oldptr;
233 }
234 
235 
236 template <class T> inline
238 {
239  if ( ptr_.setIfEqual( 0, p ) )
240  {
241  if ( setfunc_ && p )
242  setfunc_(p);
243  return true;
244  }
245 
246  return false;
247 }
248 
249 
250 template <class T> inline
252 {
253  if ( ptr_ )
254  return ptr_;
255 
256  T* newptr = creator();
257  if ( !newptr )
258  return 0;
259 
260  if ( !setIfNull(newptr) )
261  {
262  if ( setfunc_ ) setfunc_(newptr);
263  if ( deletefunc_ ) deletefunc_(newptr);
264  }
265 
266  return ptr_;
267 }
268 
269 
270 template <class T> inline
272  : PtrManBase<T>( 0, deleteFunc, 0 )
273 {
274  pErrMsg("Should not be called");
275 }
276 
277 
278 template <class T> inline
280 {
281  PtrManBase<T>::set( 0, true );
282  pErrMsg("Should not be called");
283  return *this;
284 }
285 
286 
287 template <class T> inline
289  : PtrManBase<T>( 0, deleteFunc, p )
290 {}
291 
292 
293 template <class T> inline
295 {
296  this->set( p );
297  return *this;
298 }
299 
300 
301 template <class T> inline
303  : PtrManBase<T>( 0, deleteFunc, 0 )
304 {
305  pErrMsg("Should not be called");
306 }
307 
308 
309 template <class T> inline
311 {
312  PtrManBase<T>::set( 0, true );
313  pErrMsg("Should not be called");
314  return *this;
315 }
316 
317 
318 template <class T> inline
320  : PtrManBase<T>( 0, deleteFunc, const_cast<T*>(p) )
321 {}
322 
323 
324 template <class T> inline
326 {
327  this->set( const_cast<T*>( p ) );
328  return *this;
329 }
330 
331 
332 template <class T> inline
334  : PtrManBase<T>( 0, deleteFunc, 0 )
335 {
336  pErrMsg("Should not be called");
337 }
338 
339 
340 template <class T> inline
342 {
343  PtrManBase<T>::set( 0, true );
344  pErrMsg("Should not be called");
345  return *this;
346 }
347 
348 
349 template <class T> inline
351  : PtrManBase<T>( 0, deleteFunc, p )
352  , size_(-1)
353 {}
354 
355 
356 template <class T> inline
358 {
359  this->set( p );
360  return *this;
361 }
362 
363 #ifdef __debug__
364 
365 template <class T> inline
366 T& ArrPtrMan<T>::operator[]( int idx )
367 {
368  if ( idx<0 || (size_>=0 && idx>=size_) )
369  {
370  DBG::forceCrash(true);
371  }
372  return this->ptr_[(size_t) idx];
373 }
374 
375 
376 template <class T> inline
377 const T& ArrPtrMan<T>::operator[]( int idx ) const
378 {
379  if ( idx<0 || (size_>=0 && idx>=size_) )
380  {
381  DBG::forceCrash(true);
382  }
383  return this->ptr_[(size_t) idx];
384 }
385 
386 
387 template <class T> inline
389 {
390  if ( idx<0 || (size_>=0 && idx>=size_) )
391  {
392  DBG::forceCrash(true);
393  }
394  return this->ptr_[(size_t) idx];
395 }
396 
397 
398 template <class T> inline
399 const T& ArrPtrMan<T>::operator[]( od_int64 idx ) const
400 {
401  if ( idx<0 || (size_>=0 && idx>=size_) )
402  {
403  DBG::forceCrash(true);
404  }
405  return this->ptr_[(size_t) idx];
406 }
407 
408 #endif
409 
410 
411 
412 template <class T> inline
414  : PtrManBase<T>( 0, deleteFunc, 0 )
415 {
416  pErrMsg("Shold not be called");
417 }
418 
419 
420 template <class T> inline
422  : PtrManBase<T>( 0, deleteFunc, const_cast<T*>(p) )
423 {}
424 
425 
426 template <class T> inline
428 {
429  this->set( const_cast<T*>(p) );
430  return *this;
431 }
432 
433 
434 template <class T> inline
436  : PtrManBase<T>( ref, unRef, const_cast<T*>(p.ptr()) )
437 {}
438 
439 
440 template <class T> inline
442  : PtrManBase<T>( ref, unRef, p )
443 {}
444 
445 
446 template <class T> inline
448 {
449  this->set( const_cast<T*>(p.ptr()) );
450  return *this;
451 }
452 
453 
454 template <class T> inline
456  : PtrManBase<T>( ref, unRef, const_cast<T*>(p.ptr()) )
457 {}
458 
459 
460 template <class T> inline
462  : PtrManBase<T>( ref, unRef, const_cast<T*>(p) )
463 {}
464 
465 
466 template <class T> inline
468 {
469  this->set( const_cast<T*>(p.ptr()) );
470  return *this;
471 }
472 
473 
474 template <class T> inline
476 {
477  this->set( const_cast<T*>( p ) );
478  return *this;
479 }
480 
481 #endif
PtrMan(const PtrMan< T > &)
Don&#39;t use.
Definition: ptrman.h:271
static void ref(T *p)
Definition: ptrman.h:182
static void deleteFunc(T *p)
Definition: ptrman.h:142
RefMan(const RefMan< T > &)
Definition: ptrman.h:435
#define od_int64
Definition: plftypes.h:36
#define private
Definition: winstreambuf.h:13
PtrManBase(PtrFunc setfunc, PtrFunc deletor, T *)
Definition: ptrman.h:211
ConstPtrMan(const ConstPtrMan< T > &)
Definition: ptrman.h:302
ConstArrPtrMan< T > & operator=(const T *p)
Definition: ptrman.h:427
ArrPtrMan< T > & operator=(T *p)
Definition: ptrman.h:357
int64_t size_
Definition: ptrman.h:144
static void unRef(T *p)
Definition: ptrman.h:183
ConstPtrMan< T > & operator=(const T *p)
Definition: ptrman.h:325
virtual ~PtrManBase()
Definition: ptrman.h:68
T *(* PointerCreator)()
Definition: ptrman.h:60
Definition: ptrman.h:47
void deleteAndZeroArrPtr(T *&ptr, bool isowner=true)
Definition: ptrman.h:37
Definition: ptrman.h:79
static void deleteFunc(T *p)
Definition: ptrman.h:94
PtrFunc deletefunc_
Definition: ptrman.h:73
static void unRef(T *p)
Definition: ptrman.h:202
ArrPtrMan(const ArrPtrMan< T > &)
Don&#39;t use.
Definition: ptrman.h:333
const T * ptr() const
Definition: ptrman.h:159
Definition: ptrman.h:150
T * createIfNull(PointerCreator=createSingleObject< T >)
Definition: ptrman.h:251
ConstRefMan< T > & operator=(const T *p)
Definition: ptrman.h:475
Atomic instantiated with a pointer. The class really only handles the casting from a void* to a T*...
Definition: atomic.h:144
Definition: ptrman.h:119
void setSize(int64_t size)
Definition: ptrman.h:138
#define mImpPtrManPointerAccess(qual, type)
Definition: ptrman.h:23
T * createSingleObject()
Definition: ptrman.h:40
void erase()
Definition: ptrman.h:56
void forceCrash(bool withdump)
static void deleteFunc(T *p)
Definition: ptrman.h:113
void deleteAndZeroPtr(T *&ptr, bool isowner=true)
Definition: ptrman.h:32
RefMan< T > & operator=(T *p)
Definition: ptrman.h:174
T * release()
Returns pointer. I won&#39;t take care of it any longer.
Definition: ptrman.h:54
ConstArrPtrMan(const ConstArrPtrMan< T > &)
Definition: ptrman.h:413
const T * ptr() const
Definition: ptrman.h:198
T * set(T *p, bool doerase=true)
Returns old pointer if not erased.
Definition: ptrman.h:220
ConstRefMan(const ConstRefMan< T > &)
Definition: ptrman.h:455
#define mClass(module)
Definition: commondefs.h:164
#define pErrMsg(msg)
Definition: errmsg.h:60
T * createObjectArray(int64_t sz)
Definition: ptrman.h:41
Threads::AtomicPointer< T > ptr_
Definition: ptrman.h:70
Definition: ptrman.h:190
const T * ptr() const
Definition: ptrman.h:177
Definition: ptrman.h:168
PtrFunc setfunc_
Definition: ptrman.h:72
Definition: ptrman.h:101
static void deleteFunc(T *p)
Definition: ptrman.h:162
bool setIfNull(T *p)
Definition: ptrman.h:237
static void ref(T *p)
Definition: ptrman.h:201
PtrMan< T > & operator=(T *p)
Definition: ptrman.h:294

Generated at for the OpendTect seismic interpretation project. Copyright (C): dGB Beheer B. V. 2019