Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TGeoRCPtr< T > Class Template Reference

template<class T>
class TGeoRCPtr< T >

A reference counting-managed pointer for classes derived from TGeoExtension which can be used as C pointer.

Based on CodeProject implementation example

Example:

class MyExtension : public TGeoExtension {
public:
MyExtension() : TGeoExtension(), fRC(0) {printf("Created MyExtension\n");}
virtual ~MyExtension() {printf("Deleted MyExtension\n");}
virtual TGeoExtension *Grab() const {fRC++; return (TGeoExtension*)this;}
virtual void Release() const {assert(fRC > 0); fRC--; if (fRC ==0) delete this;}
void print() const {printf("MyExtension object %p\n", this);}
private:
mutable Int_t fRC; // Reference counter
ClassDefOverride(MyExtension,1)
};
int Int_t
Definition RtypesCore.h:45
#define ClassDefOverride(name, id)
Definition Rtypes.h:346
ABC for user objects attached to TGeoVolume or TGeoNode.

Usage:

// Module 1 creates an object
TGeoRCPtr<MyExtension> a2 = new MyExtension(); //fRC=1
// Module 2 grabs object
// Module 2 invokes a method
ptr2->Print();
(*ptr2).Print();
// Module 1 no longer needs object
a2 = 0; //RC=1
// Module 2 no longer needs object
ptr2 = 0; //object will be destroyed here
A reference counting-managed pointer for classes derived from TGeoExtension which can be used as C po...
Definition TGeoRCPtr.h:65
static byte * ptr2
Definition gifdecode.c:17

Note:

Event if one forgets to call ptr2 = 0, the object gets delete when the method using ptr2 gets out of scope.

Definition at line 65 of file TGeoRCPtr.h.

Public Member Functions

 TGeoRCPtr (const TGeoRCPtr &ptr)
 
 TGeoRCPtr (T *ptr=nullptr)
 
 ~TGeoRCPtr ()
 
T * Get () const
 
 operator bool () const
 
 operator T* () const
 
T & operator* () const
 
T * operator-> () const
 
TGeoRCPtroperator= (const TGeoRCPtr &ptr)
 
TGeoRCPtroperator= (T *ptr)
 
bool operator== (const T *ptr)
 
bool operator== (const TGeoRCPtr &ptr)
 

Private Attributes

T * fPtr
 

#include <TGeoRCPtr.h>

Constructor & Destructor Documentation

◆ TGeoRCPtr() [1/2]

template<class T >
TGeoRCPtr< T >::TGeoRCPtr ( T *  ptr = nullptr)
inline

Definition at line 68 of file TGeoRCPtr.h.

◆ TGeoRCPtr() [2/2]

template<class T >
TGeoRCPtr< T >::TGeoRCPtr ( const TGeoRCPtr< T > &  ptr)
inline

Definition at line 75 of file TGeoRCPtr.h.

◆ ~TGeoRCPtr()

template<class T >
TGeoRCPtr< T >::~TGeoRCPtr ( )
inline

Definition at line 81 of file TGeoRCPtr.h.

Member Function Documentation

◆ Get()

template<class T >
T * TGeoRCPtr< T >::Get ( ) const
inline

Definition at line 102 of file TGeoRCPtr.h.

◆ operator bool()

template<class T >
TGeoRCPtr< T >::operator bool ( ) const
inline

Definition at line 109 of file TGeoRCPtr.h.

◆ operator T*()

template<class T >
TGeoRCPtr< T >::operator T* ( ) const
inline

Definition at line 108 of file TGeoRCPtr.h.

◆ operator*()

template<class T >
T & TGeoRCPtr< T >::operator* ( ) const
inline

Definition at line 107 of file TGeoRCPtr.h.

◆ operator->()

template<class T >
T * TGeoRCPtr< T >::operator-> ( ) const
inline

Definition at line 106 of file TGeoRCPtr.h.

◆ operator=() [1/2]

template<class T >
TGeoRCPtr & TGeoRCPtr< T >::operator= ( const TGeoRCPtr< T > &  ptr)
inline

Definition at line 99 of file TGeoRCPtr.h.

◆ operator=() [2/2]

template<class T >
TGeoRCPtr & TGeoRCPtr< T >::operator= ( T *  ptr)
inline

Definition at line 88 of file TGeoRCPtr.h.

◆ operator==() [1/2]

template<class T >
bool TGeoRCPtr< T >::operator== ( const T *  ptr)
inline

Definition at line 111 of file TGeoRCPtr.h.

◆ operator==() [2/2]

template<class T >
bool TGeoRCPtr< T >::operator== ( const TGeoRCPtr< T > &  ptr)
inline

Definition at line 110 of file TGeoRCPtr.h.

Member Data Documentation

◆ fPtr

template<class T >
T* TGeoRCPtr< T >::fPtr
private

Definition at line 114 of file TGeoRCPtr.h.

  • geom/geom/inc/TGeoRCPtr.h