diff options
author | randomdan <randomdan@localhost> | 2014-09-26 20:02:31 +0000 |
---|---|---|
committer | randomdan <randomdan@localhost> | 2014-09-26 20:02:31 +0000 |
commit | 0661f93741e95c43e6d69efe8c6aa1e2b94c1b8c (patch) | |
tree | dbf48f1d41680dd1950876b8d763073a5a606667 /slicer | |
parent | Allow a class to specify a specific name when referenced in a document (diff) | |
download | slicer-0661f93741e95c43e6d69efe8c6aa1e2b94c1b8c.tar.bz2 slicer-0661f93741e95c43e6d69efe8c6aa1e2b94c1b8c.tar.xz slicer-0661f93741e95c43e6d69efe8c6aa1e2b94c1b8c.zip |
Separate serializer and deserializer class hierarchiesslicer-0.8.1
Diffstat (limited to 'slicer')
-rw-r--r-- | slicer/json/serializer.cpp | 42 | ||||
-rw-r--r-- | slicer/json/serializer.h | 32 | ||||
-rw-r--r-- | slicer/slicer/serializer.h | 7 | ||||
-rw-r--r-- | slicer/slicer/slicer.h | 6 | ||||
-rw-r--r-- | slicer/test/run-slicer.cpp | 38 | ||||
-rw-r--r-- | slicer/xml/serializer.cpp | 38 | ||||
-rw-r--r-- | 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<json::Array &>(*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<json::Object>(*n).insert({name, nn}).first->second.get(), _1, _2)); + mp->OnEachChild(boost::bind(&JsonSerializer::ModelTreeIterate, boost::get<json::Object>(*n).insert({name, nn}).first->second.get(), _1, _2)); break; } case mpt_Sequence: case mpt_Dictionary: - mp->OnEachChild(boost::bind(&Json::ModelTreeIterateSeq, boost::get<json::Object>(*n).insert({name, json::ValuePtr(new json::Value(json::Array()))}).first->second.get(), _2)); + mp->OnEachChild(boost::bind(&JsonSerializer::ModelTreeIterateSeq, boost::get<json::Object>(*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 <jsonpp.h> 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<Serializer> SerializerPtr; + + class Deserializer : public IceUtil::Shared { + public: + virtual void Deserialize(ModelPartPtr) = 0; + }; + typedef IceUtil::Handle<Deserializer> 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 <slicer/serializer.h> namespace Slicer { - template <typename Serializer, typename Object, typename ... SerializerParams> + template <typename Deserializer, typename Object, typename ... SerializerParams> IceInternal::Handle<Object> Deserialize(SerializerParams & ... sp) { IceUtil::Handle<ModelPartForClassRoot<IceInternal::Handle<Object>>> root = new ModelPartForClassRoot<IceInternal::Handle<Object>>(); - 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<typename T, typename SerializerIn> +template<typename T, typename DeserializerIn> void verifyByFile(const fs::path & root, const fs::path & tmp, const fs::path & infile, const boost::function<void(const T &)> & 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<T> p = Slicer::Deserialize<SerializerIn, T>(input); + IceInternal::Handle<T> p = Slicer::Deserialize<DeserializerIn, T>(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<Slicer::JsonFile>(p, outputJson); + Slicer::Serialize<Slicer::JsonFileSerializer>(p, outputJson); fprintf(stderr, "%s : Serialize -> %s\n", input.string().c_str(), outputXml.string().c_str()); - Slicer::Serialize<Slicer::XmlFile>(p, outputXml); + Slicer::Serialize<Slicer::XmlFileSerializer>(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<typename T, typename Serializer, typename Internal> +template<typename T, typename Deserializer, typename Serializer, typename Internal> void verifyByHelper(const fs::path & root, const fs::path & tmp, const fs::path & infile, const boost::function<Internal(const fs::path &)> & 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<T> p = Slicer::Deserialize<Serializer, T>(docRead); + IceInternal::Handle<T> p = Slicer::Deserialize<Deserializer, T>(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<TestModule::BuiltIns, Slicer::XmlFile>(root, tmpf, "builtins.xml", checkBuiltIns_valuesCorrect); - verifyByFile<TestModule::Optionals, Slicer::XmlFile>(root, tmpf, "optionals-notset.xml", checkOptionals_notset); - verifyByFile<TestModule::Optionals, Slicer::XmlFile>(root, tmpf, "optionals-areset.xml", checkOptionals_areset); - verifyByFile<TestModule::InheritanceCont, Slicer::XmlFile>(root, tmpf, "inherit-a.xml"); - verifyByFile<TestModule::InheritanceCont, Slicer::XmlFile>(root, tmpf, "inherit-b.xml", checkInherits_types); - verifyByFile<TestModule::DateTimeContainer, Slicer::XmlFile>(root, tmpf, "conv-datetime.xml"); - verifyByFile<TestModule::BuiltIns, Slicer::JsonFile>(root, tmpf, "builtins2.json", checkBuiltIns_valuesCorrect); - verifyByFile<TestModule::Optionals, Slicer::JsonFile>(root, tmpf, "optionals-areset2.json", checkOptionals_areset); - verifyByFile<TestModule::InheritanceCont, Slicer::JsonFile>(root, tmpf, "inherit-c.json", checkInherits_types); - verifyByFile<TestModule::InheritanceCont2, Slicer::JsonFile>(root, tmpf, "inherit-d.json"); - verifyByFile<TestModule::InheritanceContMapped, Slicer::JsonFile>(root, tmpf, "inherit-mapped.json"); + verifyByFile<TestModule::BuiltIns, Slicer::XmlFileDeserializer>(root, tmpf, "builtins.xml", checkBuiltIns_valuesCorrect); + verifyByFile<TestModule::Optionals, Slicer::XmlFileDeserializer>(root, tmpf, "optionals-notset.xml", checkOptionals_notset); + verifyByFile<TestModule::Optionals, Slicer::XmlFileDeserializer>(root, tmpf, "optionals-areset.xml", checkOptionals_areset); + verifyByFile<TestModule::InheritanceCont, Slicer::XmlFileDeserializer>(root, tmpf, "inherit-a.xml"); + verifyByFile<TestModule::InheritanceCont, Slicer::XmlFileDeserializer>(root, tmpf, "inherit-b.xml", checkInherits_types); + verifyByFile<TestModule::DateTimeContainer, Slicer::XmlFileDeserializer>(root, tmpf, "conv-datetime.xml"); + verifyByFile<TestModule::BuiltIns, Slicer::JsonFileDeserializer>(root, tmpf, "builtins2.json", checkBuiltIns_valuesCorrect); + verifyByFile<TestModule::Optionals, Slicer::JsonFileDeserializer>(root, tmpf, "optionals-areset2.json", checkOptionals_areset); + verifyByFile<TestModule::InheritanceCont, Slicer::JsonFileDeserializer>(root, tmpf, "inherit-c.json", checkInherits_types); + verifyByFile<TestModule::InheritanceCont2, Slicer::JsonFileDeserializer>(root, tmpf, "inherit-d.json"); + verifyByFile<TestModule::InheritanceContMapped, Slicer::JsonFileDeserializer>(root, tmpf, "inherit-mapped.json"); - verifyByHelper<TestModule::Optionals, Slicer::JsonValue, json::Value>(root, tmph, "optionals-areset2.json", readJson, writeJson, freeJson, checkOptionals_areset); - verifyByHelper<TestModule::Optionals, Slicer::XmlDocument, xmlpp::Document *>(root, tmph, "optionals-areset.xml", readXml, writeXml, freeXml, checkOptionals_areset); + verifyByHelper<TestModule::Optionals, Slicer::JsonValueDeserializer, Slicer::JsonValueSerializer, json::Value>(root, tmph, "optionals-areset2.json", readJson, writeJson, freeJson, checkOptionals_areset); + verifyByHelper<TestModule::Optionals, Slicer::XmlDocumentDeserializer, Slicer::XmlDocumentSerializer, xmlpp::Document *>(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<const xmlpp::Element *>(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 <libxml++/document.h> 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 |