diff --git a/Serializing/test.cpp b/Serializing/test.cpp index 2f7d840..b849ac0 100644 --- a/Serializing/test.cpp +++ b/Serializing/test.cpp @@ -49,85 +49,62 @@ Array values; }; -#if 0 -#if 1 -template -void Visit(V& v, T& var, typename V::Visit(T&))//, typename std::enable_if< ! std::is_function< decltype(T::Visit)(V&) >::value , T >::type* = 0 ) -// typename std::enable_if< false /* !std::is_function::value*/ >::type -//void Visit(V& v, T& var, typename std::enable_if< std::is_function::type , T >::type* = 0 ) +class VisitorBase { - v.Visit(var); -} -#endif +public: + virtual ~VisitorBase() {} + virtual void Enter(const char* name) = 0; + virtual void Exit(const char* name) = 0; -template -void Visit(V& v, T& var) // , typename T::Visit) -//, typename std::enable_if< std::is_function< decltype(T::Visit)(V&) >::value , T >::type* = 0 ) + std::string quoted(const char* name) + { + return std::string("\"" + std::string(name) + "\""); + } + + std::string indent(int spaces) + { + return std::string(spaces*2, ' '); + } +}; + + +class Serializer : public VisitorBase { - var.Visit(v); -} +public: + virtual const std::string& Output() = 0; + virtual void Reset() = 0; +}; -template -void Visit(V& v, Integer32& i) + +class Deserializer : public VisitorBase { - v.Visit(i); -} - -template -void Visit(V& v, String& str) -{ - v.Visit(str); -} - -template -void Visit(V& v, Array& arr) -{ - v.Visit(arr); -} - -#endif +public: + virtual void Reset(const std::string& a_input) = 0; +}; -class XMLSerializerVisitor +class XMLSerializerVisitor : public Serializer { public: XMLSerializerVisitor() { - reset(); + Reset(); } void Enter(const char* name) { - indent(depth); - output += "<"; - output += name; - output += ">\n"; - depth++; + output += indent(depth++) + "<" + name + ">\n"; } void Exit(const char* name) { - depth--; - indent(depth); - output += "\n"; + output += indent(--depth) + "\n"; } template void Visit(Array& arr) { -/* - Integer32 siz = arr.size(); - Visit("length", siz); - for (int i = 0; i < arr.size(); i++) - { - char buf[1024]; - snprintf(buf, 1024, "element[%i]", i); - Visit(buf, arr.at(i)); - } -*/ for (auto i : arr) { Visit("item", i); @@ -136,24 +113,19 @@ void Visit(Integer32& var) { - indent(depth); - output += std::to_string(var); - output += "\n"; + output += indent(depth) + std::to_string(var) + "\n"; } void Visit(String& var) { // TODO: limitiation is that the string can not have a new line - indent(depth); - output += var; - output += "\n"; + output += indent(depth) + var + "\n"; } template void Visit(const char* name, T& var) { Enter(name); - //::Visit(*this, var); Visit(var); Exit(name); } @@ -164,39 +136,38 @@ var.Visit(*this); } - std::string& Output() + const std::string& Output() { output += "\n"; return output; } - void reset() + + void Reset() { - output = "\n"; + output = ""; depth = 1; } + private: int depth = 0; std::string output; - - void indent(int spaces) - { - for(int i = 0; i < spaces; i++) - { - output += " "; - } - } }; -class XMLDeserializerVisitor +class XMLDeserializerVisitor : public Deserializer { public: - XMLDeserializerVisitor(std::string data) : input(data) + XMLDeserializerVisitor(std::string data) { + Reset(data); + } + + void Reset(const std::string& a_input) + { + input = a_input; String str; - Visit(str); - // deserialize the tag + Visit(str); // deserialize the tag } void Enter(const char* name) @@ -205,6 +176,7 @@ Visit(str); //printf("enter deserializing for -%s-, got -%s-\n", name, str.c_str()); } + void Exit(const char* name) { String str; @@ -215,18 +187,6 @@ template void Visit(Array& arr) { -/* - Integer32 siz = 0; - Visit("length", siz); - arr.resize(siz); - //printf("deserializing array of len: %i\n", siz); - for (int i = 0; i < arr.size(); i++) - { - char buf[1024]; - snprintf(buf, 1024, "element[%i]", i); - Visit(buf, arr.at(i)); - } -*/ while (true) { int oldOffset = offset; @@ -258,15 +218,7 @@ var = input.substr(offset, newLineOff-offset); offset = newLineOff; } -/* - template - void Visit(const char* name, T& var) - { - Enter(name); - ::Visit(*this, var); - Exit(name); - } - */ + template void Visit(const char* name, T& var) { @@ -294,18 +246,17 @@ }; -class JsonSerializerVisitor +class JsonSerializerVisitor : public Serializer { public: JsonSerializerVisitor() { - reset(); + Reset(); } void Enter(const char* name) { - output += indent(depth) + quoted(name) + " : {\n"; - depth++; + output += indent(depth++) + quoted(name) + " : {\n"; } void Exit(const char* name) @@ -345,7 +296,6 @@ lastDepth = depth; output += indent(depth) + quoted(name) + " : "; - // ::Visit(*this, var); Visit(var); } @@ -355,12 +305,13 @@ var.Visit(*this); } - std::string& Output() + const std::string& Output() { output += "}\n"; return output; } - void reset() + + void Reset() { output = "{\n"; depth = 1; @@ -370,23 +321,20 @@ int depth = 0; std::string output; - std::string quoted(const char* name) - { - return std::string("\"" + std::string(name) + "\""); - } - std::string indent(int spaces) - { - return std::string(spaces*2, ' '); - } }; - -class JsonDeserializerVisitor +class JsonDeserializerVisitor : public VisitorBase { public: - JsonDeserializerVisitor(std::string data) : input(data) + JsonDeserializerVisitor(std::string data) { + Reset(data); + } + + void Reset(const std::string& a_input) + { + input = a_input; String str; ParseString(str); // gets the '{' assert(str == "{"); @@ -421,6 +369,7 @@ assert(str == "{"); //printf("enter deserializing for -%s-, got -%s-\n", name, str.c_str()); } + void Exit(const char* name) { String str; @@ -481,7 +430,6 @@ assert(str == quoted(name)); ParseString(str); // gets the ':' assert(str == ":"); - // ::Visit(*this, var); Visit(var); int oldOffset = offset;