LibMusicXML 3.22
smartpointer.h
1/*
2 MusicXML Library
3 Copyright (C) Grame 2006-2013
4
5 This Source Code Form is subject to the terms of the Mozilla Public
6 License, v. 2.0. If a copy of the MPL was not distributed with this
7 file, You can obtain one at http://mozilla.org/MPL/2.0/.
8
9 Grame Research Laboratory, 11, cours de Verdun Gensoul 69002 Lyon - France
10 research@grame.fr
11*/
12
13#ifndef __smartpointer__
14#define __smartpointer__
15
16#include <cassert>
17#include "exports.h"
18
19namespace MusicXML2
20{
21
29class EXP smartable {
30 private:
31 unsigned refCount;
32 public:
34 unsigned refs() const { return refCount; }
36 void addReference() { refCount++; assert(refCount != 0); }
38 void removeReference() { if (--refCount == 0) delete this; }
39
40 protected:
41 smartable() : refCount(0) {}
42 smartable(const smartable&): refCount(0) {}
44 virtual ~smartable() { assert (refCount == 0); }
45 smartable& operator=(const smartable&) { return *this; }
46};
47
58template<class T> class SMARTP {
59 private:
61 T* fSmartPtr;
62
63 public:
65 SMARTP() : fSmartPtr(0) {}
67 SMARTP(T* rawptr) : fSmartPtr(rawptr) { if (fSmartPtr) fSmartPtr->addReference(); }
69 template<class T2>
70 SMARTP(const SMARTP<T2>& ptr) : fSmartPtr((T*)ptr) { if (fSmartPtr) fSmartPtr->addReference(); }
72 SMARTP(const SMARTP& ptr) : fSmartPtr((T*)ptr) { if (fSmartPtr) fSmartPtr->addReference(); }
73
75 ~SMARTP() { if (fSmartPtr) fSmartPtr->removeReference(); }
76
78 operator T*() const { return fSmartPtr; }
79
81 T& operator*() const {
82 // checks for null dereference
83 assert (fSmartPtr != 0);
84 return *fSmartPtr;
85 }
86
88 T* operator->() const {
89 // checks for null dereference
90 assert (fSmartPtr != 0);
91 return fSmartPtr;
92 }
93
95 template <class T2>
96 SMARTP& operator=(T2 p1_) { *this=(T*)p1_; return *this; }
97
99 SMARTP& operator=(T* p_) {
100 // check first that pointers differ
101 if (fSmartPtr != p_) {
102 // increments the ref count of the new pointer if not null
103 if (p_ != 0) p_->addReference();
104 // decrements the ref count of the old pointer if not null
105 if (fSmartPtr != 0) fSmartPtr->removeReference();
106 // and finally stores the new actual pointer
107 fSmartPtr = p_;
108 }
109 return *this;
110 }
111
112 SMARTP& operator=(const SMARTP<T>& p_) { return operator=((T *) p_); }
114 template<class T2> SMARTP& cast(T2* p_) { return operator=(dynamic_cast<T*>(p_)); }
116 template<class T2> SMARTP& cast(const SMARTP<T2>& p_) { return operator=(dynamic_cast<T*>(p_)); }
118 bool operator < (const SMARTP<T>& p_) const { return (void*)this < (void*)p_; }
119};
120
121}
122
123#endif
SMARTP(const SMARTP &ptr)
build a smart pointer from another smart pointer reference
Definition smartpointer.h:72
T & operator*() const
'*' operator to access the actual class pointer
Definition smartpointer.h:81
SMARTP & operator=(const SMARTP< T > &p_)
operator = to support inherited class reference
Definition smartpointer.h:112
SMARTP & operator=(T2 p1_)
operator = that moves the actual class pointer
Definition smartpointer.h:96
bool operator<(const SMARTP< T > &p_) const
operator < (require by VC6 for maps)
Definition smartpointer.h:118
SMARTP(T *rawptr)
build a smart pointer from a class pointer
Definition smartpointer.h:67
SMARTP(const SMARTP< T2 > &ptr)
build a smart pointer from an convertible class reference
Definition smartpointer.h:70
T * operator->() const
operator -> overloading to access the actual class pointer
Definition smartpointer.h:88
SMARTP & cast(const SMARTP< T2 > &p_)
dynamic cast support
Definition smartpointer.h:116
SMARTP & operator=(T *p_)
operator = that moves the actual class pointer
Definition smartpointer.h:99
SMARTP()
an empty constructor - points to null
Definition smartpointer.h:65
SMARTP & cast(T2 *p_)
dynamic cast support
Definition smartpointer.h:114
~SMARTP()
the smart pointer destructor: simply removes one reference count
Definition smartpointer.h:75
the base class for smart pointers implementation
Definition smartpointer.h:29
void addReference()
addReference increments the ref count and checks for refCount overflow
Definition smartpointer.h:36
virtual ~smartable()
destructor checks for non-zero refCount
Definition smartpointer.h:44
unsigned refs() const
gives the reference count of the object
Definition smartpointer.h:34
void removeReference()
removeReference delete the object when refCount is zero
Definition smartpointer.h:38