Jpp test-rotations-new
the software that should make you happy
Loading...
Searching...
No Matches
JSharedPointer.hh
Go to the documentation of this file.
1#ifndef __JLANG__JSHAREDPOINTER__
2#define __JLANG__JSHAREDPOINTER__
3
5#include "JLang/JMemory.hh"
6#include "JLang/JStorage.hh"
7
8
9/**
10 * \author mdejong
11 */
12
13namespace JLANG {}
14namespace JPP { using namespace JLANG; }
15
16namespace JLANG {
17
18
19 /**
20 * The template JSharedPointer class can be used to share a pointer to an object.
21 * The object pointed to is deleted when the shared counter is zero,
22 * i.e.\ when no-one shares the object.
23 * It is possible to create a container with shared pointers.
24 * The first template argument refers to the data type pointed to
25 * and the second to the memory management policy.
26 */
27 template<class JClass_t, template<class> class JMemory_t = JNew>
29 public JSharedCounter,
30 public JStorage<JClass_t, JMemory_t>
31 {
32 public:
33
36
37 using JAbstractPointer<JClass_t>::reset;
38
39
40 /**
41 * Default constructor.
42 */
45
46
47 /**
48 * Copy constructor.
49 * The reference counter of the shared object is incremented by one.
50 *
51 * \param object shared pointer
52 */
54 {
55 if (object.is_valid()) {
56 this->set(object);
57 }
58 }
59
60
61 /**
62 * Assignment constructor.
63 * If the pointer is valid, the reference counter of the shared object pointed to
64 * is initialised to one.
65 *
66 * \param p pointer to derived class object
67 */
68 template<class JDerived_t>
69 JSharedPointer(JDerived_t* p)
70 {
71 if (p != NULL) {
72 this->set(p);
73 }
74 }
75
76
77 /**
78 * Destructor.
79 * The reference counter is decremented by one and
80 * the object pointed to is deleted when the reference counter is zero.
81 */
83 {
84 if (this->detach()) {
86 }
87 }
88
89
90 /**
91 * Get shared pointer.
92 *
93 * \return this shared pointer
94 */
96 {
97 return static_cast<const JSharedPointer&>(*this);
98 }
99
100
101 /**
102 * Get shared pointer.
103 *
104 * \return this shared pointer
105 */
107 {
108 return static_cast<JSharedPointer&>(*this);
109 }
110
111
112 /**
113 * Set shared pointer.
114 *
115 * \param object shared pointer
116 */
118 {
119 if (this->get() != object.get()) {
120
121 this->reset();
122
123 if (object.is_valid()) {
124 this->set(object);
125 }
126 }
127 }
128
129
130 /**
131 * Assignment operator.
132 * The reference counter is decremented by one and
133 * the object pointed to previously is deleted when its reference counter is zero.
134 * The reference counter of the shared object is incremented by one.
135 *
136 * \param object shared pointer
137 * \return this shared pointer
138 */
140 {
141 this->setSharedPointer(object);
142
143 return *this;
144 }
145
146
147 /**
148 * Assignment operator.
149 * The reference counter is decremented by one and
150 * the object pointed to previously is deleted when its reference counter is zero.
151 * If the pointer is valid, the reference counter of the shared object pointed to
152 * is initialised to one.
153 *
154 * \param p pointer to derived class object
155 * \return this shared pointer
156 */
157 template<class JDerived_t>
158 JSharedPointer& operator=(JDerived_t* p)
159 {
160 this->reset(p);
161
162 return *this;
163 }
164
165
166 /**
167 * Reset pointer.
168 * The reference counter is decremented by one and
169 * the object pointed to previously is deleted when its reference counter is zero.
170 */
171 virtual void reset() override
172 {
173 if (this->detach()) {
175 }
176
178 }
179
180
181 protected:
182 /**
183 * Set pointer.
184 * The reference counter of the shared object pointed to is incremented by one.
185 *
186 * \param object shared pointer
187 */
188 void set(const JSharedPointer& object)
189 {
190 pointer_type::set(object.get());
191
192 this->attach(object);
193 }
194
195
196 /**
197 * Set pointer.
198 * The reference counter of the shared object pointed to is initialised to one.
199 *
200 * \param p pointer to derived class object
201 */
202 virtual void set(JClass_t* p) override
203 {
205
206 this->initialise();
207 }
208 };
209}
210
211#endif
Base class for memory management.
Template interface for pointer to object(s).
bool is_valid() const
Check validity of pointer.
Memory management class for create/release policy based on new/delete.
Definition JMemory.hh:23
Template implementation of class that holds pointer to object(s).
Definition JPointer.hh:24
virtual void set(JClass_t *p) override
Set pointer.
Definition JPointer.hh:75
virtual void reset() override
Reset pointer.
Definition JPointer.hh:84
virtual JClass_t * get() const override
Get pointer.
Definition JPointer.hh:64
void initialise()
Initialise counter.
void attach(const JSharedCounter &object)
Attach this counter to given shared counter object.
The template JSharedPointer class can be used to share a pointer to an object.
JSharedPointer(JDerived_t *p)
Assignment constructor.
const JSharedPointer & getSharedPointer() const
Get shared pointer.
virtual void reset() override
Reset pointer.
JSharedPointer & operator=(JDerived_t *p)
Assignment operator.
JSharedPointer & operator=(const JSharedPointer &object)
Assignment operator.
JPointer< JClass_t > pointer_type
JSharedPointer & getSharedPointer()
Get shared pointer.
JStorage< JClass_t, JMemory_t > storage_type
JSharedPointer()
Default constructor.
JSharedPointer(const JSharedPointer &object)
Copy constructor.
virtual void set(JClass_t *p) override
Set pointer.
void set(const JSharedPointer &object)
Set pointer.
void setSharedPointer(const JSharedPointer &object)
Set shared pointer.
~JSharedPointer()
Destructor.
Template storage class.
Definition JStorage.hh:29
virtual void reset() override
Reset pointer.
Definition JStorage.hh:42
Auxiliary classes and methods for language specific functionality.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).