Medical Imaging Interaction Toolkit  2024.06.99-b3904305
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  https://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 (https://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 
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>
269 { p1.Swap(p2); }
270 
271 template <class T>
273 { p1.Swap(p2); }
274 
276 
277 #endif // USSHAREDDATA_H
us::SharedDataPointer::operator!
bool operator!() const
Definition: usSharedData.h:116
us::SharedDataPointer::pointer
T * pointer
Definition: usSharedData.h:66
us::ExplicitlySharedDataPointer::operator==
bool operator==(const ExplicitlySharedDataPointer< T > &other) const
Definition: usSharedData.h:160
us::SharedData::SharedData
SharedData()
Definition: usSharedData.h:50
us::ExplicitlySharedDataPointer::ExplicitlySharedDataPointer
ExplicitlySharedDataPointer(const ExplicitlySharedDataPointer< T > &o)
Definition: usSharedData.h:169
us::ExplicitlySharedDataPointer::operator*
T & operator*() const
Definition: usSharedData.h:142
us::SharedDataPointer::operator*
T & operator*()
Definition: usSharedData.h:69
us::swap
void swap(us::ExplicitlySharedDataPointer< T > &p1, us::ExplicitlySharedDataPointer< T > &p2)
Definition: usSharedData.h:272
us::SharedDataPointer::operator==
bool operator==(const SharedDataPointer< T > &other) const
Definition: usSharedData.h:79
us::ExplicitlySharedDataPointer::ExplicitlySharedDataPointer
ExplicitlySharedDataPointer()
Definition: usSharedData.h:165
us::ExplicitlySharedDataPointer::operator!
bool operator!() const
Definition: usSharedData.h:208
us::ExplicitlySharedDataPointer::Type
T Type
Definition: usSharedData.h:139
us::ExplicitlySharedDataPointer::ExplicitlySharedDataPointer
ExplicitlySharedDataPointer(const ExplicitlySharedDataPointer< X > &o)
Definition: usSharedData.h:173
us::ExplicitlySharedDataPointer::operator!=
bool operator!=(const ExplicitlySharedDataPointer< T > &other) const
Definition: usSharedData.h:161
us::SharedDataPointer::Swap
void Swap(SharedDataPointer &other)
Definition: usSharedData.h:118
us::SharedDataPointer::Data
T * Data()
Definition: usSharedData.h:75
us::ExplicitlySharedDataPointer
Definition: usSharedData.h:136
us::SharedDataPointer::operator=
SharedDataPointer< T > & operator=(const SharedDataPointer< T > &o)
Definition: usSharedData.h:88
us::ExplicitlySharedDataPointer::operator=
ExplicitlySharedDataPointer & operator=(T *o)
Definition: usSharedData.h:194
us::SharedDataPointer::ConstData
const T * ConstData() const
Definition: usSharedData.h:77
us::SharedData
Definition: usSharedData.h:45
us::ExplicitlySharedDataPointer::operator!=
bool operator!=(const T *ptr) const
Definition: usSharedData.h:163
us::SharedDataPointer::Type
T Type
Definition: usSharedData.h:65
us::SharedDataPointer::~SharedDataPointer
~SharedDataPointer()
Definition: usSharedData.h:83
us::SharedDataPointer::operator=
SharedDataPointer & operator=(T *o)
Definition: usSharedData.h:102
us::SharedDataPointer::Data
const T * Data() const
Definition: usSharedData.h:76
us::SharedDataPointer::SharedDataPointer
SharedDataPointer()
Definition: usSharedData.h:82
us::ExplicitlySharedDataPointer::ConstData
const T * ConstData() const
Definition: usSharedData.h:146
US_BEGIN_NAMESPACE
#define US_BEGIN_NAMESPACE
Definition: usGlobalConfig.h:76
us::ExplicitlySharedDataPointer::Swap
void Swap(ExplicitlySharedDataPointer &other)
Definition: usSharedData.h:210
us::SharedDataPointer::operator!=
bool operator!=(const SharedDataPointer< T > &other) const
Definition: usSharedData.h:80
us::SharedDataPointer::operator*
const T & operator*() const
Definition: usSharedData.h:70
us::ExplicitlySharedDataPointer::operator->
T * operator->() const
Definition: usSharedData.h:144
us::SharedDataPointer::Detach
void Detach()
Definition: usSharedData.h:68
us::ExplicitlySharedDataPointer::operator->
T * operator->()
Definition: usSharedData.h:143
us::SharedDataPointer::operator->
const T * operator->() const
Definition: usSharedData.h:72
us::SharedDataPointer::SharedDataPointer
SharedDataPointer(const SharedDataPointer< T > &o)
Definition: usSharedData.h:86
us::SharedData::ref
AtomicInt ref
Definition: usSharedData.h:48
us::SharedDataPointer::operator->
T * operator->()
Definition: usSharedData.h:71
US_END_NAMESPACE
#define US_END_NAMESPACE
Definition: usGlobalConfig.h:77
US_PREPEND_NAMESPACE
#define US_PREPEND_NAMESPACE(name)
Definition: usGlobalConfig.h:74
us::ExplicitlySharedDataPointer::operator==
bool operator==(const T *ptr) const
Definition: usSharedData.h:162
us::ExplicitlySharedDataPointer::operator=
ExplicitlySharedDataPointer< T > & operator=(const ExplicitlySharedDataPointer< T > &o)
Definition: usSharedData.h:180
us::SharedData::SharedData
SharedData(const SharedData &)
Definition: usSharedData.h:51
us::ExplicitlySharedDataPointer::Detach
void Detach()
Definition: usSharedData.h:148
us::ExplicitlySharedDataPointer::Reset
void Reset()
Definition: usSharedData.h:150
us::SharedDataPointer
Definition: usSharedData.h:62
us::ExplicitlySharedDataPointer::pointer
T * pointer
Definition: usSharedData.h:140
us::ExplicitlySharedDataPointer::~ExplicitlySharedDataPointer
~ExplicitlySharedDataPointer()
Definition: usSharedData.h:166
us::ExplicitlySharedDataPointer::Data
T * Data() const
Definition: usSharedData.h:145