Medical Imaging Interaction Toolkit  2018.4.99-ef453c4b
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 (DKFZ)
6  All rights reserved.
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 
22 /*============================================================================
23 
24 Modified version of qshareddata.h from Qt 4.7.3 for CppMicroServices.
25 Original copyright (c) Nokia Corporation. Usage covered by the
26 GNU Lesser General Public License version 2.1
27 (http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html) and the Nokia Qt
28 LGPL Exception version 1.1 (file LGPL_EXCEPTION.txt in Qt 4.7.3 package).
29 
30 ============================================================================*/
31 
32 #ifndef USSHAREDDATA_H
33 #define USSHAREDDATA_H
34 
35 #include "usAtomicInt_p.h"
36 
37 #include <algorithm>
38 #include <utility>
39 
40 US_BEGIN_NAMESPACE
41 
46 {
47 public:
48  mutable AtomicInt ref;
49 
50  inline SharedData() : ref(0) { }
51  inline SharedData(const SharedData&) : ref(0) { }
52 
53 private:
54  // using the assignment operator would lead to corruption in the ref-counting
55  SharedData& operator=(const SharedData&);
56 };
57 
61 template <class T>
63 {
64 public:
65  typedef T Type;
66  typedef T* pointer;
67 
68  inline void Detach() { if (d && d->ref != 1) Detach_helper(); }
69  inline T& operator*() { Detach(); return *d; }
70  inline const T& operator*() const { return *d; }
71  inline T* operator->() { Detach(); return d; }
72  inline const T* operator->() const { return d; }
73  inline operator T*() { Detach(); return d; }
74  inline operator const T*() const { return d; }
75  inline T* Data() { Detach(); return d; }
76  inline const T* Data() const { return d; }
77  inline const T* ConstData() const { return d; }
78 
79  inline bool operator==(const SharedDataPointer<T>& other) const { return d == other.d; }
80  inline bool operator!=(const SharedDataPointer<T>& other) const { return d != other.d; }
81 
82  inline SharedDataPointer() : d(nullptr) { }
83  inline ~SharedDataPointer() { if (d && !d->ref.Deref()) delete d; }
84 
85  explicit SharedDataPointer(T* data);
86  inline SharedDataPointer(const SharedDataPointer<T>& o) : d(o.d) { if (d) d->ref.Ref(); }
87 
89  {
90  if (o.d != d)
91  {
92  if (o.d)
93  o.d->ref.Ref();
94  T *old = d;
95  d = o.d;
96  if (old && !old->ref.Deref())
97  delete old;
98  }
99  return *this;
100  }
101 
103  {
104  if (o != d)
105  {
106  if (o)
107  o->ref.Ref();
108  T *old = d;
109  d = o;
110  if (old && !old->ref.Deref())
111  delete old;
112  }
113  return *this;
114  }
115 
116  inline bool operator!() const { return !d; }
117 
118  inline void Swap(SharedDataPointer& other)
119  {
120  using std::swap;
121  swap(d, other.d);
122  }
123 
124 protected:
125  T* Clone();
126 
127 private:
128  void Detach_helper();
129 
130  T *d;
131 };
132 
136 template <class T> class ExplicitlySharedDataPointer
137 {
138 public:
139  typedef T Type;
140  typedef T* pointer;
141 
142  inline T& operator*() const { return *d; }
143  inline T* operator->() { return d; }
144  inline T* operator->() const { return d; }
145  inline T* Data() const { return d; }
146  inline const T* ConstData() const { return d; }
147 
148  inline void Detach() { if (d && d->ref != 1) Detach_helper(); }
149 
150  inline void Reset()
151  {
152  if(d && !d->ref.Deref())
153  delete d;
154 
155  d = 0;
156  }
157 
158  inline operator bool () const { return d != nullptr; }
159 
160  inline bool operator==(const ExplicitlySharedDataPointer<T>& other) const { return d == other.d; }
161  inline bool operator!=(const ExplicitlySharedDataPointer<T>& other) const { return d != other.d; }
162  inline bool operator==(const T* ptr) const { return d == ptr; }
163  inline bool operator!=(const T* ptr) const { return d != ptr; }
164 
165  inline ExplicitlySharedDataPointer() { d = 0; }
166  inline ~ExplicitlySharedDataPointer() { if (d && !d->ref.Deref()) delete d; }
167 
168  explicit ExplicitlySharedDataPointer(T* data);
170  : d(o.d) { if (d) d->ref.Ref(); }
171 
172  template<class X>
174  : d(static_cast<T*>(o.Data()))
175  {
176  if(d)
177  d->ref.Ref();
178  }
179 
181  {
182  if (o.d != d)
183  {
184  if (o.d)
185  o.d->ref.Ref();
186  T *old = d;
187  d = o.d;
188  if (old && !old->ref.Deref())
189  delete old;
190  }
191  return *this;
192  }
193 
195  {
196  if (o != d)
197  {
198  if (o)
199  o->ref.Ref();
200  T *old = d;
201  d = o;
202  if (old && !old->ref.Deref())
203  delete old;
204  }
205  return *this;
206  }
207 
208  inline bool operator!() const { return !d; }
209 
210  inline void Swap(ExplicitlySharedDataPointer& other)
211  {
212  using std::swap;
213  swap(d, other.d);
214  }
215 
216 protected:
217  T* Clone();
218 
219 private:
220  void Detach_helper();
221 
222  T *d;
223 };
224 
225 
226 template <class T>
228 { if (d) d->ref.Ref(); }
229 
230 template <class T>
232 {
233  return new T(*d);
234 }
235 
236 template <class T>
238 {
239  T *x = Clone();
240  x->ref.Ref();
241  if (!d->ref.Deref())
242  delete d;
243  d = x;
244 }
245 
246 template <class T>
248 {
249  return new T(*d);
250 }
251 
252 template <class T>
254 {
255  T *x = Clone();
256  x->ref.Ref();
257  if (!d->ref.Deref())
258  delete d;
259  d = x;
260 }
261 
262 template <class T>
264  : d(adata)
265 { if (d) d->ref.Ref(); }
266 
267 template <class T>
268 void swap(US_PREPEND_NAMESPACE(SharedDataPointer<T)>& p1, US_PREPEND_NAMESPACE(SharedDataPointer<T)>& p2)
269 { p1.Swap(p2); }
270 
271 template <class T>
272 void swap(US_PREPEND_NAMESPACE(ExplicitlySharedDataPointer<T)>& p1, US_PREPEND_NAMESPACE(ExplicitlySharedDataPointer<T)>& p2)
273 { p1.Swap(p2); }
274 
275 US_END_NAMESPACE
276 
277 #endif // USSHAREDDATA_H
ExplicitlySharedDataPointer(const ExplicitlySharedDataPointer< X > &o)
Definition: usSharedData.h:173
void Swap(SharedDataPointer &other)
Definition: usSharedData.h:118
SharedData(const SharedData &)
Definition: usSharedData.h:51
SharedDataPointer< T > & operator=(const SharedDataPointer< T > &o)
Definition: usSharedData.h:88
bool operator==(const SharedDataPointer< T > &other) const
Definition: usSharedData.h:79
bool operator!=(const ExplicitlySharedDataPointer< T > &other) const
Definition: usSharedData.h:161
bool operator!=(const T *ptr) const
Definition: usSharedData.h:163
const T * ConstData() const
Definition: usSharedData.h:77
void Swap(ExplicitlySharedDataPointer &other)
Definition: usSharedData.h:210
SharedDataPointer & operator=(T *o)
Definition: usSharedData.h:102
AtomicInt ref
Definition: usSharedData.h:48
const T * Data() const
Definition: usSharedData.h:76
ExplicitlySharedDataPointer(const ExplicitlySharedDataPointer< T > &o)
Definition: usSharedData.h:169
ExplicitlySharedDataPointer< T > & operator=(const ExplicitlySharedDataPointer< T > &o)
Definition: usSharedData.h:180
bool operator==(const ExplicitlySharedDataPointer< T > &other) const
Definition: usSharedData.h:160
bool operator==(const T *ptr) const
Definition: usSharedData.h:162
const T * operator->() const
Definition: usSharedData.h:72
static void swap(T &x, T &y)
Definition: svm.cpp:58
ExplicitlySharedDataPointer & operator=(T *o)
Definition: usSharedData.h:194
const T & operator*() const
Definition: usSharedData.h:70
SharedDataPointer(const SharedDataPointer< T > &o)
Definition: usSharedData.h:86
bool operator!=(const SharedDataPointer< T > &other) const
Definition: usSharedData.h:80
bool operator!() const
Definition: usSharedData.h:116