MURE
Loading...
Searching...
No Matches
TReference.hxx
Go to the documentation of this file.
1/*
2 This file is part of MURE,
3 Copyright (C) 2007-2021 MURE developers.
4
5 MURE is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 MURE is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with MURE. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19#ifndef _TREFERENCE_HXX_
20#define _TREFERENCE_HXX_
21
28
38{
39 public :
41 {
42 fNbReferences = 0;
43 }
44 virtual ~TReference() = default;
45
46 void AddReference(unsigned n = 1)
47 {
48 fNbReferences += n;
49 }
50 unsigned DelReference(unsigned n = 1)
51 {
52 fNbReferences -= n;
53 return fNbReferences;
54 }
55 unsigned Reference()
56 {
57 return fNbReferences;
58 }
59 private :
60 unsigned fNbReferences;
61
62};
63
65
73template < class T > class Reference_ptr
74{
75 private:
76 T *ptr;
77
78 public:
79 explicit Reference_ptr(T *p = nullptr)
80 {
81 ptr = p;
82 if (ptr) ptr -> AddReference();
83 }
85 {
86 ptr = r.ptr;
87 ptr -> AddReference();
88 }
90 {
91 if(ptr && ptr -> DelReference() == 0) delete ptr;
92 }
93 //void Delete_ptr() {if(ptr) {cout<<" TRef delete: "<<ptr<<endl; delete ptr;}} //!< Custom destructor
94 T &operator* () const
95 {
96 return *ptr;
97 }
98 T *operator-> () const
99 {
100 return ptr;
101 }
102 T *Get() const
103 {
104 return ptr;
105 }
106
109 {
110 if (ptr != r.ptr)
111 {
112 if(ptr && ptr -> DelReference() == 0) delete ptr;
113 ptr = r.ptr;
114 if (ptr) ptr -> AddReference();
115 }
116 return *this;
117 }
118
121 {
122 if (ptr != p)
123 {
124 if(ptr && ptr -> DelReference() == 0) delete ptr;
125 ptr = p;
126 if (ptr) ptr -> AddReference();
127 }
128 return *this;
129 }
130
131 bool operator== (T *p)
132 {
133 return ptr == p;
134 }
135 bool operator!= (T *p)
136 {
137 return ptr != p;
138 }
139};
140
141#endif
Handle dynamical object creation and pointer affectation.
Definition TReference.hxx:74
Reference_ptr & operator=(const Reference_ptr &r)
Affectation ; destroy old ptr if is no more referenced.
Definition TReference.hxx:108
T * operator->() const
Definition TReference.hxx:98
T * ptr
Pointer to the template class T.
Definition TReference.hxx:76
bool operator==(T *p)
Definition TReference.hxx:131
T * Get() const
Definition TReference.hxx:102
bool operator!=(T *p)
Definition TReference.hxx:135
Reference_ptr(const Reference_ptr &r)
Definition TReference.hxx:84
~Reference_ptr()
Definition TReference.hxx:89
Reference_ptr(T *p=nullptr)
Definition TReference.hxx:79
T & operator*() const
Definition TReference.hxx:94
Base class to reference all Shape objects and some nucleus.
Definition TReference.hxx:38
void AddReference(unsigned n=1)
Definition TReference.hxx:46
unsigned fNbReferences
Number of references to an object.
Definition TReference.hxx:60
TReference()
Definition TReference.hxx:40
virtual ~TReference()=default
Destructor.
unsigned Reference()
Definition TReference.hxx:55
unsigned DelReference(unsigned n=1)
Definition TReference.hxx:50

MURE Project, documentation generated by Doxygen 1.9.7 - Fri Jan 19 2024