47 #ifndef _MIRA_POLYMORPHICPOINTERREFLECTOR_H_ 48 #define _MIRA_POLYMORPHICPOINTERREFLECTOR_H_ 66 namespace serialization {
75 template <
typename Derived>
76 struct PolymorphicPointerReflectorBase
78 virtual ~PolymorphicPointerReflectorBase() {}
85 virtual void invoke(Derived& serializer,
void* pointer) = 0;
101 template <
typename T,
typename Derived>
102 struct PolymorphicPointerReflector :
103 public PolymorphicPointerReflectorBase<Derived>
105 virtual void invoke(Derived& reflector,
void* pointer)
108 reflector.invokePointerObject(*serialization::void_downcast<T>(pointer));
124 template <
typename Derived>
125 class PolymorphicPointerReflectorRegistrar :
126 public LazySingleton<PolymorphicPointerReflectorRegistrar<Derived> >
131 typedef PolymorphicPointerReflectorBase<Derived>& ReflectorRef;
134 typedef std::unique_ptr<PolymorphicPointerReflectorBase<Derived>> ReflectorPtr;
138 ~PolymorphicPointerReflectorRegistrar() {
149 foreach(
typename ReflectorMap::value_type& v, mReflectors)
160 template<
typename Class>
163 int typeId = Class::CLASS().getTypeId();
164 if(mReflectors.count(
typeId)!=0)
168 mReflectors.insert(std::make_pair(
typeId,
169 std::make_unique<PolymorphicPointerReflector<Class, Derived>>()));
176 template<
typename Class>
177 void unregisterClass()
179 int typeId = Class::CLASS().getTypeId();
181 auto it = mReflectors.find(
typeId);
182 if(it==mReflectors.end())
187 mReflectors.erase(it);
196 ReflectorRef getPolymorphicPointerReflector(
int typeId)
198 auto it = mReflectors.find(
typeId);
199 if(it==mReflectors.end())
200 MIRA_THROW(XPolymorphicPointerReflectorNotRegistered,
201 "Type ID " <<
typeId <<
" not registered");
209 typedef std::map<int, ReflectorPtr> ReflectorMap;
212 ReflectorMap mReflectors;
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