MURE
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
TReference.hxx
Go to the documentation of this file.
1 #ifndef _TREFERENCE_HXX_
2 #define _TREFERENCE_HXX_
3 
9 
17 class TReference
18 {
19  public :
21  virtual ~TReference() {}
22 
23  void AddReference(unsigned n=1) {fNbReferences+=n;}
24  unsigned DelReference(unsigned n=1) {fNbReferences-=n; return fNbReferences;}
25  unsigned Reference(){return fNbReferences;}
26  private :
27  unsigned fNbReferences;
28 
29 };
30 
32 
40 template <class T> class Reference_ptr
41 {
42  private:
43  T* ptr;
44 
45 public:
46  explicit Reference_ptr(T* p = 0) {ptr=p; if (ptr) ptr->AddReference();}
47  Reference_ptr(const Reference_ptr& r) {ptr=r.ptr;ptr->AddReference();}
48  ~Reference_ptr() {if(ptr && ptr->DelReference()==0) delete ptr;}
49  T& operator*() const {return *ptr;}
50  T* operator->() const {return ptr;}
51  T* Get() const {return ptr;}
52 
55  {
56  if (ptr != r.ptr)
57  {
58  if(ptr && ptr->DelReference()==0) delete ptr;
59  ptr=r.ptr;
60  if (ptr) ptr->AddReference();
61  }
62  return *this;
63  }
64 
67  {
68  if (ptr != p)
69  {
70  if(ptr && ptr->DelReference()==0) delete ptr;
71  ptr=p;
72  if (ptr) ptr->AddReference();
73  }
74  return *this;
75  }
76 
77  bool operator==(T *p) {return ptr==p;}
78  bool operator!=(T *p) {return ptr!=p;}
79 };
80 
81 #endif
Reference_ptr & operator=(T *p)
Affectation ; destroy old ptr if is no more referenced.
Definition: TReference.hxx:66
Handle dynamical object creation and pointer affectation.
Definition: TReference.hxx:40
T & operator*() const
return the object of the template class
Definition: TReference.hxx:49
bool operator!=(T *p)
Comparaison of Pointer.
Definition: TReference.hxx:78
bool operator==(T *p)
Comparaison of Pointer.
Definition: TReference.hxx:77
unsigned DelReference(unsigned n=1)
Remove n references to an object.
Definition: TReference.hxx:24
unsigned fNbReferences
Number of references to an object.
Definition: TReference.hxx:27
T * ptr
Pointer to the template class T.
Definition: TReference.hxx:43
T * operator->() const
return a pointer to the template class
Definition: TReference.hxx:50
TReference()
Default constructor. Number of references is set to 0.
Definition: TReference.hxx:20
T * Get() const
return a pointer to the template class (usefull for cast)
Definition: TReference.hxx:51
virtual ~TReference()
Destructor.
Definition: TReference.hxx:21
Reference_ptr & operator=(const Reference_ptr &r)
Affectation ; destroy old ptr if is no more referenced.
Definition: TReference.hxx:54
void AddReference(unsigned n=1)
Add n references to an object.
Definition: TReference.hxx:23
Reference_ptr(const Reference_ptr &r)
Copy constructor.
Definition: TReference.hxx:47
unsigned Reference()
returns the Number of references to an object
Definition: TReference.hxx:25
Base class to reference all Shape objects.
Definition: TReference.hxx:17
~Reference_ptr()
Normal destructor.
Definition: TReference.hxx:48
Reference_ptr(T *p=0)
Normal constructor.
Definition: TReference.hxx:46

MURE Project, documentation generated by Doxygen 1.8.5 - Mon Nov 17 2014