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 ClassDefOverride(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>
65class TGeoRCPtr {
66public:
67 // Construct using a C pointer, e.g. TGeoRCPtr<T> x = new T();
68 TGeoRCPtr(T *ptr = nullptr) : fPtr(ptr)
69 {
70 if (ptr)
71 ptr->Grab();
72 }
73
74 // Copy constructor
75 TGeoRCPtr(const TGeoRCPtr &ptr) : fPtr(ptr.fPtr)
76 {
77 if (fPtr)
78 fPtr->Grab();
79 }
80
82 {
83 if (fPtr)
84 fPtr->Release();
85 }
86
87 // Assign a pointer, e.g. x = new T();
89 {
90 if (ptr)
91 ptr->Grab();
92 if (fPtr)
93 fPtr->Release();
94 fPtr = ptr;
95 return (*this);
96 }
97
98 // Assign another TGeoRCPtr
99 TGeoRCPtr &operator=(const TGeoRCPtr &ptr) { return (*this) = ptr.fPtr; }
100
101 // Retrieve actual pointer
102 T *Get() const { return fPtr; }
103
104 // Some overloaded operators to facilitate dealing with an TGeoRCPtr as a conventional 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 != nullptr; } // 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
113private:
114 T *fPtr; // Actual pointer
115};
116
117#endif
A reference counting-managed pointer for classes derived from TGeoExtension which can be used as C po...
Definition TGeoRCPtr.h:65
TGeoRCPtr & operator=(T *ptr)
Definition TGeoRCPtr.h:88
T * Get() const
Definition TGeoRCPtr.h:102
TGeoRCPtr & operator=(const TGeoRCPtr &ptr)
Definition TGeoRCPtr.h:99
bool operator==(const T *ptr)
Definition TGeoRCPtr.h:111
TGeoRCPtr(const TGeoRCPtr &ptr)
Definition TGeoRCPtr.h:75
T & operator*() const
Definition TGeoRCPtr.h:107
T * operator->() const
Definition TGeoRCPtr.h:106
TGeoRCPtr(T *ptr=nullptr)
Definition TGeoRCPtr.h:68
bool operator==(const TGeoRCPtr &ptr)
Definition TGeoRCPtr.h:110