Medical Imaging Interaction Toolkit  2026.06.00
Medical Imaging Interaction Toolkit
usSharedData.h
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 /*============================================================================
14 
15 Modified version of qshareddata.h from Qt 4.7.3 for CppMicroServices.
16 Original copyright (c) Nokia Corporation. Usage covered by the
17 GNU Lesser General Public License version 2.1
18 (https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html) and the Nokia Qt
19 LGPL Exception version 1.1 (file LGPL_EXCEPTION.txt in Qt 4.7.3 package).
20 
21 ============================================================================*/
22 
23 #ifndef USSHAREDDATA_H
24 #define USSHAREDDATA_H
25 
26 #include <usAtomicInt_p.h>
27 
28 #include <algorithm>
29 #include <utility>
30 
31 namespace us {
32 
42 {
43 public:
44  mutable AtomicInt ref;
45 
46  inline SharedData() : ref(0) { }
47  inline SharedData(const SharedData&) : ref(0) { }
48 
49 private:
50  // using the assignment operator would lead to corruption in the ref-counting
51  SharedData& operator=(const SharedData&);
52 };
53 
63 template <class T>
65 {
66 public:
67  typedef T Type;
68  typedef T* pointer;
69 
70  inline void Detach() { if (d && d->ref != 1) Detach_helper(); }
71  inline T& operator*() { Detach(); return *d; }
72  inline const T& operator*() const { return *d; }
73  inline T* operator->() { Detach(); return d; }
74  inline const T* operator->() const { return d; }
75  inline operator T*() { Detach(); return d; }
76  inline operator const T*() const { return d; }
77  inline T* Data() { Detach(); return d; }
78  inline const T* Data() const { return d; }
79  inline const T* ConstData() const { return d; }
80 
81  inline bool operator==(const SharedDataPointer<T>& other) const { return d == other.d; }
82  inline bool operator!=(const SharedDataPointer<T>& other) const { return d != other.d; }
83 
84  inline SharedDataPointer() : d(nullptr) { }
85  inline ~SharedDataPointer() { if (d && !d->ref.Deref()) delete d; }
86 
87  explicit SharedDataPointer(T* data);
88  inline SharedDataPointer(const SharedDataPointer<T>& o) : d(o.d) { if (d) d->ref.Ref(); }
89 
91  {
92  if (o.d != d)
93  {
94  if (o.d)
95  o.d->ref.Ref();
96  T *old = d;
97  d = o.d;
98  if (old && !old->ref.Deref())
99  delete old;
100  }
101  return *this;
102  }
103 
105  {
106  if (o != d)
107  {
108  if (o)
109  o->ref.Ref();
110  T *old = d;
111  d = o;
112  if (old && !old->ref.Deref())
113  delete old;
114  }
115  return *this;
116  }
117 
118  inline bool operator!() const { return !d; }
119 
120  inline void Swap(SharedDataPointer& other)
121  {
122  using std::swap;
123  swap(d, other.d);
124  }
125 
126 protected:
127  T* Clone();
128 
129 private:
130  void Detach_helper();
131 
132  T *d;
133 };
134 
145 template <class T> class ExplicitlySharedDataPointer
146 {
147 public:
148  typedef T Type;
149  typedef T* pointer;
150 
151  inline T& operator*() const { return *d; }
152  inline T* operator->() { return d; }
153  inline T* operator->() const { return d; }
154  inline T* Data() const { return d; }
155  inline const T* ConstData() const { return d; }
156 
157  inline void Detach() { if (d && d->ref != 1) Detach_helper(); }
158 
159  inline void Reset()
160  {
161  if(d && !d->ref.Deref())
162  delete d;
163 
164  d = 0;
165  }
166 
167  inline operator bool () const { return d != nullptr; }
168 
169  inline bool operator==(const ExplicitlySharedDataPointer<T>& other) const { return d == other.d; }
170  inline bool operator!=(const ExplicitlySharedDataPointer<T>& other) const { return d != other.d; }
171  inline bool operator==(const T* ptr) const { return d == ptr; }
172  inline bool operator!=(const T* ptr) const { return d != ptr; }
173 
174  inline ExplicitlySharedDataPointer() { d = 0; }
175  inline ~ExplicitlySharedDataPointer() { if (d && !d->ref.Deref()) delete d; }
176 
177  explicit ExplicitlySharedDataPointer(T* data);
179  : d(o.d) { if (d) d->ref.Ref(); }
180 
181  template<class X>
183  : d(static_cast<T*>(o.Data()))
184  {
185  if(d)
186  d->ref.Ref();
187  }
188 
190  {
191  if (o.d != d)
192  {
193  if (o.d)
194  o.d->ref.Ref();
195  T *old = d;
196  d = o.d;
197  if (old && !old->ref.Deref())
198  delete old;
199  }
200  return *this;
201  }
202 
204  {
205  if (o != d)
206  {
207  if (o)
208  o->ref.Ref();
209  T *old = d;
210  d = o;
211  if (old && !old->ref.Deref())
212  delete old;
213  }
214  return *this;
215  }
216 
217  inline bool operator!() const { return !d; }
218 
219  inline void Swap(ExplicitlySharedDataPointer& other)
220  {
221  using std::swap;
222  swap(d, other.d);
223  }
224 
225 protected:
226  T* Clone();
227 
228 private:
229  void Detach_helper();
230 
231  T *d;
232 };
233 
234 
235 template <class T>
237 { if (d) d->ref.Ref(); }
238 
239 template <class T>
241 {
242  return new T(*d);
243 }
244 
245 template <class T>
247 {
248  T *x = Clone();
249  x->ref.Ref();
250  if (!d->ref.Deref())
251  delete d;
252  d = x;
253 }
254 
255 template <class T>
257 {
258  return new T(*d);
259 }
260 
261 template <class T>
263 {
264  T *x = Clone();
265  x->ref.Ref();
266  if (!d->ref.Deref())
267  delete d;
268  d = x;
269 }
270 
271 template <class T>
273  : d(adata)
274 { if (d) d->ref.Ref(); }
275 
276 template <class T>
278 { p1.Swap(p2); }
279 
280 template <class T>
282 { p1.Swap(p2); }
283 
284 }
285 
286 #endif // USSHAREDDATA_H
A pointer to explicitly shared data requiring manual detach for copy-on-write.
Definition: usSharedData.h:146
bool operator==(const ExplicitlySharedDataPointer< T > &other) const
Definition: usSharedData.h:169
ExplicitlySharedDataPointer(const ExplicitlySharedDataPointer< X > &o)
Definition: usSharedData.h:182
ExplicitlySharedDataPointer & operator=(T *o)
Definition: usSharedData.h:203
bool operator!=(const T *ptr) const
Definition: usSharedData.h:172
bool operator!=(const ExplicitlySharedDataPointer< T > &other) const
Definition: usSharedData.h:170
ExplicitlySharedDataPointer< T > & operator=(const ExplicitlySharedDataPointer< T > &o)
Definition: usSharedData.h:189
ExplicitlySharedDataPointer(const ExplicitlySharedDataPointer< T > &o)
Definition: usSharedData.h:178
bool operator==(const T *ptr) const
Definition: usSharedData.h:171
void Swap(ExplicitlySharedDataPointer &other)
Definition: usSharedData.h:219
A pointer to implicitly shared data with automatic copy-on-write semantics.
Definition: usSharedData.h:65
const T * ConstData() const
Definition: usSharedData.h:79
bool operator!=(const SharedDataPointer< T > &other) const
Definition: usSharedData.h:82
SharedDataPointer & operator=(T *o)
Definition: usSharedData.h:104
const T & operator*() const
Definition: usSharedData.h:72
void Swap(SharedDataPointer &other)
Definition: usSharedData.h:120
SharedDataPointer< T > & operator=(const SharedDataPointer< T > &o)
Definition: usSharedData.h:90
const T * Data() const
Definition: usSharedData.h:78
const T * operator->() const
Definition: usSharedData.h:74
SharedDataPointer(const SharedDataPointer< T > &o)
Definition: usSharedData.h:88
bool operator!() const
Definition: usSharedData.h:118
bool operator==(const SharedDataPointer< T > &other) const
Definition: usSharedData.h:81
Base class for implicitly shared data with reference counting.
Definition: usSharedData.h:42
SharedData(const SharedData &)
Definition: usSharedData.h:47
AtomicInt ref
Definition: usSharedData.h:44
void swap(us::SharedDataPointer< T > &p1, us::SharedDataPointer< T > &p2)
Definition: usSharedData.h:277
void swap(us::ExplicitlySharedDataPointer< T > &p1, us::ExplicitlySharedDataPointer< T > &p2)
Definition: usSharedData.h:281