-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArchive.h
177 lines (157 loc) · 4.73 KB
/
Archive.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#pragma once
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
#include <map>
#include <iosfwd>
#include <functional>
#include <typeindex>
/*TODO:
* serialize should be templated or have a different functions ?
* should use the same class for serialize and deSerialize?
* Need to add version
* how to get the type used for serialization
* Serialize ID maybe should be dictionary or by typeof
* add validation of chunks
* */
class Base;
//#todo: check that this is one-to-one dictionary, check how to make it compile time error
static std::map<std::type_index, std::string> typesTags =
{
{std::type_index(typeid(int)), "int"},
{std::type_index(typeid(float)), "float"},
{std::type_index(typeid(std::string)), "string"},
{std::type_index(typeid(std::vector<std::shared_ptr<Base>>)), "ObjectsArray"}
};
//#TODO: serializer shouldn't know about Base
class ISerializer
{
public:
virtual void serializeInt(int i) = 0;
virtual void serializeString(std::string s) = 0;
virtual void serializeFloat(float f) = 0;
virtual void serializeObj(std::shared_ptr<Base> b) = 0;
virtual void serializeObjectsArray(std::vector<std::shared_ptr<Base>> objects) = 0;
};
class TextSerializer : public ISerializer
{
public:
TextSerializer(std::string path);
virtual ~TextSerializer()
{
ofstreamObj.close();
}
virtual void serializeInt(int i);
virtual void serializeString(std::string s);
virtual void serializeFloat(float f);
virtual void serializeObj(std::shared_ptr<Base> b);
virtual void serializeObjectsArray(std::vector<std::shared_ptr<Base>> objects);
private:
std::ofstream ofstreamObj;
};
class IDeserializer
{
public:
//basic types:
virtual int deserializeInt() = 0;
virtual std::string deserializeString() = 0;
virtual float deserializeFloat() = 0;
virtual void deserializeObjectsArray(std::vector<std::shared_ptr<Base>>& objects) = 0;
};
class TextDeserializer : public IDeserializer
{
public:
TextDeserializer(std::string fName);
virtual int deserializeInt();
virtual std::string deserializeString();
virtual float deserializeFloat();
//#TODO:ad-hock code, should use proper xml lib (with safety and performance)
virtual void deserializeObjectsArray(std::vector<std::shared_ptr<Base>>& objects);
//#TODO: don't need this anymore
void ReadXMLChunk(const std::string& chunkName,const char*& argEnd, const std::string& xml, const char*& argStart, std::vector<std::string>& args);
private:
template <class T>
std::string deserializeToString()
{
std::string typeChunk = typesTags[std::type_index(typeid(T))];
it += (size_t(2)/*"<Type>"*/ + typeChunk.length());
auto leftOffset = std::distance(stringXML.begin(), it);
auto rightOffset = stringXML.find_first_of("</", leftOffset) - leftOffset;
std::string s = stringXML.substr(leftOffset, rightOffset);
it += (s.length() + size_t(3)/*"</Type>"*/ + typeChunk.length());
return s;
}
std::string stringXML;
std::string::iterator it;
};
class Base
{
private:
protected:
std::string name;
public:
Base() {}
virtual std::string serializedId() { return "Base"; }
virtual void serialize(ISerializer* serial) const;
static void deserialize(IDeserializer* serial, Base* b);
};
class Point : public Base
{
private:
float x;
float y;
public:
Point() : x(0),y(0) {}
Point(float _x, float _y);
/*ISerializable implementation*/
virtual std::string serializedId() override { return "Point"; }
virtual void serialize(ISerializer* serial) const override;
static void deserialize(IDeserializer* serial, Base* b);
};
class Circle : public Point
{
private:
int r;
public:
Circle() : r(0) {}
Circle(float _x, float _y, int _r) : Point(_x, _y), r(_r) {}
/*ISerializable implementation*/
virtual std::string serializedId()
{
return "Circle";
}
virtual void serialize(ISerializer* serial) const override;
static void deserialize(IDeserializer* serial, Base* b);
};
static std::map<std::string, std::function<Base* ()>> typesMap =
{
{"Base", [] {return new Base; }} ,
{"Point", [] {return new Point; }},
{"Circle", [] {return new Circle; }}
};
static std::map<std::string, std::function<void (IDeserializer*, Base*)>> deserializeMap =
{
{"Base", Base::deserialize},
{"Point", Point::deserialize},
{"Circle", Circle::deserialize}
};
class OutArchive
{
public:
OutArchive(std::string fName);
void write(std::vector<std::shared_ptr<Base>> objects);
private:
std::unique_ptr<ISerializer> iSerializer;
};
class InArchive
{
public:
InArchive(std::string fName)
{
iSerializer.reset(new TextDeserializer(fName));
}
void read(std::vector<std::shared_ptr<Base>>& objects);
private:
std::unique_ptr<IDeserializer> iSerializer;
};