forked from ChrisRyan98008/NwCpp-May2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSerializable.h
61 lines (52 loc) · 1.78 KB
/
Serializable.h
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
#pragma once
#include <map>
#include <memory>
#include "types.h"
namespace Serialize {
class SerializableBase;
class TypeInfo
{
static auto& Map() { static std::map<HASH, TypeInfo*> map; return map; }
using PFNCreate = SerializableBase * (*)();
public:
TypeInfo(const size_t hash, PFNCreate pfnCreate)
: _hash(HASH(hash)), _pfnCreate(pfnCreate) { Map()[_hash] = this; }
SerializableBase* Create() const { return _pfnCreate(); }
const HASH Hash() const { return _hash; };
static TypeInfo* Find(HASH hash) { return Map()[hash]; }
private:
HASH _hash;
PFNCreate _pfnCreate;
};
class Archive;
class SerializableBase
{
friend class Archive;
protected:
virtual ~SerializableBase() {};
virtual void Serialize(Archive& arc) {}
virtual const TypeInfo* GetTypeInfo() const { return nullptr; }
virtual bool IsOfType(HASH hash) const { return false; }
public:
using shared_ptr = std::shared_ptr<SerializableBase>;
};
template<class Type, class Base = SerializableBase>
class Serializable : public Base
{
friend class Archive;
static const TypeInfo s_typeinfo;
static SerializableBase* Create() { return new Type; }
virtual const TypeInfo* GetTypeInfo() const { return &s_typeinfo; }
protected:
virtual bool IsOfType(HASH hash) const
{
if(s_typeinfo.Hash() != hash)
return Base::IsOfType(hash);
return true;
}
protected:
template<typename... Types> Serializable(Types&& ...args) : Base(args ...) {}
};
template<class Type, class Base>
const TypeInfo Serializable<Type, Base>::s_typeinfo(typeid(Type).hash_code(), Create);
} //namespace Serialize