Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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#ifndef ROOT_TGeoRCPtr
13#define ROOT_TGeoRCPtr
14
15/** \class TGeoRCPtr
16\ingroup Geometry_classes
17
18A reference counting-managed pointer for classes derived from TGeoExtension
19which can be used as C pointer. Based on CodeProject implementation example
20
21### Example:
22
23~~~ {.cpp}
24class MyExtension : public TGeoExtension {
25public:
26 MyExtension() : TGeoExtension(), fRC(0) {printf("Created MyExtension\n");}
27 virtual ~MyExtension() {printf("Deleted MyExtension\n");}
28
29 virtual TGeoExtension *Grab() const {fRC++; return (TGeoExtension*)this;}
30 virtual void Release() const {assert(fRC > 0); fRC--; if (fRC ==0) delete this;}
31 void print() const {printf("MyExtension object %p\n", this);}
32private:
33 mutable Int_t fRC; // Reference counter
34 ClassDef(MyExtension,1)
35};
36~~~
37
38### Usage:
39
40~~~ {.cpp}
41 // Module 1 creates an object
42 TGeoRCPtr<MyExtension> a2 = new MyExtension(); //fRC=1
43
44 // Module 2 grabs object
45 TGeoRCPtr<MyExtension> ptr2 = a2; //fRC=2
46
47 // Module 2 invokes a method
48 ptr2->Print();
49 (*ptr2).Print();
50
51 // Module 1 no longer needs object
52 a2 = 0; //RC=1
53
54 // Module 2 no longer needs object
55 ptr2 = 0; //object will be destroyed here
56~~~
57
58### Note:
59
60Event if one forgets to call ptr2 = 0, the object gets delete when the method
61using ptr2 gets out of scope.
62*/
63
64template<class T>
66{
67public:
68 //Construct using a C pointer, e.g. TGeoRCPtr<T> x = new T();
69 TGeoRCPtr(T* ptr = 0)
70 : fPtr(ptr)
71 {
72 if(ptr != 0) ptr->Grab();
73 }
74
75 //Copy constructor
76 TGeoRCPtr(const TGeoRCPtr &ptr)
77 : fPtr(ptr.fPtr)
78 {
79 if(fPtr != 0) fPtr->Grab();
80 }
81
83 {
84 if(fPtr != 0) fPtr->Release();
85 }
86
87 //Assign a pointer, e.g. x = new T();
89 {
90 if(ptr != 0) ptr->Grab();
91 if(fPtr != 0) fPtr->Release();
92 fPtr = ptr;
93 return (*this);
94 }
95
96 //Assign another TGeoRCPtr
98 {
99 return (*this) = ptr.fPtr;
100 }
101
102 //Retrieve actual pointer
103 T* Get() const
104 {
105 return fPtr;
106 }
107
108 //Some overloaded operators to facilitate dealing with an TGeoRCPtr as a conventional C pointer.
109 //Without these operators, one can still use the less transparent Get() method to access the pointer.
110 T* operator->() const {return fPtr;} //x->member
111 T &operator*() const {return *fPtr;} //*x, (*x).member
112 operator T*() const {return fPtr;} //T* y = x;
113 operator bool() const {return fPtr != 0;} //if(x) {/*x is not NULL*/}
114 bool operator==(const TGeoRCPtr &ptr) {return fPtr == ptr.fPtr;}
115 bool operator==(const T *ptr) {return fPtr == ptr;}
116
117private:
118 T *fPtr; //Actual pointer
119};
120
121#endif
A reference counting-managed pointer for classes derived from TGeoExtension which can be used as C po...
Definition TGeoRCPtr.h:66
TGeoRCPtr & operator=(T *ptr)
Definition TGeoRCPtr.h:88
T * Get() const
Definition TGeoRCPtr.h:103
TGeoRCPtr & operator=(const TGeoRCPtr &ptr)
Definition TGeoRCPtr.h:97
bool operator==(const T *ptr)
Definition TGeoRCPtr.h:115
TGeoRCPtr(const TGeoRCPtr &ptr)
Definition TGeoRCPtr.h:76
T & operator*() const
Definition TGeoRCPtr.h:111
T * operator->() const
Definition TGeoRCPtr.h:110
TGeoRCPtr(T *ptr=0)
Definition TGeoRCPtr.h:69
bool operator==(const TGeoRCPtr &ptr)
Definition TGeoRCPtr.h:114