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 
52 #include <utils/Foreach.h>
53 #include <serialization/VoidCast.h>
54 
55 namespace mira {
56 
58 
60 namespace serialization {
61 
63 
68 template <typename Derived>
69 struct PolymorphicPointerReflectorBase
70 {
71  virtual ~PolymorphicPointerReflectorBase() {}
72 
78  virtual void invoke(Derived& serializer, void* pointer) = 0;
79 };
80 
94 template <typename T, typename Derived>
95 struct PolymorphicPointerReflector :
96  public PolymorphicPointerReflectorBase<Derived>
97 {
98  virtual void invoke(Derived& reflector, void* pointer)
99  {
100  // and serialize content recursively
101  reflector.invokePointerObject(*serialization::void_downcast<T>(pointer));
102  }
103 };
104 
117 template <typename Derived>
118 class PolymorphicPointerReflectorRegistrar :
119  public LazySingleton<PolymorphicPointerReflectorRegistrar<Derived> >
120 {
121 public:
122 
124  typedef PolymorphicPointerReflectorBase<Derived>* ReflectorPtr;
125 
126 public:
127 
128  ~PolymorphicPointerReflectorRegistrar() {
129 
130  if(mReflectors.size()>0) {
131  // If we reach here, some classes were not unregistered before this
132  // singleton is destroyed. We could life with that since it is
133  // not a growing memory leak and the memory will be released upon
134  // the termination of the process. However, to make tools like
135  // valgrind happy, we release the memory, but we do NOT call the
136  // proper destructor (therefore the char* cast). Calling the
137  // destructor can be dangerous especially on Windows, since some
138  // necessary dynamic libraries were already removed from memory
139  // for some reason. This is somewhat hackish, but no problem at all
140  // since the destructors do not do anything.
141  // Maybe we find a better solution some day, but this has very low
142  // priority.
143  foreach(typename ReflectorMap::value_type& v, mReflectors)
144  delete (char*)v.second;
145  }
146  mReflectors.clear();
147  }
148 
149 public:
150 
155  template<typename Class>
156  void registerClass()
157  {
158  int typeId = Class::CLASS().getTypeId();
159  if(mReflectors.count(typeId)!=0)
160  return; // we already have registered that class type
161 
162  // add a new pointer serializer
163  mReflectors.insert(std::make_pair(typeId,
164  new PolymorphicPointerReflector<Class, Derived>) );
165  }
166 
171  template<typename Class>
172  void unregisterClass()
173  {
174  int typeId = Class::CLASS().getTypeId();
175 
176  auto it = mReflectors.find(typeId);
177  if(it==mReflectors.end())
178  return; // we have not registered that class type,
179  // or already unregistered it
180 
181  // delete the reflector
182  delete it->second;
183 
184  // and remove it from our map
185  mReflectors.erase(it);
186  }
187 
189  ReflectorPtr getPolymorphicPointerReflector(int typeId)
190  {
191  typename ReflectorMap::iterator i = mReflectors.find(typeId);
192  if(mReflectors.find(typeId)==mReflectors.end())
193  return ReflectorPtr(); // NULL
194  return i->second;
195  }
196 
197 private:
198 
200  typedef std::map<int, ReflectorPtr > ReflectorMap;
201 
203  ReflectorMap mReflectors;
204 
205 };
206 
208 
209 } // namespace
211 
213 
214 } // namespace
215 
216 #endif
TypeId typeId()
Generates unique IDs for different types.
Definition: TypeId.h:94
Macro for iterating over all elements in a container.
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