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