Debian Codegraph @

3rdparty/xspublic/xscommon/xsens_shared_pointer.h

raw symbols SCIP
1
2 // Copyright (c) 2003-2019 Xsens Technologies B.V. or subsidiaries worldwide.
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification,
6 // are permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice,
9 // this list of conditions, and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice,
12 // this list of conditions, and the following disclaimer in the documentation
13 // and/or other materials provided with the distribution.
14 //
15 // 3. Neither the names of the copyright holders nor the names of their contributors
16 // may be used to endorse or promote products derived from this software without
17 // specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
24 // OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR
26 // TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.THE LAWS OF THE NETHERLANDS
28 // SHALL BE EXCLUSIVELY APPLICABLE AND ANY DISPUTES SHALL BE FINALLY SETTLED UNDER THE RULES
29 // OF ARBITRATION OF THE INTERNATIONAL CHAMBER OF COMMERCE IN THE HAGUE BY ONE OR MORE
30 // ARBITRATORS APPOINTED IN ACCORDANCE WITH SAID RULES.
31 //
32
33 #ifndef XSENS_SHARED_POINTER_H
34 #define XSENS_SHARED_POINTER_H
35
36 #include <assert.h>
37 #include <atomic>
38
39 namespace xsens {
40
41 #ifndef XSENS_SHARED_POINTER_API
42 #define XSENS_SHARED_POINTER_API
43 #endif
44
45 #ifdef _DEBUG // must use _DEBUG here, otherwise these values are not valid
46 #ifdef XSENS_64BIT
47 #define ptrValid(p) (p != (void*)0 && p != (void*)0xfeeefeeefeeefeeeULL && p != (void*)0xccccccccccccccccULL && p != (void*)0xbaadf00dbaadf00dULL && p != (void*)0xdeadbeefdeadbeefULL && p != (void*)0xababababababababULL && p != (void*)0xbdbdbdbdbdbdbdbdULL && p != (void*)0xfdfdfdfdfdfdfdfdULL && p != (void*)0xcdcdcdcdcdcdcdcdULL && p != (void*)0xddddddddddddddddULL)
48 #else
49 #define ptrValid(p) (p != (void*)0 && p != (void*)0xfeeefeee && p != (void*)0xcccccccc && p != (void*)0xbaadf00d && p != (void*)0xdeadbeef && p != (void*)0xabababab && p != (void*)0xbdbdbdbd && p != (void*)0xfdfdfdfd && p != (void*)0xcdcdcdcd && p != (void*)0xdddddddd)
50 #endif
51 #else
52 #define ptrValid(p) (p != 0)
53 #endif
54
55 /*! \brief Base class for shared operations.
56 \details This class is a required base class when using shared pointers, since it provides
57 the reference counter.
58 */
59 class XSENS_SHARED_POINTER_API SharedData {
60 public:
61 mutable volatile std::atomic_int m_sdAtomicRef; //!< \brief The atomic reference count value
62
63 protected:
64 //! \brief Constructor, initializes the reference count to 0
65 SharedData() : m_sdAtomicRef(0) {}
66
67 //! \internal \brief Destructor
68 virtual ~SharedData() {}
69
70 /*! \brief Copy constructor, initializes the reference count to 0 since the copy is a new object
71 \note This function is not publicly accessible, since the inheriting class should specify its own copying
72 */
73 SharedData(const SharedData&) : m_sdAtomicRef(0) {}
74
75 private:
76 /*! \internal \brief The assignment operator is not implemented and not publicly accessible, since the
77 inheriting class should specify its own copying and how the reference count is handled differs
78 */
79 SharedData& operator =(const SharedData&) = delete;
80 };
81
82 /*! \brief A template class that contains reference counted data with copy-on-write semantics
83 \details The SharedDataPointer class implements implicit sharing and is inspired on QSharedDataPointer.
84 The contained type T should inherit from SharedData and should implement at least a basic constructor and
85 either a copy constructor or a function that allows it to clone itself. In the latter case, a specialized
86 clone() function should be written for the SharedDataPointer class.
87 */
88 template <typename T>
89 class XSENS_SHARED_POINTER_API SharedDataPointer {
90 private:
91 //! \internal \brief A pointer to the contained data.
92 T* d;
93
94 //! \internal \brief Increase the reference count by one
95 inline void ref() const
96 {
97 if (d)
98 {
99 ++d->m_sdAtomicRef;
100 }
101 }
102
103 //! \internal \brief Decrease the reference count by one and possibly delete the referenced object
104 inline void deref()
105 {
106 if (d && !--d->m_sdAtomicRef)
107 {
108 delete d;
109 d = NULL; // we should only set this to NULL if we have deleted it (self-assignment is a bitch)
110 }
111 }
112
113 protected:
114 /*! \brief Create a copy of the referenced object
115 \internal
116 \details This function allows specializations to supply a different way of copying than by using
117 the copy constructor. The default implementation uses the copy constructor.
118
119 The function should not be virtual. Use specialization semantics to define the specialized
120 implementation, for example: \code
121 template<>
122 XmeAuxUnitData* xsens::SharedDataPointer<XmeAuxUnitData>::clone() const { return d->clone(); }
123 \endcode
124 */
125 T* clone() const
126 {
127 if (d)
128 {
129 return new T(*d);
130 }
131 return new T;
132 }
133
134 public:
135 //! \brief Constructor, takes ownership of the pointer and sets the reference count to 1
136 SharedDataPointer() : d(0) {}
137
138 //! \brief Default constructor
139 SharedDataPointer(T* p)
140 {
141 assert(ptrValid(p));
142 ++p->m_sdAtomicRef;
143 d = p;
144 }
145
146 //! \brief Copy constructor, adds a reference to the stored object
147 SharedDataPointer(const SharedDataPointer& p) : d(p.d)
148 {
149 ref();
150 }
151
152 //! \brief Destructor, dereferences the stored data. If the reference count hits 0, the data is deleted.
153 ~SharedDataPointer() { deref(); }
154
155 /*! \brief Prepare the object for editing
156 \details This function makes sure that the reference count is 1 so that we can safely edit the
157 stored object. If the reference count is higher than 1, a deep copy is made. Otherwise the
158 function simply returns.
159 */
160 void detach()
161 {
162 if (!d)
163 {
164 d = clone();
165 ref();
166 return;
167 }
168
169 if (d->m_sdAtomicRef == 1)
170 return;
171
172 T* p = clone();
173 deref();
174 assert(ptrValid(p));
175 d = p;
176 ref();
177 }
178
179 //! \brief Return a const pointer to the data. This does not detach/copy the data.
180 const T* operator->() const { return d; }
181
182 //! \brief Return a pointer to the data. This does not detach/copy the data.
183 T* operator->() { detach(); return d; }
184
185 //! \brief Return a mutable pointer to the data. This detaches/copies the data if the reference count is greater than 1.
186 T* mutableData() { detach(); return d; }
187
188 //! \brief Return a const pointer to the data. This does not detach/copy the data.
189 const T* constData() const { return d; }
190
191 //! \brief Return a const pointer to the data. This does not detach/copy the data.
192 const T* data() const { return d; }
193
194 //! \brief Whether this is a null-pointer
195 bool empty() const { return d == NULL; }
196
197 //! \brief Copy the reference from one SharedDataPointer to another.
198 SharedDataPointer& operator = (const SharedDataPointer& p)
199 {
200 // catch self-assignment, using the proper order of ref and deref
201 p.ref();
202 deref();
203 //assert(ptrValid(p.d)); // FKL: really, it is useless having null-objects when we cannot assign them.
204 d = p.d;
205 return *this;
206 }
207
208 //! \returns The pointer to this object
209 T* get() const throw() {return d;}
210
211 //! \returns The reference to this object
212 T& operator* () const throw() {assert(d != 0); return *d;}
213
214 /*! \brief Swaps the current object with an other object
215 \param other The reference to an other object
216 */
217 void swap(SharedDataPointer& other) throw() {std::swap(d, other.d);}
218
219 /*! \brief Swaps the current object with an other object
220 \details If no argument is passed the object will be reset
221 \param p The pointer to an object
222 */
223 void reset(T* p = 0) throw() {if (d != p) {SharedDataPointer(p).swap(*this);}}
224 };
225
226 /*! \brief A template class that contains reference counted data with explicit copy semantics
227 \details The ExplicitlySharedDataPointer class implements implicit sharing and is inspired on QExplicitlySharedDataPointer.
228 The contained type T should inherit from SharedData and should implement at least a basic constructor and
229 either a copy constructor or a function that allows it to clone itself. In the latter case, a specialized
230 clone() function should be written for the ExplicitlySharedDataPointer class.
231
232 \note Where the SharedDataPointer class implements copy-on-write semantics (which means an implicit detach()),
233 ExplicitlySharedDataPointer requires the user to explicitly call detach() when necessary. The classes
234 are otherwise identical.
235 */
236 template <typename T>
237 class XSENS_SHARED_POINTER_API ExplicitlySharedDataPointer {
238 private:
239 //! \internal \brief A pointer to the contained data.
240 T* d;
241
242 //! \internal \brief Increase the reference count by one
243 void ref() const {
244 if (d)
245 {
246 ++d->m_sdAtomicRef;
247 }
248 }
249
250 //! \internal \brief Decrease the reference count by one and possibly delete the referenced object
251 void deref() {
252 if (d && !--d->m_sdAtomicRef)
253 {
254 delete d;
255 d = 0;
256 }
257 }
258
259 protected:
260 /*! \brief Create a copy of the referenced object
261 \internal
262 \details This function allows specializations to supply a different way of copying than by using
263 the copy constructor. The default implementation uses the copy constructor.
264
265 The function should not be virtual. Use specialization semantics to define the specialized
266 implementation, for example: \code
267 template<>
268 XmeAuxUnitData* xsens::ExplicitlySharedDataPointer<XmeAuxUnitData>::clone() const { return d->clone(); }
269 \endcode
270 */
271 T* clone() const { return new T(*d); }
272
273 public:
274 //! \brief Constructor, creates a new default T object with reference count 1
275 ExplicitlySharedDataPointer() { }
276
277 //! \brief Copy constructor, adds a reference to the stored object
278 ExplicitlySharedDataPointer(const ExplicitlySharedDataPointer& p) : d(p.d) { ref(); }
279
280 //! \brief Constructor, takes ownership of the pointer and sets the reference count to 1
281 ExplicitlySharedDataPointer(T* p)
282 {
283 if (p == NULL)
284 {
285 d = new T;
286 }
287 else
288 {
289 ++p->m_sdAtomicRef;
290 d = p;
291 }
292 }
293
294 //! \brief Destructor, dereferences the stored data. If the reference count hits 0, the data is deleted.
295 ~ExplicitlySharedDataPointer() { deref(); }
296
297 /*! \brief Prepare the object for editing
298 \details This function makes sure that the reference count is 1 so that we can safely edit the
299 stored object. If the reference count is higher than 1, a deep copy is made. Otherwise the
300 function simply returns.
301 */
302 void detach()
303 {
304 if (!d)
305 {
306 d = new T;
307 ref();
308 return;
309 }
310
311 if (d->m_sdAtomicRef.value() == 1)
312 return;
313
314 T* p = clone(d);
315 deref();
316 d = p;
317 }
318
319 //! \brief Return a const pointer to the data. This does not detach/copy the data.
320 const T* operator->() const { return d; }
321
322 //! \brief Return a mutable pointer to the data. This does not detach/copy the data.
323 T* operator->() { return d; }
324
325 //! \brief Return an const pointer to the data. This does not detach/copy the data.
326 const T* constData() const { return d; }
327
328 //! \brief Return a mutable pointer to the data. This does not detach/copy the data.
329 T* mutableData() { return d; }
330
331 //! \brief Return a const pointer to the data. This does not detach/copy the data.
332 const T* data() const { return d; }
333
334 //! \brief Whether this is a null-pointer
335 bool empty() const { return d == NULL; }
336
337 //! \brief Copy the reference from one SharedDataPointer to another.
338 ExplicitlySharedDataPointer& operator = (const ExplicitlySharedDataPointer& p)
339 {
340 // catch self-assignment, using the proper order of ref and deref
341 p.ref();
342 deref();
343 d = p.d;
344 return *this;
345 }
346
347 //! \returns The pointer to this object
348 T* get() const throw() {return d;}
349
350 //! \returns The reference to this object
351 T& operator* () const throw() {assert(d != 0); return *d;}
352
353 /*! \brief Swaps the current object with an other object
354 \param other The reference to an other object
355 */
356 void swap(ExplicitlySharedDataPointer& other) throw() {std::swap(d, other.d);}
357
358 /*! \brief Swaps the current object with an other object
359 \details If no argument is passed the object will be reset
360 \param p The pointer to an object
361 */
362 void reset(T* p = 0) throw() {if (d != p) {ExplicitlySharedDataPointer(p).swap(*this);}}
363 };
364
365 /*! \brief A template class that contains a reference counted pointer
366 \details The SharedDataPointer class implements implicit sharing and is inspired on QSharedDataPointer.
367 The contained type T should inherit from SharedData and should implement at least a basic constructor and
368 either a copy constructor or a function that allows it to clone itself. In the latter case, a specialized
369 clone() function should be written for the SharedDataPointer class.
370 */
371 template <typename T>
372 class XSENS_SHARED_POINTER_API SharedPointer {
373 private:
374 //! \internal \brief A pointer to the contained data.
375 T* d;
376
377 //! \internal \brief Increase the reference count by one
378 void ref() const
379 {
380 if (d != 0)
381 {
382 ++d->m_sdAtomicRef;
383 }
384 }
385
386 //! \internal \brief Decrease the reference count by one and possibly delete the referenced object
387 void deref()
388 {
389 if (d != 0)
390 {
391 if (!--d->m_sdAtomicRef)
392 {
393 delete d;
394 d = 0;
395 }
396 }
397 }
398
399 public:
400 //! \brief Null-pointer constructor
401 SharedPointer() : d(0) {}
402
403 //! \brief Copy constructor, adds a reference to the stored object
404 SharedPointer(const SharedPointer& p) : d(p.d)
405 {
406 ref();
407 }
408
409 //! \brief Constructor, takes ownership of the pointer and sets the reference count to 1
410 SharedPointer(T* p)
411 {
412 if (p)
413 ++p->m_sdAtomicRef;
414 d = p;
415 }
416
417 //! \brief Destructor, dereferences the stored data. If the reference count hits 0, the data is deleted.
418 ~SharedPointer() { deref(); }
419
420 //! \brief Return a const pointer to the data. This does not detach/copy the data.
421 const T* operator()() const { return d; }
422
423 //! \brief Return a mutable pointer to the data. This detaches/copies the data if the reference count is greater than 1.
424 T* operator()() { return d; }
425
426 //! \brief Copy the reference from one SharedDataPointer to another.
427 SharedPointer& operator = (const SharedPointer& p)
428 {
429 // catch self-assignment, using the proper order of ref and deref
430 p.ref();
431 deref();
432 d = p.d;
433 return *this;
434 }
435
436 //! \returns True if this object is null
437 bool empty() const { return d == NULL; }
438
439 //! \returns The pointer to this object
440 T* get() const throw() {return d;}
441
442 //! \returns The pointer to this object. This does not detach/copy the data.
443 T* operator -> () const throw() {assert(d != 0); return d;}
444
445 //! \returns The reference to this object. This does not detach/copy the data.
446 T& operator* () const throw() {assert(d != 0); return *d;}
447
448 /*! \brief Swaps the current object with an other object
449 \param other The reference to an other object
450 */
451 void swap(SharedPointer& other) throw() {std::swap(d, other.d);}
452
453 /*! \brief Swaps the current object with an other object
454 \details If no argument is passed the object will be reset
455 \param p The pointer to an object
456 */
457 void reset(T* p = 0) throw() {if (d != p) {SharedPointer(p).swap(*this);}}
458 };
459
460 } // namespace xsens
461
462 #endif // file guard