2017-03-07 09:32:29 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-03-25 23:58:11 +00:00
|
|
|
#include <macro_map.h>
|
2017-03-25 20:32:44 +00:00
|
|
|
#include <optional.h>
|
2017-02-23 08:47:07 +00:00
|
|
|
#include <rapidjson/document.h>
|
|
|
|
#include <rapidjson/prettywriter.h>
|
2017-03-25 20:32:44 +00:00
|
|
|
|
2017-03-14 08:33:39 +00:00
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
2017-02-23 08:47:07 +00:00
|
|
|
|
2017-03-14 08:33:39 +00:00
|
|
|
|
|
|
|
using std::experimental::optional;
|
|
|
|
using std::experimental::nullopt;
|
2017-03-01 08:36:11 +00:00
|
|
|
|
2017-03-07 09:32:29 +00:00
|
|
|
using Reader = rapidjson::GenericValue<rapidjson::UTF8<>>;
|
2017-03-27 04:04:48 +00:00
|
|
|
using Writer = rapidjson::Writer<rapidjson::StringBuffer>;
|
2017-03-07 09:32:29 +00:00
|
|
|
struct IndexedFile;
|
2017-02-23 08:47:07 +00:00
|
|
|
|
2017-03-07 09:32:29 +00:00
|
|
|
#define REFLECT_MEMBER_START() \
|
|
|
|
if (!ReflectMemberStart(visitor, value)) return
|
|
|
|
#define REFLECT_MEMBER_START1(value) \
|
|
|
|
if (!ReflectMemberStart(visitor, value)) return
|
|
|
|
#define REFLECT_MEMBER_END() \
|
|
|
|
ReflectMemberEnd(visitor, value);
|
|
|
|
#define REFLECT_MEMBER_END1(value) \
|
|
|
|
ReflectMemberEnd(visitor, value);
|
|
|
|
#define REFLECT_MEMBER(name) \
|
|
|
|
ReflectMember(visitor, #name, value.name)
|
|
|
|
#define REFLECT_MEMBER2(name, value) \
|
|
|
|
ReflectMember(visitor, name, value)
|
|
|
|
|
|
|
|
|
2017-03-25 22:13:19 +00:00
|
|
|
#define MAKE_REFLECT_TYPE_PROXY(type, as_type) \
|
|
|
|
template<typename TVisitor> \
|
|
|
|
void Reflect(TVisitor& visitor, type& value) { \
|
|
|
|
auto value0 = static_cast<as_type>(value); \
|
|
|
|
Reflect(visitor, value0); \
|
|
|
|
value = static_cast<type>(value0); \
|
|
|
|
}
|
|
|
|
|
2017-03-25 23:58:11 +00:00
|
|
|
#define _MAPPABLE_REFLECT_MEMBER(name) \
|
|
|
|
REFLECT_MEMBER(name);
|
|
|
|
|
|
|
|
#define MAKE_REFLECT_EMPTY_STRUCT(type, ...) \
|
|
|
|
template<typename TVisitor> \
|
|
|
|
void Reflect(TVisitor& visitor, type& value) { \
|
|
|
|
REFLECT_MEMBER_START(); \
|
|
|
|
REFLECT_MEMBER_END(); \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MAKE_REFLECT_STRUCT(type, ...) \
|
|
|
|
template<typename TVisitor> \
|
|
|
|
void Reflect(TVisitor& visitor, type& value) { \
|
|
|
|
REFLECT_MEMBER_START(); \
|
|
|
|
MACRO_MAP(_MAPPABLE_REFLECT_MEMBER, __VA_ARGS__) \
|
|
|
|
REFLECT_MEMBER_END(); \
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-03-26 00:22:06 +00:00
|
|
|
template<typename T>
|
|
|
|
struct NonElidedVector : public std::vector<T> {};
|
|
|
|
|
2017-03-25 23:58:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-03-25 22:13:19 +00:00
|
|
|
|
2017-03-07 09:32:29 +00:00
|
|
|
// API:
|
|
|
|
/*
|
|
|
|
template<typename TVisitor, typename T>
|
|
|
|
void Reflect(TVisitor& visitor, T& value) {
|
|
|
|
static_assert(false, "Missing implementation");
|
|
|
|
}
|
|
|
|
template<typename TVisitor>
|
|
|
|
void DefaultReflectMemberStart(TVisitor& visitor) {
|
|
|
|
static_assert(false, "Missing implementation");
|
|
|
|
}
|
|
|
|
template<typename TVisitor, typename T>
|
|
|
|
bool ReflectMemberStart(TVisitor& visitor, T& value) {
|
|
|
|
static_assert(false, "Missing implementation");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
template<typename TVisitor, typename T>
|
|
|
|
void ReflectMemberEnd(TVisitor& visitor, T& value) {
|
|
|
|
static_assert(false, "Missing implementation");
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2017-03-07 18:01:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
// int
|
|
|
|
void Reflect(Reader& visitor, int& value);
|
|
|
|
void Reflect(Writer& visitor, int& value);
|
|
|
|
// bool
|
|
|
|
void Reflect(Reader& visitor, bool& value);
|
|
|
|
void Reflect(Writer& visitor, bool& value);
|
|
|
|
// std::string
|
|
|
|
void Reflect(Reader& visitor, std::string& value);
|
|
|
|
void Reflect(Writer& visitor, std::string& value);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-03-07 09:32:29 +00:00
|
|
|
// Writer:
|
2017-03-01 08:36:11 +00:00
|
|
|
template<typename T>
|
2017-03-07 09:32:29 +00:00
|
|
|
void Reflect(Writer& visitor, std::vector<T>& values) {
|
|
|
|
visitor.StartArray();
|
|
|
|
for (auto& value : values)
|
|
|
|
Reflect(visitor, value);
|
|
|
|
visitor.EndArray();
|
|
|
|
}
|
|
|
|
template<typename T>
|
|
|
|
void Reflect(Writer& visitor, optional<T> value) {
|
|
|
|
if (value)
|
|
|
|
Reflect(visitor, value.value());
|
|
|
|
}
|
|
|
|
inline void DefaultReflectMemberStart(Writer& visitor) {
|
|
|
|
visitor.StartObject();
|
|
|
|
}
|
|
|
|
template<typename T>
|
|
|
|
bool ReflectMemberStart(Writer& visitor, T& value) {
|
|
|
|
visitor.StartObject();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
template<typename T>
|
|
|
|
void ReflectMemberEnd(Writer& visitor, T& value) {
|
|
|
|
visitor.EndObject();
|
|
|
|
}
|
|
|
|
template<typename T>
|
|
|
|
void ReflectMember(Writer& visitor, const char* name, T& value) {
|
|
|
|
visitor.Key(name);
|
|
|
|
Reflect(visitor, value);
|
|
|
|
}
|
|
|
|
template<typename T>
|
|
|
|
void ReflectMember(Writer& visitor, const char* name, std::vector<T>& values) {
|
|
|
|
if (values.empty())
|
|
|
|
return;
|
|
|
|
visitor.Key(name);
|
|
|
|
visitor.StartArray();
|
|
|
|
for (auto& value : values)
|
|
|
|
Reflect(visitor, value);
|
|
|
|
visitor.EndArray();
|
|
|
|
}
|
|
|
|
template<typename T>
|
2017-03-26 00:22:06 +00:00
|
|
|
void ReflectMember(Writer& visitor, const char* name, NonElidedVector<T>& values) {
|
|
|
|
visitor.Key(name);
|
|
|
|
visitor.StartArray();
|
|
|
|
for (auto& value : values)
|
|
|
|
Reflect(visitor, value);
|
|
|
|
visitor.EndArray();
|
|
|
|
}
|
|
|
|
template<typename T>
|
2017-03-07 09:32:29 +00:00
|
|
|
void ReflectMember(Writer& visitor, const char* name, optional<T>& value) {
|
|
|
|
if (!value)
|
|
|
|
return;
|
|
|
|
visitor.Key(name);
|
|
|
|
Reflect(visitor, value);
|
2017-03-01 08:36:11 +00:00
|
|
|
}
|
2017-03-08 08:09:15 +00:00
|
|
|
void ReflectMember(Writer& visitor, const char* name, std::string& value);
|
2017-03-01 08:36:11 +00:00
|
|
|
|
2017-03-07 09:32:29 +00:00
|
|
|
// Reader:
|
|
|
|
template<typename T>
|
|
|
|
void Reflect(Reader& visitor, std::vector<T>& values) {
|
|
|
|
for (auto& entry : visitor.GetArray()) {
|
|
|
|
T entry_value;
|
|
|
|
Reflect(entry, entry_value);
|
|
|
|
values.push_back(entry_value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
template<typename T>
|
2017-03-08 09:00:39 +00:00
|
|
|
void Reflect(Reader& visitor, optional<T>& value) {
|
2017-03-07 09:32:29 +00:00
|
|
|
T real_value;
|
|
|
|
Reflect(visitor, real_value);
|
|
|
|
value = real_value;
|
|
|
|
}
|
|
|
|
inline void DefaultReflectMemberStart(Reader& visitor) {}
|
|
|
|
template<typename T>
|
|
|
|
bool ReflectMemberStart(Reader& visitor, T& value) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
template<typename T>
|
|
|
|
void ReflectMemberEnd(Reader& visitor, T& value) {}
|
2017-03-01 08:36:11 +00:00
|
|
|
template<typename T>
|
2017-03-07 09:32:29 +00:00
|
|
|
void ReflectMember(Reader& visitor, const char* name, T& value) {
|
|
|
|
auto it = visitor.FindMember(name);
|
|
|
|
if (it != visitor.MemberEnd()) {
|
|
|
|
Reader& child_visitor = it->value;
|
|
|
|
Reflect(child_visitor, value);
|
2017-03-01 08:36:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-07 09:32:29 +00:00
|
|
|
std::string Serialize(IndexedFile& file);
|
2017-04-03 01:34:15 +00:00
|
|
|
optional<IndexedFile> Deserialize(std::string path, std::string serialized);
|