OpendTect  6.3
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 ________________________________________________________________________
10 
11 -*/
12 
13 #include "gendefs.h"
14 #include "atomic.h"
15 #include "refcount.h"
16 #include <stdlib.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 = 0; }
29 
30 
31 template <class T>
32 void deleteAndZeroArrPtr( T*& ptr, bool isowner=true )
33 { if ( isowner ) delete [] ptr; ptr = 0; }
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 
72 
73 template<class T>
74 mClass(Basic) ConstPtrManBase : public PtrManBase<T>
75 {
76 public:
77  inline const T* ptr() const { return this->ptr_; }
78  inline operator const T*() const { return this->ptr_; }
79  inline const T* operator->() const { return this->ptr_; }
80  inline const T& operator*() const { return *this->ptr_; }
81  inline T* getNonConstPtr() const
82  { return const_cast<T*>(this->ptr()); }
83 protected:
84  typedef void (*PtrFunc)(T*);
85  inline ConstPtrManBase(PtrFunc setfunc,PtrFunc deletor,T* p)
86  : PtrManBase<T>( setfunc, deletor, p )
87  {}
88 };
89 
90 
91 template<class T>
93 {
94 public:
95  inline const T* ptr() const { return this->ptr_; }
96  inline operator const T*() const { return this->ptr_; }
97  inline const T* operator->() const { return this->ptr_; }
98  inline const T& operator*() const { return *this->ptr_; }
99  inline T* ptr() { return this->ptr_; }
100  inline operator T*() { return this->ptr_; }
101  inline T* operator ->() { return this->ptr_; }
102  inline T& operator *() { return *this->ptr_; }
103  inline T* getNonConstPtr() const
104  { return const_cast<T*>(this->ptr()); }
105 protected:
106  typedef void (*PtrFunc)(T*);
107  inline NonConstPtrManBase(PtrFunc setfunc,PtrFunc deletor,T* p)
108  : PtrManBase<T>( setfunc, deletor, p )
109  {}
110 };
111 
112 
114 template <class T>
115 mClass(Basic) PtrMan : public NonConstPtrManBase<T>
116 {
117 public:
118  inline PtrMan(T* = 0);
119  inline PtrMan(PtrMan<T>&&);
120  PtrMan<T>& operator=( T* p );
121 
122  PtrMan(const PtrMan<T>&) = delete;
123  PtrMan<T>& operator=(const PtrMan<T>&) = delete;
124 
125 
126 private:
127 
128  static void deleteFunc( T* p ) { delete p; }
129 
130 };
131 
132 
134 template <class T>
136 {
137 public:
138 
139  inline ConstPtrMan(const T* = 0);
140  inline ConstPtrMan(ConstPtrMan<T>&&);
141  ConstPtrMan<T>& operator=(const T* p);
142 
143  ConstPtrMan(const ConstPtrMan<T>&) = delete;
144  ConstPtrMan<T>& operator=(const ConstPtrMan<T>&) = delete;
145 
146 private:
147 
148  static void deleteFunc( T* p ) { delete p; }
149 };
150 
151 
153 template <class T>
155 {
156 public:
157  inline ArrPtrMan(T* = 0);
158  inline ArrPtrMan(ArrPtrMan<T>&&);
159  ArrPtrMan<T>& operator=( T* p );
160 
161 
162 #ifdef __debug__
163  T& operator[](int);
164  const T& operator[](int) const;
165  T& operator[](od_int64);
166  const T& operator[](od_int64) const;
167 
168 #endif
169  void setSize(od_int64 size) { size_=size; }
170 
171  ArrPtrMan(const ArrPtrMan<T>&) = delete;
172  inline ArrPtrMan<T>& operator=(const ArrPtrMan<T>&) = delete;
173 
174 private:
175 
176  static void deleteFunc( T* p ) { delete [] p; }
177 
179 };
180 
181 
183 template <class T>
185 {
186 public:
187  inline ConstArrPtrMan(const T* = 0);
188  ConstArrPtrMan<T>& operator=(const T* p);
189 
190  ConstArrPtrMan<T>& operator=(const ConstArrPtrMan<T>&) = delete;
191  ConstArrPtrMan(const ConstArrPtrMan<T>&) = delete;
192 private:
193 
194  static void deleteFunc( T* p ) { delete p; }
195 };
196 
197 
199 template <class T>
200 mClass(Basic) RefMan : public NonConstPtrManBase<T>
201 {
202 public:
203 
204  template <class TT> inline RefMan(const RefMan<TT>&);
205  inline RefMan(const RefMan<T>&);
206  inline RefMan(const WeakPtr<T>&);
207  inline RefMan(T* = 0);
208  inline RefMan<T>& operator=( T* p )
209  { this->set( p, true ); return *this; }
210  template <class TT>
211  inline RefMan<T>& operator=(const RefMan<TT>&);
212  inline RefMan<T>& operator=(const RefMan<T>&);
213  inline RefMan<T>& operator=(const WeakPtr<T>&);
214 
215  void setNoDelete(bool yn);
216 
217 private:
218 
219  static void ref(T* p);
220  static void unRef(T* p);
221  static void unRefNoDelete(T* p);
222 };
223 
224 
226 template <class T>
228 {
229 public:
230  inline ConstRefMan(const ConstRefMan<T>&);
231  template <class TT> inline ConstRefMan(const ConstRefMan<TT>&);
232  template <class TT> inline ConstRefMan(const RefMan<TT>&);
233 
234  inline ConstRefMan(const T* = 0);
235  ConstRefMan<T>& operator=(const T* p);
236  template <class TT>
237  ConstRefMan<T>& operator=(const RefMan<TT>&);
238  template <class TT>
239  ConstRefMan<T>& operator=(const ConstRefMan<TT>&);
240  ConstRefMan<T>& operator=(const ConstRefMan<T>&);
241 
242 
243  void setNoDelete(bool yn);
244 
245 private:
246  static void ref(T* p);
247  static void unRef(T* p);
248  static void unRefNoDelete(T* p);
249 
250 };
251 
252 //Implementations below
253 
254 template <class T> inline
255 PtrManBase<T>::PtrManBase( PtrFunc setfunc, PtrFunc deletor, T* p )
256  : deletefunc_( deletor )
257  , setfunc_( setfunc )
258 {
259  this->set(p);
260 }
261 
262 
263 template <class T> inline
264 T* PtrManBase<T>::set( T* p, bool doerase )
265 {
266  if ( setfunc_ && p )
267  setfunc_(p);
268 
269  T* oldptr = ptr_.exchange(p);
270  if ( doerase )
271  {
272  deletefunc_( oldptr );
273  return 0;
274  }
275 
276  return oldptr;
277 }
278 
279 
280 template <class T> inline
281 bool PtrManBase<T>::setIfNull( T* p, bool takeover )
282 {
283  if ( ptr_.setIfEqual( 0, p ) )
284  {
285  if ( setfunc_ && p )
286  setfunc_(p);
287  return true;
288  }
289 
290  if ( takeover && p )
291  {
292  if ( setfunc_ ) setfunc_(p);
293  if ( deletefunc_ ) deletefunc_(p);
294  }
295 
296  return false;
297 }
298 
299 
300 template <class T> inline
302 {
303  if ( ptr_ )
304  return ptr_;
305 
306  T* newptr = creator();
307  if ( !newptr )
308  return 0;
309 
310  setIfNull(newptr,true);
311 
312  return ptr_;
313 }
314 
315 template <class T> inline
317  : NonConstPtrManBase<T>( 0, deleteFunc, p )
318 {}
319 
320 
321 template <class T> inline
323  : PtrMan<T>( p.release() )
324 {}
325 
326 
327 template <class T> inline
329 {
330  this->set( p );
331  return *this;
332 }
333 
334 
335 template <class T> inline
337  : ConstPtrManBase<T>( 0, deleteFunc, const_cast<T*>(p) )
338 {}
339 
340 
341 template <class T> inline
343  : ConstPtrMan<T>( p.release() )
344 {}
345 
346 
347 template <class T> inline
349 {
350  this->set( const_cast<T*>( p ) );
351  return *this;
352 }
353 
354 
355 template <class T> inline
357  : NonConstPtrManBase<T>( 0, deleteFunc, p )
358  , size_(-1)
359 {}
360 
361 template <class T> inline
363  : ArrPtrMan<T>( p.release() )
364 {}
365 
366 
367 template <class T> inline
369 {
370  this->set( p );
371  return *this;
372 }
373 
374 #ifdef __debug__
375 
376 template <class T> inline
377 T& ArrPtrMan<T>::operator[]( int idx )
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
388 const T& ArrPtrMan<T>::operator[]( int idx ) const
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
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 
409 template <class T> inline
410 const T& ArrPtrMan<T>::operator[]( od_int64 idx ) const
411 {
412  if ( idx<0 || (size_>=0 && idx>=size_) )
413  {
414  DBG::forceCrash(true);
415  }
416  return this->ptr_[(size_t) idx];
417 }
418 
419 #endif
420 
421 
422 template <class T> inline
424  : ConstPtrManBase<T>( 0, deleteFunc, const_cast<T*>(p) )
425 {}
426 
427 
428 template <class T> inline
430 {
431  this->set( const_cast<T*>(p) );
432  return *this;
433 }
434 
435 
436 template <class T> inline
438  : NonConstPtrManBase<T>( ref, unRef, const_cast<T*>(p.ptr()) )
439 {}
440 
441 
442 template <class T> inline
444  : NonConstPtrManBase<T>( ref, unRef, 0 )
445 {
446  *this = p;
447 }
448 
449 
450 template <class T>
451 template <class TT> inline
453  : NonConstPtrManBase<T>( ref, unRef, (T*) p.ptr() )
454 {}
455 
456 
457 template <class T> inline
459  : NonConstPtrManBase<T>( ref, unRef, p )
460 {}
461 
462 
463 template <class T> inline
465 {
466  this->set( const_cast<T*>(p.ptr()) );
467  return *this;
468 }
469 
470 
471 template <class T> inline
473 {
474  return RefMan<T>::operator=( p.get() );
475 }
476 
477 
478 template <class T>
479 template <class TT> inline
481 {
482  this->set( (T*) p.ptr() );
483  return *this;
484 }
485 
486 
487 
488 template <class T> inline
489 void RefMan<T>::setNoDelete( bool yn )
490 {
491  this->deletefunc_ = yn ? unRefNoDelete : unRef;
492 }
493 
494 
495 template <class T> inline
496 void RefMan<T>::ref(T* p) { refPtr((RefCount::Referenced*) p ); }
497 
498 
499 template <class T> inline
500 void RefMan<T>::unRef(T* p) { unRefPtr((RefCount::Referenced*) p); }
501 
502 
503 template <class T> inline
505 { unRefNoDeletePtr((RefCount::Referenced*) p ); }
506 
507 
508 
509 template <class T> inline
511  : ConstPtrManBase<T>( ref, unRef, const_cast<T*>(p.ptr()) )
512 {}
513 
514 
515 template <class T> inline
517  : ConstPtrManBase<T>( ref, unRef, const_cast<T*>(p) )
518 {}
519 
520 
521 template <class T>
522 template <class TT> inline
524  : ConstPtrManBase<T>( ref, unRef, (T*) p.ptr() )
525 {}
526 
527 
528 template <class T>
529 template <class TT> inline
531  : ConstPtrManBase<T>( ref, unRef, (T*) p.ptr() )
532 {}
533 
534 
535 template <class T>
536 template <class TT> inline
538 {
539  this->set( (T*) p.ptr() );
540  return *this;
541 }
542 
543 
544 template <class T> inline
546 {
547  this->set( (T*) p.ptr() );
548  return *this;
549 }
550 
551 
552 template <class T> inline
554 {
555  this->set( const_cast<T*>( p ) );
556  return *this;
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->deletefunc_ = yn ? unRefNoDelete : unRef;
573 }
574 
575 
576 template <class T> inline
577 void ConstRefMan<T>::ref(T* p) { refPtr((RefCount::Referenced*) p ); }
578 
579 
580 template <class T> inline
581 void ConstRefMan<T>::unRef(T* p) { unRefPtr((RefCount::Referenced*) p); }
582 
583 
584 template <class T> inline
586 { unRefNoDeletePtr((RefCount::Referenced*) p ); }
PtrMan(T *=0)
Definition: ptrman.h:316
const T * operator->() const
Definition: ptrman.h:97
ConstPtrMan(const T *=0)
Definition: ptrman.h:336
T * ptr()
Definition: ptrman.h:99
NonConstPtrManBase(PtrFunc setfunc, PtrFunc deletor, T *p)
Definition: ptrman.h:107
static void deleteFunc(T *p)
Definition: ptrman.h:176
static void unRefNoDelete(T *p)
Definition: ptrman.h:504
void setNoDelete(bool yn)
Definition: ptrman.h:489
Definition: ptrman.h:74
#define od_int64
Definition: plftypes.h:34
static void unRef(T *p)
Definition: ptrman.h:581
PtrManBase(PtrFunc setfunc, PtrFunc deletor, T *)
Definition: ptrman.h:255
static void ref(T *p)
Definition: ptrman.h:577
ConstArrPtrMan< T > & operator=(const T *p)
Definition: ptrman.h:429
ArrPtrMan< T > & operator=(T *p)
Definition: ptrman.h:368
int64_t size_
Definition: ptrman.h:178
ConstPtrMan< T > & operator=(const T *p)
Definition: ptrman.h:348
virtual ~PtrManBase()
Definition: ptrman.h:64
T *(* PointerCreator)()
Definition: ptrman.h:56
Definition: ptrman.h:42
void deleteAndZeroArrPtr(T *&ptr, bool isowner=true)
Definition: ptrman.h:32
void setNoDelete(bool yn)
Definition: ptrman.h:570
const T * ptr() const
Definition: ptrman.h:77
Definition: ptrman.h:115
static void deleteFunc(T *p)
Definition: ptrman.h:128
PtrFunc deletefunc_
Definition: ptrman.h:69
bool setIfNull(T *p, bool takeover)
Definition: ptrman.h:281
ConstPtrManBase(PtrFunc setfunc, PtrFunc deletor, T *p)
Definition: ptrman.h:85
static void unRef(T *p)
Definition: ptrman.h:500
Definition: ptrman.h:184
T * getNonConstPtr() const
Definition: ptrman.h:81
T * createIfNull(PointerCreator=createSingleObject< T >)
Definition: ptrman.h:301
ConstRefMan< T > & operator=(const T *p)
Definition: ptrman.h:553
Definition: ptrman.h:92
static void unRefNoDelete(T *p)
Definition: ptrman.h:585
ConstArrPtrMan(const T *=0)
Definition: ptrman.h:423
ArrPtrMan(T *=0)
Definition: ptrman.h:356
Atomic instantiated with a pointer. The class really only handles the casting from a void* to a T*...
Definition: atomic.h:92
Definition: ptrman.h:154
void setSize(int64_t size)
Definition: ptrman.h:169
T * createSingleObject()
Definition: ptrman.h:35
static void ref(T *p)
Definition: ptrman.h:496
void erase()
Definition: ptrman.h:50
void forceCrash(bool withdump)
const T * operator->() const
Definition: ptrman.h:79
static void deleteFunc(T *p)
Definition: ptrman.h:148
void deleteAndZeroPtr(T *&ptr, bool isowner=true)
Definition: ptrman.h:27
RefMan(const RefMan< TT > &)
Definition: ptrman.h:452
RefMan< T > & operator=(T *p)
Definition: ptrman.h:208
T * release()
Returns pointer. I won&#39;t take care of it any longer.
Definition: ptrman.h:48
Definition: ptrman.h:22
T * set(T *p, bool doerase=true)
Returns old pointer if not erased.
Definition: ptrman.h:264
Point2D< T > operator*(int f, const Point2D< T > &b)
Definition: geometry.h:86
ConstRefMan(const ConstRefMan< T > &)
Definition: ptrman.h:510
#define mClass(module)
Definition: commondefs.h:161
const T & operator*() const
Definition: ptrman.h:80
T * createObjectArray(int64_t sz)
Definition: ptrman.h:36
Threads::AtomicPointer< T > ptr_
Definition: ptrman.h:66
const T & operator*() const
Definition: ptrman.h:98
Definition: ptrman.h:227
const T * ptr() const
Definition: ptrman.h:95
Definition: ptrman.h:200
PtrFunc setfunc_
Definition: ptrman.h:68
Definition: ptrman.h:135
static void deleteFunc(T *p)
Definition: ptrman.h:194
T * getNonConstPtr() const
Definition: ptrman.h:103
PtrMan< T > & operator=(T *p)
Definition: ptrman.h:328

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