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-2016 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 public:
49  struct Destructor {
51  virtual void operator ()(T& MIA_PARAM_UNUSED(data)) const = 0;
52  };
53 
54  struct EmptyDestructor : public Destructor{
56  virtual void operator ()(T& MIA_PARAM_UNUSED(data))const {}
57  };
58 
60 
62 
63  TSingleReferencedObject(T data, const Destructor& d = empty_destructor);
64 
66 
68 
70 
71  operator T()const;
72 
73  unsigned get_refcount()const;
74 
75 private:
76  class TheObject {
77  public:
78  TheObject(T data, const TSingleReferencedObject::Destructor& d);
79  ~TheObject();
80  void add_ref();
81  bool del_ref();
82  T get() const;
83 
84  unsigned get_refcount()const;
85  private:
86  TheObject(const TheObject& data) = delete;
87  TheObject& operator = (const TheObject& data) = delete;
88  T m_data;
89  unsigned m_refcount;
90  const Destructor& m_destructor;
91  };
92  TheObject *m_object;
93 };
94 
95 template <typename T>
97 
98 
99 template <typename T>
101  m_object(nullptr)
102 {
103 }
104 
105 template <typename T>
107 {
108  m_object = new TheObject(data, d);
109 }
110 
111 template <typename T>
113 {
114  m_object = other.m_object;
115  if (m_object)
116  m_object->add_ref();
117 }
118 
119 template <typename T>
121 {
122  if (m_object)
123  m_object->del_ref();
124  m_object = other.m_object;
125  if (m_object)
126  m_object->add_ref();
127  return *this;
128 }
129 
130 template <typename T>
132 {
133  if (m_object)
134  if (m_object->del_ref())
135  delete m_object;
136 }
137 
138 template <typename T>
140 {
141  return m_object->get();
142 }
143 
144 template <typename T>
146 {
147  if (m_object)
148  return m_object->get_refcount();
149  else
150  return 0;
151 }
152 
153 template <typename T>
155  m_data(data),
156  m_refcount(1),
157  m_destructor(d)
158 {
159 }
160 
161 template <typename T>
163 {
164  assert(m_refcount == 0);
165  m_destructor(m_data);
166 }
167 
168 template <typename T>
170 {
171  ++m_refcount;
172 }
173 
174 template <typename T>
176 {
177  --m_refcount;
178  return (m_refcount <= 0);
179 }
180 
181 template <typename T>
183 {
184  return m_refcount;
185 }
186 
187 template <typename T>
189 {
190  return m_data;
191 }
192 
194 #endif
TSingleReferencedObject & operator=(const TSingleReferencedObject< T > &other)
#define NS_MIA_BEGIN
conveniance define to start the mia namespace
Definition: defines.hh:33
virtual void operator()(T &data) const =0
a singulater reference counted object that gets destroyed when the refount goes to zero ...
unsigned get_refcount() const
static const EmptyDestructor empty_destructor
#define NS_MIA_END
conveniance define to end the mia namespace
Definition: defines.hh:36