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 
140 public:
141 
146  template<typename Class>
147  void registerClass()
148  {
149  int typeId = Class::CLASS().getTypeId();
150  if(mReflectors.count(typeId)!=0)
151  return; // we already have registered that class type
152 
153  // add a new pointer serializer
154  mReflectors.insert(std::make_pair(typeId,
155  std::make_unique<PolymorphicPointerReflector<Class, Derived>>()));
156  }
157 
162  template<typename Class>
163  void unregisterClass()
164  {
165  int typeId = Class::CLASS().getTypeId();
166 
167  auto it = mReflectors.find(typeId);
168  if(it==mReflectors.end())
169  return; // we have not registered that class type,
170  // or already unregistered it
171 
172  // remove it from our map (will automatically delete the managed object)
173  mReflectors.erase(it);
174  }
175 
182  ReflectorRef getPolymorphicPointerReflector(int typeId)
183  {
184  auto it = mReflectors.find(typeId);
185  if(it==mReflectors.end())
186  MIRA_THROW(XPolymorphicPointerReflectorNotRegistered,
187  "Type ID " << typeId << " not registered");
188 
189  return *it->second;
190  }
191 
192 private:
193 
195  typedef std::map<int, ReflectorPtr> ReflectorMap;
196 
198  ReflectorMap mReflectors;
199 
200 };
201 
203 
204 } // namespace
206 
208 
209 } // namespace
210 
211 #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:166
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:78