Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
usSharedData.h
Go to the documentation of this file.
1 /*=============================================================================
2 
3  Library: CppMicroServices
4 
5  Copyright (c) German Cancer Research Center,
6  Division of Medical and Biological Informatics
7 
8  Licensed under the Apache License, Version 2.0 (the "License");
9  you may not use this file except in compliance with the License.
10  You may obtain a copy of the License at
11 
12  http://www.apache.org/licenses/LICENSE-2.0
13 
14  Unless required by applicable law or agreed to in writing, software
15  distributed under the License is distributed on an "AS IS" BASIS,
16  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  See the License for the specific language governing permissions and
18  limitations under the License.
19 
20 
21 Modified version of qshareddata.h from Qt 4.7.3 for CppMicroServices.
22 Original copyright (c) Nokia Corporation. Usage covered by the
23 GNU Lesser General Public License version 2.1
24 (http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html) and the Nokia Qt
25 LGPL Exception version 1.1 (file LGPL_EXCEPTION.txt in Qt 4.7.3 package).
26 
27 =========================================================================*/
28 
29 #ifndef USSHAREDDATA_H
30 #define USSHAREDDATA_H
31 
32 #include "usAtomicInt_p.h"
33 
34 #include <algorithm>
35 #include <utility>
36 
37 US_BEGIN_NAMESPACE
38 
43 {
44 public:
45  mutable AtomicInt ref;
46 
47  inline SharedData() : ref(0) { }
48  inline SharedData(const SharedData&) : ref(0) { }
49 
50 private:
51  // using the assignment operator would lead to corruption in the ref-counting
52  SharedData& operator=(const SharedData&);
53 };
54 
58 template <class T>
60 {
61 public:
62  typedef T Type;
63  typedef T* pointer;
64 
65  inline void Detach() { if (d && d->ref != 1) Detach_helper(); }
66  inline T& operator*() { Detach(); return *d; }
67  inline const T& operator*() const { return *d; }
68  inline T* operator->() { Detach(); return d; }
69  inline const T* operator->() const { return d; }
70  inline operator T*() { Detach(); return d; }
71  inline operator const T*() const { return d; }
72  inline T* Data() { Detach(); return d; }
73  inline const T* Data() const { return d; }
74  inline const T* ConstData() const { return d; }
75 
76  inline bool operator==(const SharedDataPointer<T>& other) const { return d == other.d; }
77  inline bool operator!=(const SharedDataPointer<T>& other) const { return d != other.d; }
78 
79  inline SharedDataPointer() : d(0) { }
80  inline ~SharedDataPointer() { if (d && !d->ref.Deref()) delete d; }
81 
82  explicit SharedDataPointer(T* data);
83  inline SharedDataPointer(const SharedDataPointer<T>& o) : d(o.d) { if (d) d->ref.Ref(); }
84 
86  {
87  if (o.d != d)
88  {
89  if (o.d)
90  o.d->ref.Ref();
91  T *old = d;
92  d = o.d;
93  if (old && !old->ref.Deref())
94  delete old;
95  }
96  return *this;
97  }
98 
100  {
101  if (o != d)
102  {
103  if (o)
104  o->ref.Ref();
105  T *old = d;
106  d = o;
107  if (old && !old->ref.Deref())
108  delete old;
109  }
110  return *this;
111  }
112 
113  inline bool operator!() const { return !d; }
114 
115  inline void Swap(SharedDataPointer& other)
116  {
117  using std::swap;
118  swap(d, other.d);
119  }
120 
121 protected:
122  T* Clone();
123 
124 private:
125  void Detach_helper();
126 
127  T *d;
128 };
129 
133 template <class T> class ExplicitlySharedDataPointer
134 {
135 public:
136  typedef T Type;
137  typedef T* pointer;
138 
139  inline T& operator*() const { return *d; }
140  inline T* operator->() { return d; }
141  inline T* operator->() const { return d; }
142  inline T* Data() const { return d; }
143  inline const T* ConstData() const { return d; }
144 
145  inline void Detach() { if (d && d->ref != 1) Detach_helper(); }
146 
147  inline void Reset()
148  {
149  if(d && !d->ref.Deref())
150  delete d;
151 
152  d = 0;
153  }
154 
155  inline operator bool () const { return d != 0; }
156 
157  inline bool operator==(const ExplicitlySharedDataPointer<T>& other) const { return d == other.d; }
158  inline bool operator!=(const ExplicitlySharedDataPointer<T>& other) const { return d != other.d; }
159  inline bool operator==(const T* ptr) const { return d == ptr; }
160  inline bool operator!=(const T* ptr) const { return d != ptr; }
161 
162  inline ExplicitlySharedDataPointer() { d = 0; }
163  inline ~ExplicitlySharedDataPointer() { if (d && !d->ref.Deref()) delete d; }
164 
165  explicit ExplicitlySharedDataPointer(T* data);
167  : d(o.d) { if (d) d->ref.Ref(); }
168 
169  template<class X>
171  : d(static_cast<T*>(o.Data()))
172  {
173  if(d)
174  d->ref.Ref();
175  }
176 
178  {
179  if (o.d != d)
180  {
181  if (o.d)
182  o.d->ref.Ref();
183  T *old = d;
184  d = o.d;
185  if (old && !old->ref.Deref())
186  delete old;
187  }
188  return *this;
189  }
190 
192  {
193  if (o != d)
194  {
195  if (o)
196  o->ref.Ref();
197  T *old = d;
198  d = o;
199  if (old && !old->ref.Deref())
200  delete old;
201  }
202  return *this;
203  }
204 
205  inline bool operator!() const { return !d; }
206 
207  inline void Swap(ExplicitlySharedDataPointer& other)
208  {
209  using std::swap;
210  swap(d, other.d);
211  }
212 
213 protected:
214  T* Clone();
215 
216 private:
217  void Detach_helper();
218 
219  T *d;
220 };
221 
222 
223 template <class T>
225 { if (d) d->ref.Ref(); }
226 
227 template <class T>
229 {
230  return new T(*d);
231 }
232 
233 template <class T>
235 {
236  T *x = Clone();
237  x->ref.Ref();
238  if (!d->ref.Deref())
239  delete d;
240  d = x;
241 }
242 
243 template <class T>
245 {
246  return new T(*d);
247 }
248 
249 template <class T>
251 {
252  T *x = Clone();
253  x->ref.Ref();
254  if (!d->ref.Deref())
255  delete d;
256  d = x;
257 }
258 
259 template <class T>
261  : d(adata)
262 { if (d) d->ref.Ref(); }
263 
264 template <class T>
265 void swap(US_PREPEND_NAMESPACE(SharedDataPointer<T)>& p1, US_PREPEND_NAMESPACE(SharedDataPointer<T)>& p2)
266 { p1.Swap(p2); }
267 
268 template <class T>
269 void swap(US_PREPEND_NAMESPACE(ExplicitlySharedDataPointer<T)>& p1, US_PREPEND_NAMESPACE(ExplicitlySharedDataPointer<T)>& p2)
270 { p1.Swap(p2); }
271 
272 US_END_NAMESPACE
273 
274 #endif // USSHAREDDATA_H
bool operator==(const SharedDataPointer< T > &other) const
Definition: usSharedData.h:76
ExplicitlySharedDataPointer(const ExplicitlySharedDataPointer< X > &o)
Definition: usSharedData.h:170
const T * ConstData() const
Definition: usSharedData.h:74
void Swap(SharedDataPointer &other)
Definition: usSharedData.h:115
bool operator!=(const T *ptr) const
Definition: usSharedData.h:160
SharedData(const SharedData &)
Definition: usSharedData.h:48
SharedDataPointer< T > & operator=(const SharedDataPointer< T > &o)
Definition: usSharedData.h:85
bool operator!=(const SharedDataPointer< T > &other) const
Definition: usSharedData.h:77
void swap(us::SharedDataPointer< T > &p1, us::SharedDataPointer< T > &p2)
Definition: usSharedData.h:265
bool operator!=(const ExplicitlySharedDataPointer< T > &other) const
Definition: usSharedData.h:158
void Swap(ExplicitlySharedDataPointer &other)
Definition: usSharedData.h:207
SharedDataPointer & operator=(T *o)
Definition: usSharedData.h:99
bool operator!() const
Definition: usSharedData.h:113
AtomicInt ref
Definition: usSharedData.h:45
ExplicitlySharedDataPointer(const ExplicitlySharedDataPointer< T > &o)
Definition: usSharedData.h:166
ExplicitlySharedDataPointer< T > & operator=(const ExplicitlySharedDataPointer< T > &o)
Definition: usSharedData.h:177
bool operator==(const T *ptr) const
Definition: usSharedData.h:159
static void swap(T &x, T &y)
Definition: svm.cpp:72
ExplicitlySharedDataPointer & operator=(T *o)
Definition: usSharedData.h:191
const T * operator->() const
Definition: usSharedData.h:69
const T * ConstData() const
Definition: usSharedData.h:143
SharedDataPointer(const SharedDataPointer< T > &o)
Definition: usSharedData.h:83
bool operator==(const ExplicitlySharedDataPointer< T > &other) const
Definition: usSharedData.h:157
static mitk::PlanarFigure::Pointer Clone(mitk::PlanarFigure::Pointer original)
const T * Data() const
Definition: usSharedData.h:73
const T & operator*() const
Definition: usSharedData.h:67