ROOT  6.06/09
Reference Guide
TGeoRCPtr.h
Go to the documentation of this file.
1 /*************************************************************************
2  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
3  * All rights reserved. *
4  * *
5  * For the licensing terms see $ROOTSYS/LICENSE. *
6  * For the list of contributors see $ROOTSYS/README/CREDITS. *
7  *************************************************************************/
8 
9 // Author: Andrei.Gheata@cern.ch 29/05/2013
10 
11 //______________________________________________________________________________
12 // TGeoRCPtr - A reference counting-managed pointer for classes derived from
13 // TGeoExtension which can be used as C pointer. Based on
14 // CodeProject implementation example
15 //______________________________________________________________________________
16 
17 
18 
19 /*______________________________________________________________________________
20 Example:
21 =======
22 class MyExtension : public TGeoExtension {
23 public:
24  MyExtension() : TGeoExtension(), fRC(0) {printf("Created MyExtension\n");}
25  virtual ~MyExtension() {printf("Deleted MyExtension\n");}
26 
27  virtual TGeoExtension *Grab() const {fRC++; return (TGeoExtension*)this;}
28  virtual void Release() const {assert(fRC > 0); fRC--; if (fRC ==0) delete this;}
29  void print() const {printf("MyExtension object %p\n", this);}
30 private:
31  mutable Int_t fRC; // Reference counter
32  ClassDef(MyExtension,1)
33 };
34 
35 
36 Usage:
37 ======
38  // Module 1 creates an object
39  TGeoRCPtr<MyExtension> a2 = new MyExtension(); //fRC=1
40 
41  // Module 2 grabs object
42  TGeoRCPtr<MyExtension> ptr2 = a2; //fRC=2
43 
44  // Module 2 invokes a method
45  ptr2->Print();
46  (*ptr2).Print();
47 
48  // Module 1 no longer needs object
49  a2 = 0; //RC=1
50 
51  // Module 2 no longer needs object
52  ptr2 = 0; //object will be destroyed here
53 
54 Note:
55 =====
56  Event if one forgets to call ptr2 = 0, the object gets delete when the method
57  using ptr2 gets out of scope.
58 ______________________________________________________________________________*/
59 
60 template<class T>
61 class TGeoRCPtr
62 {
63 public:
64  //Construct using a C pointer, e.g. TGeoRCPtr<T> x = new T();
65  TGeoRCPtr(T* ptr = 0)
66  : fPtr(ptr)
67  {
68  if(ptr != 0) ptr->Grab();
69  }
70 
71  //Copy constructor
72  TGeoRCPtr(const TGeoRCPtr &ptr)
73  : fPtr(ptr.fPtr)
74  {
75  if(fPtr != 0) fPtr->Grab();
76  }
77 
79  {
80  if(fPtr != 0) fPtr->Release();
81  }
82 
83  //Assign a pointer, e.g. x = new T();
85  {
86  if(ptr != 0) ptr->Grab();
87  if(fPtr != 0) fPtr->Release();
88  fPtr = ptr;
89  return (*this);
90  }
91 
92  //Assign another TGeoRCPtr
94  {
95  return (*this) = ptr.fPtr;
96  }
97 
98  //Retrieve actual pointer
99  T* Get() const
100  {
101  return fPtr;
102  }
103 
104  //Some overloaded operators to facilitate dealing with an TGeoRCPtr as a convetional C pointer.
105  //Without these operators, one can still use the less transparent Get() method to access the pointer.
106  T* operator->() const {return fPtr;} //x->member
107  T &operator*() const {return *fPtr;} //*x, (*x).member
108  operator T*() const {return fPtr;} //T* y = x;
109  operator bool() const {return fPtr != 0;} //if(x) {/*x is not NULL*/}
110  bool operator==(const TGeoRCPtr &ptr) {return fPtr == ptr.fPtr;}
111  bool operator==(const T *ptr) {return fPtr == ptr;}
112 
113 private:
114  T *fPtr; //Actual pointer
115 };
T * fPtr
Definition: TGeoRCPtr.h:114
bool operator==(const TGeoRCPtr &ptr)
Definition: TGeoRCPtr.h:110
double T(double x)
Definition: ChebyshevPol.h:34
TGeoRCPtr(T *ptr=0)
Definition: TGeoRCPtr.h:65
~TGeoRCPtr()
Definition: TGeoRCPtr.h:78
TGeoRCPtr(const TGeoRCPtr &ptr)
Definition: TGeoRCPtr.h:72
TGeoRCPtr & operator=(const TGeoRCPtr &ptr)
Definition: TGeoRCPtr.h:93
bool operator==(const T *ptr)
Definition: TGeoRCPtr.h:111
T * Get() const
Definition: TGeoRCPtr.h:99
T & operator*() const
Definition: TGeoRCPtr.h:107
TGeoRCPtr & operator=(T *ptr)
Definition: TGeoRCPtr.h:84
T * operator->() const
Definition: TGeoRCPtr.h:106