1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
#ifndef ODFAEG_SERIALIZATION_HPP
#define ODFAEG_SERIALIZATION_HPP
#include <iostream>
#include <typeinfo>
#include "tuple.h"
#include <tuple>
#include <map>
#include "fastDelegate.h"
#define REGISTER_DERIVED(ID, TYPE) \
TYPE* create##ID() { \
return new TYPE(); \
} \
friend TYPE* create##ID(); \
public : \
static void getCreateFunc (TYPE*(**func)()) { \
TYPE* create##ID(); \
*func = &create##ID; \
}
#define REGISTER_TYPE(typeName, TYPE) \
using TP = typename std::remove_pointer<decltype(TYPE)>::type; \
TP*(*func)(); \
TP::getCreateFunc(&func); \
register_type(typeName, func);
#define CALL_FUNCTION(typeID, funcName, TYPE, ARGS...) \
using TP = typename std::remove_pointer<decltype(TYPE)>::type; \
TP* derived = dynamic_cast<TP*>(TYPE); \
derived->vt##funcName(ARGS);
namespace odfaeg {
template <typename B>
class BaseFact {
public :
template <typename T>
static void register_type(std::string typeName, T* type) {
REGISTER_TYPE(typeName, type);
}
static void register_type(std::string typeName, B*(*func)()) {
typename std::map<std::string, B*(*)()>::iterator it = types.find(typeName);
if (it == types.end())
types[typeName] = func;
}
static B* create (std::string typeName) {
typename std::map<std::string, B*(*)()>::iterator it = types.find(typeName);
if (it != types.end())
return types[typeName]();
return nullptr;
}
static std::string getTypeName (B* type) {
typename std::map<std::string, B*(*)()>::iterator it = types.find(typeid(type).name());
if (it == types.end())
return it->first;
}
private :
static std::map<std::string, B*(*)()> types;
};
template <class B>
class Serializer : public BaseFact<B> {
public :
Serializer() {
baseObject = nullptr;
}
void setObject(B* baseObject) {
this->baseObject = baseObject;
}
template <typename Archive>
void serialize(Archive & ar) {
CALL_FUNCTION(typeid(*baseObject).name(), serialize, baseObject, ar)
}
B* sallocate(std::string typeName) {
return BaseFact<B>::create(typeName);
}
std::string getTypeName() {
return BaseFact<B>::getTypeName(baseObject);
}
private :
B* baseObject;
};
}
#endif // SERIALIZATION |
Partager