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 
46 template <typename T>
48 {
49 public:
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 
81 private:
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 
102 template <typename T>
104 
105 
106 template <typename T>
108  m_object(nullptr)
109 {
110 }
111 
112 template <typename T>
114 {
115  m_object = new TheObject(data, d);
116 }
117 
118 template <typename T>
120 {
121  m_object = other.m_object;
122 
123  if (m_object)
124  m_object->add_ref();
125 }
126 
127 template <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 
141 template <typename T>
143  m_object(other.m_object)
144 {
145  other.m_object = nullptr;
146 }
147 
148 template <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 
163 template <typename T>
165 {
166  if (m_object)
167  if (m_object->del_ref())
168  delete m_object;
169 }
170 
171 template <typename T>
173 {
174  return m_object->get();
175 }
176 
177 template <typename T>
179 {
180  if (m_object)
181  return m_object->get_refcount();
182  else
183  return 0;
184 }
185 
186 template <typename T>
187 TSingleReferencedObject<T>::TheObject::TheObject(T data, const Destructor& d):
188  m_data(data),
189  m_refcount(1),
190  m_destructor(d)
191 {
192 }
193 
194 template <typename T>
196 {
197  assert(m_refcount == 0);
198  m_destructor(m_data);
199 }
200 
201 template <typename T>
203 {
204  ++m_refcount;
205 }
206 
207 template <typename T>
209 {
210  --m_refcount;
211  return (m_refcount <= 0);
212 }
213 
214 template <typename T>
216 {
217  return m_refcount;
218 }
219 
220 template <typename T>
222 {
223  return m_data;
224 }
225 
227 #endif
NS_MIA_BEGIN
#define NS_MIA_BEGIN
conveniance define to start the mia namespace
Definition: defines.hh:33
TSingleReferencedObject::get_refcount
unsigned get_refcount() const
Definition: singular_refobj.hh:178
TSingleReferencedObject::Destructor::operator()
virtual void operator()(T &data) const =0
NS_MIA_END
#define NS_MIA_END
conveniance define to end the mia namespace
Definition: defines.hh:36
TSingleReferencedObject::EmptyDestructor::operator()
virtual void operator()(T &data) const
Definition: singular_refobj.hh:57
TSingleReferencedObject::operator=
TSingleReferencedObject & operator=(const TSingleReferencedObject< T > &other)
Definition: singular_refobj.hh:128
TSingleReferencedObject::EmptyDestructor::EmptyDestructor
EmptyDestructor()
Definition: singular_refobj.hh:56
TSingleReferencedObject::Destructor::Destructor
Destructor()
Definition: singular_refobj.hh:51
TSingleReferencedObject::Destructor
Definition: singular_refobj.hh:50
TSingleReferencedObject::empty_destructor
static const EmptyDestructor empty_destructor
Definition: singular_refobj.hh:60
TSingleReferencedObject::EmptyDestructor
Definition: singular_refobj.hh:55
TSingleReferencedObject
a singulater reference counted object that gets destroyed when the refount goes to zero
Definition: singular_refobj.hh:48
TSingleReferencedObject::TSingleReferencedObject
TSingleReferencedObject()
Definition: singular_refobj.hh:107
defines.hh
TSingleReferencedObject::~TSingleReferencedObject
~TSingleReferencedObject()
Definition: singular_refobj.hh:164