From 0661f93741e95c43e6d69efe8c6aa1e2b94c1b8c Mon Sep 17 00:00:00 2001 From: randomdan Date: Fri, 26 Sep 2014 20:02:31 +0000 Subject: Separate serializer and deserializer class hierarchies --- slicer/json/serializer.cpp | 42 ++++++++++++++++++++++++++---------------- slicer/json/serializer.h | 32 +++++++++++++++++++++++++------- slicer/slicer/serializer.h | 7 ++++++- slicer/slicer/slicer.h | 6 +++--- slicer/test/run-slicer.cpp | 38 +++++++++++++++++++------------------- slicer/xml/serializer.cpp | 38 ++++++++++++++++++++++++-------------- slicer/xml/serializer.h | 40 +++++++++++++++++++++++++++++++--------- 7 files changed, 134 insertions(+), 69 deletions(-) diff --git a/slicer/json/serializer.cpp b/slicer/json/serializer.cpp index 2c718ae..bb6a357 100644 --- a/slicer/json/serializer.cpp +++ b/slicer/json/serializer.cpp @@ -163,7 +163,7 @@ namespace Slicer { }; void - Json::ModelTreeIterateSeq(json::Value * n, ModelPartPtr mp) + JsonSerializer::ModelTreeIterateSeq(json::Value * n, ModelPartPtr mp) { auto & arr = boost::get(*n); arr.push_back(json::ValuePtr(new json::Value())); @@ -171,7 +171,7 @@ namespace Slicer { } void - Json::ModelTreeIterate(json::Value * n, const std::string & name, ModelPartPtr mp) + JsonSerializer::ModelTreeIterate(json::Value * n, const std::string & name, ModelPartPtr mp) { if (name.empty() || !n) { return; @@ -193,19 +193,19 @@ namespace Slicer { mp = mp->GetSubclassModelPart(*typeId); } } - mp->OnEachChild(boost::bind(&Json::ModelTreeIterate, boost::get(*n).insert({name, nn}).first->second.get(), _1, _2)); + mp->OnEachChild(boost::bind(&JsonSerializer::ModelTreeIterate, boost::get(*n).insert({name, nn}).first->second.get(), _1, _2)); break; } case mpt_Sequence: case mpt_Dictionary: - mp->OnEachChild(boost::bind(&Json::ModelTreeIterateSeq, boost::get(*n).insert({name, json::ValuePtr(new json::Value(json::Array()))}).first->second.get(), _2)); + mp->OnEachChild(boost::bind(&JsonSerializer::ModelTreeIterateSeq, boost::get(*n).insert({name, json::ValuePtr(new json::Value(json::Array()))}).first->second.get(), _2)); break; } } } void - Json::ModelTreeIterateRoot(json::Value * n, ModelPartPtr mp) + JsonSerializer::ModelTreeIterateRoot(json::Value * n, ModelPartPtr mp) { if (mp) { switch (mp->GetType()) { @@ -223,27 +223,32 @@ namespace Slicer { mp = mp->GetSubclassModelPart(*typeId); } } - mp->OnEachChild(boost::bind(&Json::ModelTreeIterate, n, _1, _2)); + mp->OnEachChild(boost::bind(&JsonSerializer::ModelTreeIterate, n, _1, _2)); break; case mpt_Sequence: *n = json::Array(); - mp->OnEachChild(boost::bind(&Json::ModelTreeIterate, n, _1, _2)); + mp->OnEachChild(boost::bind(&JsonSerializer::ModelTreeIterate, n, _1, _2)); break; case mpt_Dictionary: *n = json::Array(); - mp->OnEachChild(boost::bind(&Json::ModelTreeIterate, n, _1, _2)); + mp->OnEachChild(boost::bind(&JsonSerializer::ModelTreeIterate, n, _1, _2)); break; } } } - JsonFile::JsonFile(const boost::filesystem::path & p) : + JsonFileSerializer::JsonFileSerializer(const boost::filesystem::path & p) : + path(p) + { + } + + JsonFileDeserializer::JsonFileDeserializer(const boost::filesystem::path & p) : path(p) { } void - JsonFile::Deserialize(ModelPartPtr modelRoot) + JsonFileDeserializer::Deserialize(ModelPartPtr modelRoot) { std::ifstream inFile(path.string()); std::stringstream buffer; @@ -256,30 +261,35 @@ namespace Slicer { } void - JsonFile::Serialize(ModelPartPtr modelRoot) + JsonFileSerializer::Serialize(ModelPartPtr modelRoot) { json::Value doc; - modelRoot->OnEachChild(boost::bind(&Json::ModelTreeIterateRoot, &doc, _2)); + modelRoot->OnEachChild(boost::bind(&JsonSerializer::ModelTreeIterateRoot, &doc, _2)); std::ofstream outFile(path.string()); json::serializeValue(doc, outFile, "utf-8"); } - JsonValue::JsonValue(json::Value & v) : + JsonValueSerializer::JsonValueSerializer(json::Value & v) : + value(v) + { + } + + JsonValueDeserializer::JsonValueDeserializer(const json::Value & v) : value(v) { } void - JsonValue::Deserialize(ModelPartPtr modelRoot) + JsonValueDeserializer::Deserialize(ModelPartPtr modelRoot) { auto mp = modelRoot->GetChild(std::string()); boost::apply_visitor(DocumentTreeIterate(mp), value); } void - JsonValue::Serialize(ModelPartPtr modelRoot) + JsonValueSerializer::Serialize(ModelPartPtr modelRoot) { - modelRoot->OnEachChild(boost::bind(&Json::ModelTreeIterateRoot, &value, _2)); + modelRoot->OnEachChild(boost::bind(&JsonSerializer::ModelTreeIterateRoot, &value, _2)); } } diff --git a/slicer/json/serializer.h b/slicer/json/serializer.h index a01b836..6e87071 100644 --- a/slicer/json/serializer.h +++ b/slicer/json/serializer.h @@ -5,34 +5,52 @@ #include namespace Slicer { - class Json : public Serializer { + class JsonSerializer : public Serializer { protected: static void ModelTreeIterate(json::Value *, const std::string &, ModelPartPtr mp); static void ModelTreeIterateSeq(json::Value *, ModelPartPtr mp); static void ModelTreeIterateRoot(json::Value *, ModelPartPtr mp); }; - class JsonFile : public Json { + class JsonFileSerializer : public JsonSerializer { public: - JsonFile(const boost::filesystem::path &); + JsonFileSerializer(const boost::filesystem::path &); - virtual void Deserialize(ModelPartPtr) override; virtual void Serialize(ModelPartPtr) override; private: const boost::filesystem::path path; }; - class JsonValue : public Json { + class JsonValueSerializer : public JsonSerializer { public: - JsonValue(json::Value &); + JsonValueSerializer(json::Value &); - virtual void Deserialize(ModelPartPtr) override; virtual void Serialize(ModelPartPtr) override; private: json::Value & value; }; + + class JsonFileDeserializer : public Deserializer { + public: + JsonFileDeserializer(const boost::filesystem::path &); + + virtual void Deserialize(ModelPartPtr) override; + + private: + const boost::filesystem::path path; + }; + + class JsonValueDeserializer : public Deserializer { + public: + JsonValueDeserializer(const json::Value &); + + virtual void Deserialize(ModelPartPtr) override; + + private: + const json::Value & value; + }; } #endif diff --git a/slicer/slicer/serializer.h b/slicer/slicer/serializer.h index d89f9aa..b7b4541 100644 --- a/slicer/slicer/serializer.h +++ b/slicer/slicer/serializer.h @@ -9,10 +9,15 @@ namespace Slicer { class Serializer : public IceUtil::Shared { public: - virtual void Deserialize(ModelPartPtr) = 0; virtual void Serialize(ModelPartPtr) = 0; }; typedef IceUtil::Handle SerializerPtr; + + class Deserializer : public IceUtil::Shared { + public: + virtual void Deserialize(ModelPartPtr) = 0; + }; + typedef IceUtil::Handle DeserializerPtr; } #endif diff --git a/slicer/slicer/slicer.h b/slicer/slicer/slicer.h index d8d9f8e..250f0a7 100644 --- a/slicer/slicer/slicer.h +++ b/slicer/slicer/slicer.h @@ -6,13 +6,13 @@ #include namespace Slicer { - template + template IceInternal::Handle Deserialize(SerializerParams & ... sp) { IceUtil::Handle>> root = new ModelPartForClassRoot>(); - SerializerPtr serializer = new Serializer(sp ...); - serializer->Deserialize(root); + DeserializerPtr deserializer = new Deserializer(sp ...); + deserializer->Deserialize(root); return root->GetModel(); } diff --git a/slicer/test/run-slicer.cpp b/slicer/test/run-slicer.cpp index 8231866..8b56eed 100644 --- a/slicer/test/run-slicer.cpp +++ b/slicer/test/run-slicer.cpp @@ -60,7 +60,7 @@ namespace Slicer { } } -template +template void verifyByFile(const fs::path & root, const fs::path & tmp, const fs::path & infile, const boost::function & check = NULL) { @@ -70,15 +70,15 @@ verifyByFile(const fs::path & root, const fs::path & tmp, const fs::path & infil const fs::path outputXml = tmp / fs::change_extension(infile, "xml"); fprintf(stderr, "%s : Deserialize\n", input.string().c_str()); - IceInternal::Handle p = Slicer::Deserialize(input); + IceInternal::Handle p = Slicer::Deserialize(input); if (check) { fprintf(stderr, "%s : Check1\n", input.string().c_str()); check(*p); } fprintf(stderr, "%s : Serialize -> %s\n", input.string().c_str(), outputJson.string().c_str()); - Slicer::Serialize(p, outputJson); + Slicer::Serialize(p, outputJson); fprintf(stderr, "%s : Serialize -> %s\n", input.string().c_str(), outputXml.string().c_str()); - Slicer::Serialize(p, outputXml); + Slicer::Serialize(p, outputXml); if (check) { fprintf(stderr, "%s : Check2\n", input.string().c_str()); check(*p); @@ -88,7 +88,7 @@ verifyByFile(const fs::path & root, const fs::path & tmp, const fs::path & infil system(stringbf("diff -w %s %s", input, output)); } -template +template void verifyByHelper(const fs::path & root, const fs::path & tmp, const fs::path & infile, const boost::function & in, @@ -103,7 +103,7 @@ verifyByHelper(const fs::path & root, const fs::path & tmp, const fs::path & inf Internal docRead = in(input); fprintf(stderr, "%s : Deserialize\n", input.string().c_str()); - IceInternal::Handle p = Slicer::Deserialize(docRead); + IceInternal::Handle p = Slicer::Deserialize(docRead); ifree(docRead); if (check) { fprintf(stderr, "%s : Check1\n", input.string().c_str()); @@ -269,20 +269,20 @@ main(int, char ** argv) fs::create_directory(tmph); // Execute - verifyByFile(root, tmpf, "builtins.xml", checkBuiltIns_valuesCorrect); - verifyByFile(root, tmpf, "optionals-notset.xml", checkOptionals_notset); - verifyByFile(root, tmpf, "optionals-areset.xml", checkOptionals_areset); - verifyByFile(root, tmpf, "inherit-a.xml"); - verifyByFile(root, tmpf, "inherit-b.xml", checkInherits_types); - verifyByFile(root, tmpf, "conv-datetime.xml"); - verifyByFile(root, tmpf, "builtins2.json", checkBuiltIns_valuesCorrect); - verifyByFile(root, tmpf, "optionals-areset2.json", checkOptionals_areset); - verifyByFile(root, tmpf, "inherit-c.json", checkInherits_types); - verifyByFile(root, tmpf, "inherit-d.json"); - verifyByFile(root, tmpf, "inherit-mapped.json"); + verifyByFile(root, tmpf, "builtins.xml", checkBuiltIns_valuesCorrect); + verifyByFile(root, tmpf, "optionals-notset.xml", checkOptionals_notset); + verifyByFile(root, tmpf, "optionals-areset.xml", checkOptionals_areset); + verifyByFile(root, tmpf, "inherit-a.xml"); + verifyByFile(root, tmpf, "inherit-b.xml", checkInherits_types); + verifyByFile(root, tmpf, "conv-datetime.xml"); + verifyByFile(root, tmpf, "builtins2.json", checkBuiltIns_valuesCorrect); + verifyByFile(root, tmpf, "optionals-areset2.json", checkOptionals_areset); + verifyByFile(root, tmpf, "inherit-c.json", checkInherits_types); + verifyByFile(root, tmpf, "inherit-d.json"); + verifyByFile(root, tmpf, "inherit-mapped.json"); - verifyByHelper(root, tmph, "optionals-areset2.json", readJson, writeJson, freeJson, checkOptionals_areset); - verifyByHelper(root, tmph, "optionals-areset.xml", readXml, writeXml, freeXml, checkOptionals_areset); + verifyByHelper(root, tmph, "optionals-areset2.json", readJson, writeJson, freeJson, checkOptionals_areset); + verifyByHelper(root, tmph, "optionals-areset.xml", readXml, writeXml, freeXml, checkOptionals_areset); return 0; } diff --git a/slicer/xml/serializer.cpp b/slicer/xml/serializer.cpp index 0ac4034..ad3e87b 100644 --- a/slicer/xml/serializer.cpp +++ b/slicer/xml/serializer.cpp @@ -161,7 +161,7 @@ namespace Slicer { }; void - Xml::DocumentTreeIterate(const xmlpp::Node * node, ModelPartPtr mp) + XmlDeserializer::DocumentTreeIterate(const xmlpp::Node * node, ModelPartPtr mp) { while (node) { if (auto element = dynamic_cast(node)) { @@ -197,13 +197,13 @@ namespace Slicer { } void - Xml::DocumentTreeIterate(const xmlpp::Document * doc, ModelPartPtr mp) + XmlDeserializer::DocumentTreeIterate(const xmlpp::Document * doc, ModelPartPtr mp) { DocumentTreeIterate(doc->get_root_node(), mp); } void - Xml::ModelTreeIterate(xmlpp::Element * n, const std::string & name, ModelPartPtr mp) + XmlSerializer::ModelTreeIterate(xmlpp::Element * n, const std::string & name, ModelPartPtr mp) { if (name.empty()) { return; @@ -220,24 +220,29 @@ namespace Slicer { mp = mp->GetSubclassModelPart(*typeId); } mp->GetValue(new XmlContentValueTarget(element)); - mp->OnEachChild(boost::bind(&Xml::ModelTreeIterate, element, _1, _2)); + mp->OnEachChild(boost::bind(&XmlSerializer::ModelTreeIterate, element, _1, _2)); } } void - Xml::ModelTreeIterateRoot(xmlpp::Document * doc, const std::string & name, ModelPartPtr mp) + XmlSerializer::ModelTreeIterateRoot(xmlpp::Document * doc, const std::string & name, ModelPartPtr mp) { auto root = doc->create_root_node(name); - mp->OnEachChild(boost::bind(&Xml::ModelTreeIterate, root, _1, _2)); + mp->OnEachChild(boost::bind(&XmlSerializer::ModelTreeIterate, root, _1, _2)); } - XmlFile::XmlFile(const boost::filesystem::path & p) : + XmlFileSerializer::XmlFileSerializer(const boost::filesystem::path & p) : + path(p) + { + } + + XmlFileDeserializer::XmlFileDeserializer(const boost::filesystem::path & p) : path(p) { } void - XmlFile::Deserialize(ModelPartPtr modelRoot) + XmlFileDeserializer::Deserialize(ModelPartPtr modelRoot) { xmlpp::DomParser dom(path.string()); auto doc = dom.get_document(); @@ -245,29 +250,34 @@ namespace Slicer { } void - XmlFile::Serialize(ModelPartPtr modelRoot) + XmlFileSerializer::Serialize(ModelPartPtr modelRoot) { xmlpp::Document doc; - modelRoot->OnEachChild(boost::bind(&Xml::ModelTreeIterateRoot, &doc, _1, _2)); + modelRoot->OnEachChild(boost::bind(&XmlSerializer::ModelTreeIterateRoot, &doc, _1, _2)); doc.write_to_file_formatted(path.string()); } - XmlDocument::XmlDocument(xmlpp::Document * & d) : + XmlDocumentSerializer::XmlDocumentSerializer(xmlpp::Document * & d) : + doc(d) + { + } + + XmlDocumentDeserializer::XmlDocumentDeserializer(const xmlpp::Document * d) : doc(d) { } void - XmlDocument::Deserialize(ModelPartPtr modelRoot) + XmlDocumentDeserializer::Deserialize(ModelPartPtr modelRoot) { DocumentTreeIterate(doc, modelRoot); } void - XmlDocument::Serialize(ModelPartPtr modelRoot) + XmlDocumentSerializer::Serialize(ModelPartPtr modelRoot) { doc = new xmlpp::Document(); - modelRoot->OnEachChild(boost::bind(&Xml::ModelTreeIterateRoot, doc, _1, _2)); + modelRoot->OnEachChild(boost::bind(&XmlSerializer::ModelTreeIterateRoot, doc, _1, _2)); } } diff --git a/slicer/xml/serializer.h b/slicer/xml/serializer.h index 842b565..fc5794d 100644 --- a/slicer/xml/serializer.h +++ b/slicer/xml/serializer.h @@ -5,35 +5,57 @@ #include namespace Slicer { - class Xml : public Serializer { + class XmlSerializer : public Serializer { protected: - static void DocumentTreeIterate(const xmlpp::Node * node, ModelPartPtr mp); - static void DocumentTreeIterate(const xmlpp::Document * doc, ModelPartPtr mp); static void ModelTreeIterate(xmlpp::Element *, const std::string &, ModelPartPtr mp); static void ModelTreeIterateRoot(xmlpp::Document *, const std::string &, ModelPartPtr mp); }; - class XmlFile : public Xml { + class XmlFileSerializer : public XmlSerializer { public: - XmlFile(const boost::filesystem::path &); + XmlFileSerializer(const boost::filesystem::path &); - virtual void Deserialize(ModelPartPtr) override; virtual void Serialize(ModelPartPtr) override; private: const boost::filesystem::path path; }; - class XmlDocument : public Xml { + class XmlDocumentSerializer : public XmlSerializer { public: - XmlDocument(xmlpp::Document * &); + XmlDocumentSerializer(xmlpp::Document * &); - virtual void Deserialize(ModelPartPtr) override; virtual void Serialize(ModelPartPtr) override; private: xmlpp::Document * & doc; }; + + class XmlDeserializer : public Deserializer { + protected: + static void DocumentTreeIterate(const xmlpp::Node * node, ModelPartPtr mp); + static void DocumentTreeIterate(const xmlpp::Document * doc, ModelPartPtr mp); + }; + + class XmlFileDeserializer : public XmlDeserializer { + public: + XmlFileDeserializer(const boost::filesystem::path &); + + virtual void Deserialize(ModelPartPtr) override; + + private: + const boost::filesystem::path path; + }; + + class XmlDocumentDeserializer : public XmlDeserializer { + public: + XmlDocumentDeserializer(const xmlpp::Document *); + + virtual void Deserialize(ModelPartPtr) override; + + private: + const xmlpp::Document * doc; + }; } #endif -- cgit v1.2.3