OpendTect  6.6
ptrman.h
Go to the documentation of this file.
1 #pragma once
2 
3 /*+
4 ________________________________________________________________________
5 
6  (C) dGB Beheer B.V.; (LICENSE) http://opendtect.org/OpendTect_license.txt
7  Author: Kristofer Tingdahl
8  Date: 10-12-1999
9  RCS: $Id$
10 ________________________________________________________________________
11 
12 -*/
13 
14 #include "gendefs.h"
15 #include "atomic.h"
16 #include "refcount.h"
17 
18 #ifdef __debug__
19 # include "debug.h"
20 #endif
21 
22 template <class T> class WeakPtr;
23 
26 template <class T>
27 void deleteAndZeroPtr( T*& ptr, bool isowner=true )
28 { if ( isowner ) delete ptr; ptr = nullptr; }
29 
30 
31 template <class T>
32 void deleteAndZeroArrPtr( T*& ptr, bool isowner=true )
33 { if ( isowner ) delete [] ptr; ptr = nullptr; }
34 
35 template <class T> T* createSingleObject() { return new T; }
36 template <class T> T* createObjectArray(od_int64 sz) { return new T[sz]; }
37 
41 template<class T>
43 {
44 public:
45  inline bool operator !() const { return !ptr_; }
46  inline T* set(T* p, bool doerase=true);
48  inline T* release() { return set(0,false); }
50  inline void erase() { set( 0, true ); }
51 
52  inline bool setIfNull(T* p,bool takeover);
56  typedef T* (*PointerCreator)();
57  inline T* createIfNull(PointerCreator=createSingleObject<T>);
60 protected:
61 
62  typedef void (*PtrFunc)(T*);
63  inline PtrManBase(PtrFunc setfunc,PtrFunc deletor,T*);
64  virtual ~PtrManBase() { set(0,true); }
65 
67 
68  PtrFunc setfunc_;
69  PtrFunc deletefunc_;
70 
71 public:
72 
73  mDeprecated("Let it takeover") inline bool setIfNull( T* p )
74  { return setIfNull( p, false ); }
75 };
76 
77 
78 template<class T>
80 {
81 public:
82  inline const T* ptr() const { return this->ptr_; }
83  inline operator const T*() const { return this->ptr_; }
84  inline const T* operator->() const { return this->ptr_; }
85  inline const T& operator*() const { return *this->ptr_; }
86  inline T* getNonConstPtr() const
87  { return const_cast<T*>(this->ptr()); }
88 protected:
89  typedef void (*PtrFunc)(T*);
90  inline ConstPtrManBase(PtrFunc setfunc,PtrFunc deletor,T* p)
91  : PtrManBase<T>( setfunc, deletor, p )
92  {}
93 };
94 
95 
96 template<class T>
98 {
99 public:
100  inline const T* ptr() const { return this->ptr_; }
101  inline operator const T*() const { return this->ptr_; }
102  inline const T* operator->() const { return this->ptr_; }
103  inline const T& operator*() const { return *this->ptr_; }
104  inline T* ptr() { return this->ptr_; }
105  inline operator T*() { return this->ptr_; }
106  inline T* operator ->() { return this->ptr_; }
107  inline T& operator *() { return *this->ptr_; }
108  inline T* getNonConstPtr() const
109  { return const_cast<T*>(this->ptr()); }
110 protected:
111  typedef void (*PtrFunc)(T*);
112  inline NonConstPtrManBase(PtrFunc setfunc,PtrFunc deletor,T* p)
113  : PtrManBase<T>( setfunc, deletor, p )
114  {}
115 };
116 
117 
119 template <class T>
121 {
122 public:
123  inline PtrMan(T* = nullptr);
124  inline PtrMan(PtrMan<T>&&);
126 
127  PtrMan(const PtrMan<T>&) = delete;
128  PtrMan<T>& operator=(const PtrMan<T>&) = delete;
129 
130 
131 private:
132 
133  static void deleteFunc( T* p ) { delete p; }
134 
135 };
136 
137 
139 template <class T>
141 {
142 public:
143 
144  inline ConstPtrMan(const T* = nullptr);
146  ConstPtrMan<T>& operator=(const T* p);
147 
148  ConstPtrMan(const ConstPtrMan<T>&) = delete;
150 
151 private:
152 
153  static void deleteFunc( T* p ) { delete p; }
154 };
155 
156 
158 template <class T>
160 {
161 public:
162  inline ArrPtrMan(T* = nullptr);
165 
166 
167 #ifdef __debug__
168  T& operator[](int);
169  const T& operator[](int) const;
170  T& operator[](od_int64);
171  const T& operator[](od_int64) const;
172 
173 #endif
174  void setSize(od_int64 size) { size_=size; }
175 
176  ArrPtrMan(const ArrPtrMan<T>&) = delete;
177  inline ArrPtrMan<T>& operator=(const ArrPtrMan<T>&) = delete;
178 
179 private:
180 
181  static void deleteFunc( T* p ) { delete [] p; }
182 
184 };
185 
186 
188 template <class T>
190 {
191 public:
192  inline ConstArrPtrMan(const T* = nullptr);
193  ConstArrPtrMan<T>& operator=(const T* p);
194 
197 private:
198 
199  static void deleteFunc( T* p ) { delete p; }
200 };
201 
202 
204 template <class T>
206 {
207 public:
208 
209  template <class TT> inline RefMan(const RefMan<TT>&);
210  inline RefMan(const RefMan<T>&);
211  inline RefMan(const WeakPtr<T>&);
212  inline RefMan(T* = nullptr);
213  inline RefMan<T>& operator=( T* p )
214  { this->set( p, true ); return *this; }
215  template <class TT>
216  inline RefMan<T>& operator=(const RefMan<TT>&);
217  inline RefMan<T>& operator=(const RefMan<T>&);
218  inline RefMan<T>& operator=(const WeakPtr<T>&);
219 
220  void setNoDelete(bool yn);
221 
222 private:
223 
224  static void ref(T* p);
225  static void unRef(T* p);
226  static void unRefNoDelete(T* p);
227 };
228 
229 
231 template <class T>
233 {
234 public:
235  inline ConstRefMan(const ConstRefMan<T>&);
236  template <class TT> inline ConstRefMan(const ConstRefMan<TT>&);
237  template <class TT> inline ConstRefMan(const RefMan<TT>&);
238 
239  inline ConstRefMan(const T* = nullptr);
240  ConstRefMan<T>& operator=(const T* p);
241  template <class TT>
243  template <class TT>
246 
247 
248  void setNoDelete(bool yn);
249 
250 private:
251  static void ref(T* p);
252  static void unRef(T* p);
253  static void unRefNoDelete(T* p);
254 
255 };
256 
257 //Implementations below
258 
259 template <class T> inline
260 PtrManBase<T>::PtrManBase( PtrFunc setfunc, PtrFunc deletor, T* p )
261  : deletefunc_( deletor )
262  , setfunc_( setfunc )
263 {
264  this->set(p);
265 }
266 
267 
268 template <class T> inline
269 T* PtrManBase<T>::set( T* p, bool doerase )
270 {
271  if ( setfunc_ && p )
272  setfunc_(p);
273 
274  T* oldptr = ptr_.exchange(p);
275  if ( doerase )
276  {
277  deletefunc_( oldptr );
278  return 0;
279  }
280 
281  return oldptr;
282 }
283 
284 
285 template <class T> inline
286 bool PtrManBase<T>::setIfNull( T* p, bool takeover )
287 {
288  if ( ptr_.setIfEqual( 0, p ) )
289  {
290  if ( setfunc_ && p )
291  setfunc_(p);
292  return true;
293  }
294 
295  if ( takeover && p )
296  {
297  if ( setfunc_ ) setfunc_(p);
298  if ( deletefunc_ ) deletefunc_(p);
299  }
300 
301  return false;
302 }
303 
304 
305 template <class T> inline
306 T* PtrManBase<T>::createIfNull(PointerCreator creator)
307 {
308  if ( ptr_ )
309  return ptr_;
310 
311  T* newptr = creator();
312  if ( !newptr )
313  return 0;
314 
315  setIfNull(newptr,true);
316 
317  return ptr_;
318 }
319 
320 template <class T> inline
322  : NonConstPtrManBase<T>( 0, deleteFunc, p )
323 {}
324 
325 
326 template <class T> inline
328  : PtrMan<T>( p.release() )
329 {}
330 
331 
332 template <class T> inline
334 {
335  this->set( p );
336  return *this;
337 }
338 
339 
340 template <class T> inline
342  : ConstPtrManBase<T>( 0, deleteFunc, const_cast<T*>(p) )
343 {}
344 
345 
346 template <class T> inline
348  : ConstPtrMan<T>( p.release() )
349 {}
350 
351 
352 template <class T> inline
354 {
355  this->set( const_cast<T*>( p ) );
356  return *this;
357 }
358 
359 
360 template <class T> inline
362  : NonConstPtrManBase<T>( 0, deleteFunc, p )
363  , size_(-1)
364 {}
365 
366 template <class T> inline
368  : ArrPtrMan<T>( p.release() )
369 {}
370 
371 
372 template <class T> inline
374 {
375  this->set( p );
376  return *this;
377 }
378 
379 #ifdef __debug__
380 
381 template <class T> inline
382 T& ArrPtrMan<T>::operator[]( int idx )
383 {
384  if ( idx<0 || (size_>=0 && idx>=size_) )
385  {
386  DBG::forceCrash(true);
387  }
388  return this->ptr_[(size_t) idx];
389 }
390 
391 
392 template <class T> inline
393 const T& ArrPtrMan<T>::operator[]( int idx ) const
394 {
395  if ( idx<0 || (size_>=0 && idx>=size_) )
396  {
397  DBG::forceCrash(true);
398  }
399  return this->ptr_[(size_t) idx];
400 }
401 
402 
403 template <class T> inline
405 {
406  if ( idx<0 || (size_>=0 && idx>=size_) )
407  {
408  DBG::forceCrash(true);
409  }
410  return this->ptr_[(size_t) idx];
411 }
412 
413 
414 template <class T> inline
415 const T& ArrPtrMan<T>::operator[]( od_int64 idx ) const
416 {
417  if ( idx<0 || (size_>=0 && idx>=size_) )
418  {
419  DBG::forceCrash(true);
420  }
421  return this->ptr_[(size_t) idx];
422 }
423 
424 #endif
425 
426 
427 template <class T> inline
429  : ConstPtrManBase<T>( 0, deleteFunc, const_cast<T*>(p) )
430 {}
431 
432 
433 template <class T> inline
435 {
436  this->set( const_cast<T*>(p) );
437  return *this;
438 }
439 
440 
441 template <class T> inline
443  : NonConstPtrManBase<T>( ref, unRef, const_cast<T*>(p.ptr()) )
444 {}
445 
446 
447 template <class T> inline
449  : NonConstPtrManBase<T>( ref, unRef, 0 )
450 {
451  *this = p;
452 }
453 
454 
455 template <class T>
456 template <class TT> inline
458  : NonConstPtrManBase<T>( ref, unRef, (T*) p.ptr() )
459 {}
460 
461 
462 template <class T> inline
464  : NonConstPtrManBase<T>( ref, unRef, p )
465 {}
466 
467 
468 template <class T> inline
470 {
471  this->set( const_cast<T*>(p.ptr()) );
472  return *this;
473 }
474 
475 
476 template <class T> inline
478 {
479  return RefMan<T>::operator=( p.get() );
480 }
481 
482 
483 template <class T>
484 template <class TT> inline
486 {
487  this->set( (T*) p.ptr() );
488  return *this;
489 }
490 
491 
492 
493 template <class T> inline
494 void RefMan<T>::setNoDelete( bool yn )
495 {
496  this->deletefunc_ = yn ? unRefNoDelete : unRef;
497 }
498 
499 
500 template <class T> inline
501 void RefMan<T>::ref( T* p )
502 {
503  mDynamicCastGet(RefCount::Referenced*,refcp,p)
504  if ( refcp )
505  refPtr( refcp );
506  else if ( p )
507  p->ref();
508 }
509 
510 
511 template <class T> inline
512 void RefMan<T>::unRef( T* p )
513 {
514  mDynamicCastGet(RefCount::Referenced*,refcp,p)
515  if ( refcp )
516  unRefPtr( refcp );
517  else if ( p )
518  p->unRef();
519 }
520 
521 
522 template <class T> inline
524 {
525  mDynamicCastGet(RefCount::Referenced*,refcp,p)
526  if ( refcp )
527  unRefNoDeletePtr( refcp );
528  else if ( p )
529  p->unRefNoDelete();
530 }
531 
532 
533 
534 template <class T> inline
536  : ConstPtrManBase<T>( ref, unRef, const_cast<T*>(p.ptr()) )
537 {}
538 
539 
540 template <class T> inline
542  : ConstPtrManBase<T>( ref, unRef, const_cast<T*>(p) )
543 {}
544 
545 
546 template <class T>
547 template <class TT> inline
549  : ConstPtrManBase<T>( ref, unRef, (T*) p.ptr() )
550 {}
551 
552 
553 template <class T>
554 template <class TT> inline
556  : ConstPtrManBase<T>( ref, unRef, (T*) p.ptr() )
557 {}
558 
559 
560 template <class T>
561 template <class TT> inline
563 {
564  this->set( (T*) p.ptr() );
565  return *this;
566 }
567 
568 
569 template <class T> inline
571 {
572  this->set( (T*) p.ptr() );
573  return *this;
574 }
575 
576 
577 template <class T> inline
579 {
580  this->set( const_cast<T*>( p ) );
581  return *this;
582 }
583 
584 
585 template <class T>
586 template <class TT> inline
588 {
589  this->set( (T*) p.ptr() );
590  return *this;
591 }
592 
593 
594 template <class T> inline
596 {
597  this->deletefunc_ = yn ? unRefNoDelete : unRef;
598 }
599 
600 
601 template <class T> inline
603 {
604  mDynamicCastGet(RefCount::Referenced*,refcp,p)
605  if ( refcp )
606  refPtr( refcp );
607  else if ( p )
608  p->ref();
609 }
610 
611 
612 template <class T> inline
614 {
615  mDynamicCastGet(RefCount::Referenced*,refcp,p)
616  if ( refcp )
617  unRefPtr( refcp );
618  else if ( p )
619  p->unRef();
620 }
621 
622 
623 template <class T> inline
625 {
626  mDynamicCastGet(RefCount::Referenced*,refcp,p)
627  if ( refcp )
628  unRefNoDeletePtr( refcp );
629  else if ( p )
630  p->unRefNoDelete();
631 }
Threads::AtomicPointer
Atomic instantiated with a pointer. The class really only handles the casting from a void* to a T*.
Definition: atomic.h:70
ConstRefMan
Definition: ptrman.h:233
ConstPtrManBase
Definition: ptrman.h:80
ConstArrPtrMan::ConstArrPtrMan
ConstArrPtrMan(const T *=nullptr)
Definition: ptrman.h:428
ConstPtrMan::operator=
ConstPtrMan< T > & operator=(const T *p)
Definition: ptrman.h:353
ConstPtrMan::deleteFunc
static void deleteFunc(T *p)
Definition: ptrman.h:153
ConstArrPtrMan::operator=
ConstArrPtrMan< T > & operator=(const ConstArrPtrMan< T > &)=delete
ArrPtrMan::setSize
void setSize(int64_t size)
Definition: ptrman.h:174
ConstPtrMan
Definition: ptrman.h:141
ConstRefMan::operator=
ConstRefMan< T > & operator=(const RefMan< TT > &)
Definition: ptrman.h:587
createSingleObject
T * createSingleObject()
Definition: ptrman.h:35
NonConstPtrManBase::operator*
const T & operator*() const
Definition: ptrman.h:103
PtrManBase::~PtrManBase
virtual ~PtrManBase()
Definition: ptrman.h:64
ConstArrPtrMan::deleteFunc
static void deleteFunc(T *p)
Definition: ptrman.h:199
ConstRefMan::ConstRefMan
ConstRefMan(const ConstRefMan< TT > &)
Definition: ptrman.h:548
PtrMan::operator=
PtrMan< T > & operator=(T *p)
Definition: ptrman.h:333
ArrPtrMan::deleteFunc
static void deleteFunc(T *p)
Definition: ptrman.h:181
RefMan::operator=
RefMan< T > & operator=(const WeakPtr< T > &)
Definition: ptrman.h:477
ConstArrPtrMan::operator=
ConstArrPtrMan< T > & operator=(const T *p)
Definition: ptrman.h:434
NonConstPtrManBase::NonConstPtrManBase
NonConstPtrManBase(PtrFunc setfunc, PtrFunc deletor, T *p)
Definition: ptrman.h:112
od_int64
#define od_int64
Definition: plftypes.h:35
ConstRefMan::setNoDelete
void setNoDelete(bool yn)
Definition: ptrman.h:595
ConstPtrMan::operator=
ConstPtrMan< T > & operator=(const ConstPtrMan< T > &)=delete
mDynamicCastGet
#define mDynamicCastGet(typ, out, in)
Definition: commondefs.h:148
PtrMan::PtrMan
PtrMan(const PtrMan< T > &)=delete
ConstPtrManBase::operator->
const T * operator->() const
Definition: ptrman.h:84
ConstRefMan::ConstRefMan
ConstRefMan(const RefMan< TT > &)
Definition: ptrman.h:555
ConstPtrManBase::operator*
const T & operator*() const
Definition: ptrman.h:85
ArrPtrMan::operator=
ArrPtrMan< T > & operator=(T *p)
Definition: ptrman.h:373
createObjectArray
T * createObjectArray(int64_t sz)
Definition: ptrman.h:36
RefMan::unRefNoDelete
static void unRefNoDelete(T *p)
Definition: ptrman.h:523
RefMan::setNoDelete
void setNoDelete(bool yn)
Definition: ptrman.h:494
deleteAndZeroPtr
void deleteAndZeroPtr(T *&ptr, bool isowner=true)
Definition: ptrman.h:27
ArrPtrMan::ArrPtrMan
ArrPtrMan(const ArrPtrMan< T > &)=delete
RefMan::unRef
static void unRef(T *p)
Definition: ptrman.h:512
PtrManBase::ptr_
Threads::AtomicPointer< T > ptr_
Definition: ptrman.h:66
RefMan::operator=
RefMan< T > & operator=(const RefMan< T > &)
Definition: ptrman.h:469
ConstRefMan::operator=
ConstRefMan< T > & operator=(const T *p)
Definition: ptrman.h:578
ConstRefMan::ConstRefMan
ConstRefMan(const T *=nullptr)
Definition: ptrman.h:541
ArrPtrMan::ArrPtrMan
ArrPtrMan(ArrPtrMan< T > &&)
Definition: ptrman.h:367
ConstRefMan::unRefNoDelete
static void unRefNoDelete(T *p)
Definition: ptrman.h:624
PtrManBase::erase
void erase()
Definition: ptrman.h:50
ConstPtrMan::ConstPtrMan
ConstPtrMan(const T *=nullptr)
Definition: ptrman.h:341
PtrManBase::set
T * set(T *p, bool doerase=true)
Returns old pointer if not erased.
Definition: ptrman.h:269
PtrManBase
Definition: ptrman.h:43
mClass
#define mClass(module)
Definition: commondefs.h:181
mDeprecated
#define mDeprecated(msg)
Definition: plfdefs.h:215
deleteAndZeroArrPtr
void deleteAndZeroArrPtr(T *&ptr, bool isowner=true)
Definition: ptrman.h:32
ArrPtrMan::operator=
ArrPtrMan< T > & operator=(const ArrPtrMan< T > &)=delete
PtrManBase::setfunc_
PtrFunc setfunc_
Definition: ptrman.h:68
gendefs.h
RefMan::ref
static void ref(T *p)
Definition: ptrman.h:501
RefMan::RefMan
RefMan(T *=nullptr)
Definition: ptrman.h:463
PtrMan
Definition: ptrman.h:121
ArrPtrMan
Definition: ptrman.h:160
NonConstPtrManBase::ptr
const T * ptr() const
Definition: ptrman.h:100
ConstRefMan::operator=
ConstRefMan< T > & operator=(const ConstRefMan< T > &)
Definition: ptrman.h:570
RefMan::RefMan
RefMan(const RefMan< TT > &)
Definition: ptrman.h:457
PtrMan::operator=
PtrMan< T > & operator=(const PtrMan< T > &)=delete
ConstRefMan::ConstRefMan
ConstRefMan(const ConstRefMan< T > &)
Definition: ptrman.h:535
Conv::set
void set(T &_to, const F &fr)
template based type conversion
Definition: convert.h:27
PtrManBase::deletefunc_
PtrFunc deletefunc_
Definition: ptrman.h:69
operator*
Coord3 operator*(double f, const Coord3 &b)
Definition: coord.h:129
PtrMan::deleteFunc
static void deleteFunc(T *p)
Definition: ptrman.h:133
RefMan::operator=
RefMan< T > & operator=(T *p)
Definition: ptrman.h:213
NonConstPtrManBase::getNonConstPtr
T * getNonConstPtr() const
Definition: ptrman.h:108
RefMan::operator=
RefMan< T > & operator=(const RefMan< TT > &)
Definition: ptrman.h:485
ArrPtrMan::size_
int64_t size_
Definition: ptrman.h:183
WeakPtr
Definition: ptrman.h:22
NonConstPtrManBase::ptr
T * ptr()
Definition: ptrman.h:104
PtrManBase::createIfNull
T * createIfNull(PointerCreator=createSingleObject< T >)
Definition: ptrman.h:306
ConstArrPtrMan
Definition: ptrman.h:190
ArrPtrMan::ArrPtrMan
ArrPtrMan(T *=nullptr)
Definition: ptrman.h:361
atomic.h
PtrMan::PtrMan
PtrMan(PtrMan< T > &&)
Definition: ptrman.h:327
refcount.h
NonConstPtrManBase
Definition: ptrman.h:98
PtrManBase::setIfNull
bool setIfNull(T *p, bool takeover)
Definition: ptrman.h:286
PtrManBase::PtrManBase
PtrManBase(PtrFunc setfunc, PtrFunc deletor, T *)
Definition: ptrman.h:260
ConstPtrManBase::ptr
const T * ptr() const
Definition: ptrman.h:82
ConstRefMan::operator=
ConstRefMan< T > & operator=(const ConstRefMan< TT > &)
Definition: ptrman.h:562
DBG::forceCrash
void forceCrash(bool withdump)
ConstPtrManBase::getNonConstPtr
T * getNonConstPtr() const
Definition: ptrman.h:86
ConstPtrMan::ConstPtrMan
ConstPtrMan(const ConstPtrMan< T > &)=delete
RefMan::RefMan
RefMan(const WeakPtr< T > &)
Definition: ptrman.h:448
RefMan::RefMan
RefMan(const RefMan< T > &)
Definition: ptrman.h:442
ConstRefMan::unRef
static void unRef(T *p)
Definition: ptrman.h:613
ConstArrPtrMan::ConstArrPtrMan
ConstArrPtrMan(const ConstArrPtrMan< T > &)=delete
PtrMan::PtrMan
PtrMan(T *=nullptr)
Definition: ptrman.h:321
PtrManBase::release
T * release()
Returns pointer. I won't take care of it any longer.
Definition: ptrman.h:48
debug.h
ConstPtrMan::ConstPtrMan
ConstPtrMan(ConstPtrMan< T > &&)
Definition: ptrman.h:347
ConstRefMan::ref
static void ref(T *p)
Definition: ptrman.h:602
NonConstPtrManBase::operator->
const T * operator->() const
Definition: ptrman.h:102
RefMan
Definition: ptrman.h:206
ConstPtrManBase::ConstPtrManBase
ConstPtrManBase(PtrFunc setfunc, PtrFunc deletor, T *p)
Definition: ptrman.h:90

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