#pragma once #include #include #include #include #include "optional.h" using std::experimental::optional; using std::experimental::nullopt; using Reader = rapidjson::GenericValue>; using Writer = rapidjson::PrettyWriter; struct IndexedFile; #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) // API: /* template void Reflect(TVisitor& visitor, T& value) { static_assert(false, "Missing implementation"); } template void DefaultReflectMemberStart(TVisitor& visitor) { static_assert(false, "Missing implementation"); } template bool ReflectMemberStart(TVisitor& visitor, T& value) { static_assert(false, "Missing implementation"); return true; } template void ReflectMemberEnd(TVisitor& visitor, T& value) { static_assert(false, "Missing implementation"); } */ // 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); // Writer: template void Reflect(Writer& visitor, std::vector& values) { visitor.StartArray(); for (auto& value : values) Reflect(visitor, value); visitor.EndArray(); } template void Reflect(Writer& visitor, optional value) { if (value) Reflect(visitor, value.value()); } inline void DefaultReflectMemberStart(Writer& visitor) { visitor.StartObject(); } template bool ReflectMemberStart(Writer& visitor, T& value) { visitor.StartObject(); return true; } template void ReflectMemberEnd(Writer& visitor, T& value) { visitor.EndObject(); } template void ReflectMember(Writer& visitor, const char* name, T& value) { visitor.Key(name); Reflect(visitor, value); } template void ReflectMember(Writer& visitor, const char* name, std::vector& values) { if (values.empty()) return; visitor.Key(name); visitor.StartArray(); for (auto& value : values) Reflect(visitor, value); visitor.EndArray(); } template void ReflectMember(Writer& visitor, const char* name, optional& value) { if (!value) return; visitor.Key(name); Reflect(visitor, value); } void ReflectMember(Writer& visitor, const char* name, std::string& value); // Reader: template void Reflect(Reader& visitor, std::vector& values) { for (auto& entry : visitor.GetArray()) { T entry_value; Reflect(entry, entry_value); values.push_back(entry_value); } } template void Reflect(Reader& visitor, optional& value) { T real_value; Reflect(visitor, real_value); value = real_value; } inline void DefaultReflectMemberStart(Reader& visitor) {} template bool ReflectMemberStart(Reader& visitor, T& value) { return true; } template void ReflectMemberEnd(Reader& visitor, T& value) {} template 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); } } #if false void Serialize(Writer& writer, int value); void Serialize(Writer& writer, const std::string& value); void Serialize(Writer& writer, Location location); void Serialize(Writer& writer, uint64_t value); void Serialize(Writer& writer, IndexedFile& file); template void Serialize(Writer& writer, Id id) { writer.Uint64(id.id); } template void Serialize(Writer& writer, optional value) { if (value) Serialize(writer, value.value()); else writer.Null(); } template void Serialize(Writer& writer, const std::vector& values) { writer.StartArray(); for (const T& value : values) Serialize(writer, value); writer.EndArray(); } template void Serialize(Writer& writer, Ref ref) { std::string s = std::to_string(ref.id.id) + "@" + ref.loc.ToString(); writer.String(s.c_str()); } template void Serialize(Writer& writer, const char* key, const std::vector>& refs) { if (refs.size() == 0) return; if (key) writer.Key(key); writer.StartArray(); for (Ref ref : refs) Serialize(writer, nullptr, ref); writer.EndArray(); } #define SERIALIZE_MEMBER(name) \ SerializeMember(writer, #name, value.name) #define SERIALIZE_MEMBER2(name, value) \ SerializeMember(writer, name, value) #define DESERIALIZE_MEMBER(name) \ DeserializeMember(reader, #name, value.name) #define DESERIALIZE_MEMBER2(name, value) \ DeserializeMember(reader, name, value) // Special templates used by (DE)SERIALIZE_MEMBER macros. template void SerializeMember(Writer& writer, const char* name, const T& value) { writer.Key(name); Serialize(writer, value); } template void SerializeMember(Writer& writer, const char* name, const std::vector& value) { if (value.empty()) return; writer.Key(name); Serialize(writer, value); } template void SerializeMember(Writer& writer, const char* name, const optional& value) { if (!value) return; writer.Key(name); Serialize(writer, value.value()); } void SerializeMember(Writer& writer, const char* name, const std::string& value); template void DeserializeMember(const Reader& reader, const char* name, T& value) { auto it = reader.FindMember(name); if (it != reader.MemberEnd()) Deserialize(it->value, value); } template void Deserialize(const Reader& reader, Id& output) { output = Id(reader.GetUint64()); } template void Deserialize(const Reader& reader, Ref& output) { const char* str_value = reader.GetString(); uint64_t id = atoi(str_value); const char* loc_string = strchr(str_value, '@') + 1; output.id = Id(id); output.loc = Location(loc_string); } template void Deserialize(const Reader& reader, std::vector& value) { for (const auto& entry : reader.GetArray()) { T entry_value; Deserialize(entry, entry_value); value.push_back(entry_value); } } template void Deserialize(const Reader& reader, optional& value) { T real_value; Deserialize(reader, real_value); value = real_value; } void Deserialize(const Reader& reader, int& value); void Deserialize(const Reader& reader, bool& value); void Deserialize(const Reader& reader, std::string& value); void Deserialize(const Reader& reader, Location& output); void Deserialize(const Reader& reader, IndexedTypeDef& value); void Deserialize(const Reader& reader, IndexedFuncDef& value); void Deserialize(const Reader& reader, IndexedVarDef& value); void Deserialize(const Reader& reader, IndexedFile& file); #endif std::string Serialize(IndexedFile& file); IndexedFile Deserialize(std::string path, std::string serialized);