summaryrefslogtreecommitdiff
path: root/test/test-persistance.cpp
blob: 7d6667525ea3533b0d1e674142132a613fb7f745 (plain)
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#define BOOST_TEST_MODULE test_persistance

#include <boost/test/unit_test.hpp>

#include <gfx/models/vertex.hpp>
#include <glm/glm.hpp>
#include <jsonParse.h>
#include <maths.h>
#include <memory>
#include <stack>
#include <stream_support.hpp>
#include <utility>
#include <variant>
#include <vector>

struct PersistanceStore;
struct Selection;
using SelectionPtr = std::unique_ptr<Selection>;
struct Selection {
	virtual ~Selection() = default;
	virtual void
	operator()(float &)
	{
		throw std::runtime_error("Unexpected float");
	}
	virtual void
	operator()(bool &)
	{
		throw std::runtime_error("Unexpected bool");
	}
	virtual void
	operator()(const std::nullptr_t &)
	{
		throw std::runtime_error("Unexpected null");
	}
	virtual void
	operator()(std::string &)
	{
		throw std::runtime_error("Unexpected string");
	}
	virtual void
	BeginArray()
	{
		throw std::runtime_error("Unexpected array");
	}
	virtual SelectionPtr
	BeginObject()
	{
		throw std::runtime_error("Unexpected object");
	}
	virtual SelectionPtr select(std::string)
	{
		throw std::runtime_error("Unexpected persist");
	}
};

template<typename T> struct SelectionT : public Selection {
	SelectionT(T & value) : v {value} { }
	void
	operator()(T & evalue) override
	{
		std::swap(v, evalue);
	}
	T & v;
};

template<glm::length_t L, glm::qualifier Q> struct SelectionT<glm::vec<L, float, Q>> : public Selection {
	SelectionT(glm::vec3 & value) : v {value}
	{
		CLOG(__PRETTY_FUNCTION__);
	}
	void
	operator()(float & value) override
	{
		v[idx++] = value;
	}
	void
	BeginArray() override
	{
	}
	glm::vec3 & v;
	glm::vec3::length_type idx {0};
};

template<typename T> struct SelectionT<std::vector<T>> : public Selection {
	SelectionT(std::vector<T> & value) : v {value} { }
	void
	operator()(T & value) override
	{
		v.push_back(value);
	}
	void
	BeginArray()
	{
	}
	std::vector<T> & v;
};

struct TestObject;

template<typename Ptr> struct MakeObjectByTypeName : public Selection {
	MakeObjectByTypeName(Ptr & o) : o {o} { }
	virtual void
	operator()(std::string &)
	{
		o = std::make_unique<TestObject>(); // TODO shared_ptr
	}
	Ptr & o;
};

struct PersistanceStore {
	// virtual bool persistType(const std::type_info &) = 0;
	template<typename T>
	bool
	persistValue(const std::string_view & key, T & value)
	{
		if (key == name) {
			sel = std::make_unique<SelectionT<T>>(std::ref(value));
			return false;
		}
		return true;
	}
	const std::string & name;
	std::unique_ptr<Selection> sel {};
};

template<typename Ptr> struct SelectionObj : public Selection {
	SelectionObj(Ptr & o) : v {o} { }

	SelectionPtr
	select(std::string mbr) override
	{
		if (mbr == "@typeid") {
			return std::make_unique<MakeObjectByTypeName<Ptr>>(std::ref(v));
		}
		else if (v) {
			CLOG(mbr);
			PersistanceStore ps {mbr};
			v->persist(ps);
			return std::move(ps.sel);
		}
		throw std::runtime_error("cannot select member of null object");
	}

	Ptr & v;
};

template<typename T> struct SelectionT<std::unique_ptr<T>> : public Selection {
	SelectionT(std::unique_ptr<T> & o) : v {o} { }
	SelectionPtr
	BeginObject() override
	{
		CLOG(__PRETTY_FUNCTION__);
		return std::make_unique<SelectionObj<std::unique_ptr<T>>>(v);
	}
	std::unique_ptr<T> & v;
};

struct Persistable {
	virtual void persist(PersistanceStore & store) = 0;
};

struct JsonLoadPersistanceStore : public json::jsonParser {
	JsonLoadPersistanceStore(std::istream & in) : json::jsonParser {&in} { }

	std::stack<std::unique_ptr<Selection>> stk;
	std::unique_ptr<Selection> selection;

	template<typename T>
	void
	loadState(std::unique_ptr<T> & t)
	{
		yy_push_state(0);
		stk.push(std::make_unique<SelectionT<std::unique_ptr<T>>>(std::ref(t)));
		yylex();
	}
	void
	BeginObject() override
	{
		CLOG(__FUNCTION__);
		stk.push(stk.top()->BeginObject());
	}
	void
	BeginArray() override
	{
		CLOG(__FUNCTION__);
		selection->BeginArray();
	}
	void
	PushBoolean(bool value) override
	{
		CLOG(__FUNCTION__);
		(*selection)(value);
	}
	void
	PushNumber(float value) override
	{
		CLOG(__FUNCTION__);
		(*selection)(value);
	}
	void
	PushNull() override
	{
		CLOG(__FUNCTION__);
		(*selection)(nullptr);
	}
	void
	PushText(std::string && value) override
	{
		CLOG(__FUNCTION__);
		(*selection)(value);
	}
	void
	PushKey(std::string && k) override
	{
		CLOG(__FUNCTION__);
		selection = stk.top()->select(std::move(k));
	}
	void
	EndArray() override
	{
		CLOG(__FUNCTION__);
	}
	void
	EndObject() override
	{
		CLOG(__FUNCTION__);
		stk.pop();
	}
};

#define STORE_TYPE store.persistType(typeid(*this))
#define STORE_MEMBER(mbr) store.persistValue(#mbr##sv, mbr)

struct TestObject : public Persistable {
	TestObject() = default;

	float flt {};
	std::string str {};
	bool bl {};
	glm::vec3 pos {};
	std::vector<float> flts;

	void
	persist(PersistanceStore & store) override
	{
		using namespace std::literals;
		STORE_MEMBER(flt) && STORE_MEMBER(str) && STORE_MEMBER(bl) && STORE_MEMBER(pos) && STORE_MEMBER(flts);
	}
};

const std::string input(R"J({
	"@typeid": "TestObject",
	"flt": 3.14,
	"str": "Lovely string",
	"bl": true,
	"pos": [3.14, 6.28, 1.57],
	"flts": [3.14, 6.28, 1.57, 0, -1, -3.14],
})J");

BOOST_AUTO_TEST_CASE(load_object)
{
	std::stringstream ss {input};
	JsonLoadPersistanceStore jlps {ss};
	std::unique_ptr<TestObject> to;
	jlps.loadState(to);

	BOOST_CHECK_CLOSE(to->flt, 3.14, 0.01);
	BOOST_CHECK_EQUAL(to->str, "Lovely string");
	BOOST_CHECK_EQUAL(to->bl, true);
	BOOST_CHECK_CLOSE(to->pos[0], 3.14, 0.01);
	BOOST_CHECK_CLOSE(to->pos[1], 6.28, 0.01);
	BOOST_CHECK_CLOSE(to->pos[2], 1.57, 0.01);
	BOOST_REQUIRE_EQUAL(to->flts.size(), 6);
	BOOST_CHECK_CLOSE(to->flts[0], 3.14, 0.01);
	BOOST_CHECK_CLOSE(to->flts[1], 6.28, 0.01);
	BOOST_CHECK_CLOSE(to->flts[2], 1.57, 0.01);
	BOOST_CHECK_CLOSE(to->flts[3], 0, 0.01);
	BOOST_CHECK_CLOSE(to->flts[4], -1, 0.01);
	BOOST_CHECK_CLOSE(to->flts[5], -3.14, 0.01);
}