Logo ROOT   6.16/01
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
15A reference counting-managed pointer for classes derived from TGeoExtension
16which can be used as C pointer. Based on CodeProject implementation example
17
18### Example:
19
20~~~ {.cpp}
21class MyExtension : public TGeoExtension {
22public:
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);}
29private:
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
57Event if one forgets to call ptr2 = 0, the object gets delete when the method
58using ptr2 gets out of scope.
59*/
60
61template<class T>
63{
64public:
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
114private:
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:63
TGeoRCPtr & operator=(T *ptr)
Definition: TGeoRCPtr.h:85
~TGeoRCPtr()
Definition: TGeoRCPtr.h:79
T * Get() const
Definition: TGeoRCPtr.h:100
TGeoRCPtr & operator=(const TGeoRCPtr &ptr)
Definition: TGeoRCPtr.h:94
bool operator==(const T *ptr)
Definition: TGeoRCPtr.h:112
TGeoRCPtr(const TGeoRCPtr &ptr)
Definition: TGeoRCPtr.h:73
T & operator*() const
Definition: TGeoRCPtr.h:108
T * operator->() const
Definition: TGeoRCPtr.h:107
TGeoRCPtr(T *ptr=0)
Definition: TGeoRCPtr.h:66
T * fPtr
Definition: TGeoRCPtr.h:115
bool operator==(const TGeoRCPtr &ptr)
Definition: TGeoRCPtr.h:111
double T(double x)
Definition: ChebyshevPol.h:34