My Project
singular_refobj.hh
Go to the documentation of this file.
1/* -*- mia-c++ -*-
2 *
3 * This file is part of MIA - a toolbox for medical image analysis
4 * Copyright (c) Leipzig, Madrid 1999-2017 Gert Wollny
5 *
6 * MIA is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with MIA; if not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21#ifndef mia_singular_refobj_hh
22#define mia_singular_refobj_hh
23
24
25#include <mia/core/defines.hh>
26#include <cassert>
27
29
30
31
46template <typename T>
48{
49public:
50 struct Destructor {
52 virtual void operator ()(T& MIA_PARAM_UNUSED(data)) const = 0;
53 };
54
55 struct EmptyDestructor : public Destructor {
57 virtual void operator ()(T& MIA_PARAM_UNUSED(data))const {}
58 };
59
61
63
65
67
69
71
73
74
76
77 operator T()const;
78
79 unsigned get_refcount()const;
80
81private:
82 class TheObject
83 {
84 public:
85 TheObject(T data, const TSingleReferencedObject::Destructor& d);
86 ~TheObject();
87 void add_ref();
88 bool del_ref();
89 T get() const;
90
91 unsigned get_refcount()const;
92 private:
93 TheObject(const TheObject& data) = delete;
94 TheObject& operator = (const TheObject& data) = delete;
95 T m_data;
96 unsigned m_refcount;
97 const Destructor& m_destructor;
98 };
99 TheObject *m_object;
100};
101
102template <typename T>
104
105
106template <typename T>
111
112template <typename T>
114{
115 m_object = new TheObject(data, d);
116}
117
118template <typename T>
120{
121 m_object = other.m_object;
122
123 if (m_object)
124 m_object->add_ref();
125}
126
127template <typename T>
129{
130 if (m_object)
131 m_object->del_ref();
132
133 m_object = other.m_object;
134
135 if (m_object)
136 m_object->add_ref();
137
138 return *this;
139}
140
141template <typename T>
143 m_object(other.m_object)
144{
145 other.m_object = nullptr;
146}
147
148template <typename T>
150{
151 if (&other != this) {
152 if (m_object)
153 m_object->del_ref();
154
155 m_object = other.m_object;
156 other.m_object = nullptr;
157 }
158
159 return *this;
160}
161
162
163template <typename T>
165{
166 if (m_object)
167 if (m_object->del_ref())
168 delete m_object;
169}
170
171template <typename T>
173{
174 return m_object->get();
175}
176
177template <typename T>
179{
180 if (m_object)
181 return m_object->get_refcount();
182 else
183 return 0;
184}
185
186template <typename T>
187TSingleReferencedObject<T>::TheObject::TheObject(T data, const Destructor& d):
188 m_data(data),
189 m_refcount(1),
190 m_destructor(d)
191{
192}
193
194template <typename T>
196{
197 assert(m_refcount == 0);
198 m_destructor(m_data);
199}
200
201template <typename T>
203{
204 ++m_refcount;
205}
206
207template <typename T>
209{
210 --m_refcount;
211 return (m_refcount <= 0);
212}
213
214template <typename T>
216{
217 return m_refcount;
218}
219
220template <typename T>
222{
223 return m_data;
224}
225
227#endif
a singulater reference counted object that gets destroyed when the refount goes to zero
static const EmptyDestructor empty_destructor
unsigned get_refcount() const
TSingleReferencedObject & operator=(const TSingleReferencedObject< T > &other)
#define NS_MIA_BEGIN
conveniance define to start the mia namespace
Definition defines.hh:33
#define NS_MIA_END
conveniance define to end the mia namespace
Definition defines.hh:36
virtual void operator()(T &data) const =0
virtual void operator()(T &data) const