55
55
inline DSharedData() : ref(0) { }
56
inline DSharedData(const DSharedData &) : ref(0) { }
56
inline DSharedData(const DSharedData&) : ref(0) { }
58
58
/** Returns true if the reference count is not 0.
59
59
* For the normal use case, you do not need this method.
61
61
inline bool isReferenced() const
62
{ return (int)ref > 0; }
63
65
inline bool hasMoreReferences() const
64
{ return (int)ref != 1; }
67
71
// using the assignment operator would lead to corruption in the ref-counting
68
DSharedData& operator=(const DSharedData &);
72
DSharedData& operator=(const DSharedData&);
71
75
template <class T> class DSharedDataPointer
86
90
/** Various operators for accessing the pointer const and non-const */
87
inline T& operator*() { return *d; }
88
inline const T& operator*() const { return *d; }
89
inline T *operator->() { return d; }
90
inline const T *operator->() const { return d; }
91
inline operator T *() { return d; }
92
inline operator const T *() const { return d; }
93
inline T *data() { return d; }
95
inline const T& operator*() const
99
inline T* operator->()
103
inline const T* operator->() const
107
inline operator T* ()
111
inline operator const T* () const
95
inline const T *data() const { return d; }
96
inline const T *constData() const { return d; }
120
inline const T* data() const
124
inline const T* constData() const
98
129
/** This method carries out a const_cast, so it returns a non-const pointer
99
130
* from a const DSharedDataPointer.
100
131
* Typically, this should only be used if you know it should be used
101
132
* (to implement a lazy loading caching technique or similar)
103
inline T* constCastData() const { return const_cast<T*>(d); }
105
inline bool operator==(const DSharedDataPointer<T>& other) const { return d == other.d; }
106
inline bool operator!=(const DSharedDataPointer<T>& other) const { return d != other.d; }
108
inline DSharedDataPointer() { d = 0; }
110
explicit inline DSharedDataPointer(T *data) : d(data)
111
{ if (d) d->ref.ref(); }
134
inline T* constCastData() const
136
return const_cast<T*>(d);
139
inline bool operator==(const DSharedDataPointer<T>& other) const
143
inline bool operator!=(const DSharedDataPointer<T>& other) const
148
inline DSharedDataPointer()
153
explicit inline DSharedDataPointer(T* data) : d(data)
113
161
inline ~DSharedDataPointer()
114
{ if (d && !d->ref.deref()) delete d; }
163
if (d && !d->ref.deref())
116
169
inline DSharedDataPointer(const DSharedDataPointer<T>& o) : d(o.d)
117
{ if (d) d->ref.ref(); }
119
177
inline DSharedDataPointer<T> & operator=(const DSharedDataPointer<T>& o)
137
195
* @returns A T object with reference count 0, which may be deleted;
138
196
* or 0 if no object need to be dropped.
140
inline T *assign(const DSharedDataPointer<T>& o)
198
inline T* assign(const DSharedDataPointer<T>& o)
144
202
// reference new value
147
208
// store old value
149
210
// assign new value
151
213
// dereference old value,
152
214
// return value and ownership if dereferenced
153
215
if (x && !x->ref.deref())
159
inline T *assign(T *o)
224
inline T* assign(T* o)
163
228
// reference new value
166
234
// store old value
168
236
// assign new value
170
239
// dereference old value,
171
240
// return value and ownership if dereferenced
172
241
if (x && !x->ref.deref())
179
251
* Semantics like assign, but no new pointer is assigned to this.
183
255
return assign(0);
186
inline bool operator!() const { return !d; }
258
inline bool operator!() const