MIRA
PolymorphicPointerReflector.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Contact: info@mira-project.org
9  *
10  * Commercial Usage:
11  * Licensees holding valid commercial licenses may use this file in
12  * accordance with the commercial license agreement provided with the
13  * software or, alternatively, in accordance with the terms contained in
14  * a written agreement between you and MLAB or NICR.
15  *
16  * GNU General Public License Usage:
17  * Alternatively, this file may be used under the terms of the GNU
18  * General Public License version 3.0 as published by the Free Software
19  * Foundation and appearing in the file LICENSE.GPL3 included in the
20  * packaging of this file. Please review the following information to
21  * ensure the GNU General Public License version 3.0 requirements will be
22  * met: http://www.gnu.org/copyleft/gpl.html.
23  * Alternatively you may (at your option) use any later version of the GNU
24  * General Public License if such license has been publicly approved by
25  * MLAB and NICR (or its successors, if any).
26  *
27  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
28  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
29  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
30  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
33  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
34  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
35  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
36  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
37  */
38 
47 #ifndef _MIRA_POLYMORPHICPOINTERREFLECTOR_H_
48 #define _MIRA_POLYMORPHICPOINTERREFLECTOR_H_
49 
50 #include <map>
51 #include <memory>
52 
53 #include <utils/Foreach.h>
54 #include <serialization/VoidCast.h>
55 
56 namespace mira {
57 
59 
64 MIRA_DEFINE_EXCEPTION(XPolymorphicPointerReflectorNotRegistered, XIO)
65 
66 namespace serialization {
68 
70 
75 template <typename Derived>
76 struct PolymorphicPointerReflectorBase
77 {
78  virtual ~PolymorphicPointerReflectorBase() {}
79 
85  virtual void invoke(Derived& serializer, void* pointer) = 0;
86 };
87 
101 template <typename T, typename Derived>
102 struct PolymorphicPointerReflector :
103  public PolymorphicPointerReflectorBase<Derived>
104 {
105  virtual void invoke(Derived& reflector, void* pointer)
106  {
107  // and serialize content recursively
108  reflector.invokePointerObject(*serialization::void_downcast<T>(pointer));
109  }
110 };
111 
124 template <typename Derived>
125 class PolymorphicPointerReflectorRegistrar :
126  public LazySingleton<PolymorphicPointerReflectorRegistrar<Derived> >
127 {
128 public:
129 
131  typedef PolymorphicPointerReflectorBase<Derived>& ReflectorRef;
132 
134  typedef std::unique_ptr<PolymorphicPointerReflectorBase<Derived>> ReflectorPtr;
135 
136 public:
137 
138  ~PolymorphicPointerReflectorRegistrar() {
139 #ifdef MIRA_WINDOWS
140  // If mReflectors is not empty here, some classes were not unregistered
141  // before this singleton is destroyed. Their destructors will be called
142  // when mReflectors is destroyed with its parent object.
143  // However, calling the destructor can be dangerous especially on
144  // Windows, since some necessary dynamic libraries were already
145  // removed from memory for some reason. Since the destructor does not
146  // actually do anything, it is preferable to just omit the deletion
147  // and leak some memory at program end. We achieve that by
148  // releasing ownership of the managed objects.
149  foreach(typename ReflectorMap::value_type& v, mReflectors)
150  v.second.release();
151 #endif
152  }
153 
154 public:
155 
160  template<typename Class>
161  void registerClass()
162  {
163  int typeId = Class::CLASS().getTypeId();
164  if(mReflectors.count(typeId)!=0)
165  return; // we already have registered that class type
166 
167  // add a new pointer serializer
168  mReflectors.insert(std::make_pair(typeId,
169  std::make_unique<PolymorphicPointerReflector<Class, Derived>>()));
170  }
171 
176  template<typename Class>
177  void unregisterClass()
178  {
179  int typeId = Class::CLASS().getTypeId();
180 
181  auto it = mReflectors.find(typeId);
182  if(it==mReflectors.end())
183  return; // we have not registered that class type,
184  // or already unregistered it
185 
186  // remove it from our map (will automatically delete the managed object)
187  mReflectors.erase(it);
188  }
189 
196  ReflectorRef getPolymorphicPointerReflector(int typeId)
197  {
198  auto it = mReflectors.find(typeId);
199  if(it==mReflectors.end())
200  MIRA_THROW(XPolymorphicPointerReflectorNotRegistered,
201  "Type ID " << typeId << " not registered");
202 
203  return *it->second;
204  }
205 
206 private:
207 
209  typedef std::map<int, ReflectorPtr> ReflectorMap;
210 
212  ReflectorMap mReflectors;
213 
214 };
215 
217 
218 } // namespace
220 
222 
223 } // namespace
224 
225 #endif
TypeId typeId()
Generates unique IDs for different types.
Definition: TypeId.h:94
Macro for iterating over all elements in a container.
#define MIRA_DEFINE_EXCEPTION(Ex, Base)
Macro for easily defining a new compatible exception class.
Definition: Exception.h:170
Provides safe casts for casting from a pointer to void* and vice versa while taking care of polymorph...
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
#define MIRA_THROW(ex, msg)
Macro for throwing an exception.
Definition: Exception.h:82