From 4fe7df35051600bbcb05b0ebc98efcb90c891cb5 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 6 Jan 2015 19:48:26 +0000 Subject: Replace ModelPartForClassRoot with ModelPartForRoot which should work with any type for a root --- slicer/slicer/modelParts.h | 52 +++++++++++++++++++++++++++++++++++----------- slicer/slicer/parser.cpp | 2 +- slicer/slicer/slicer.h | 4 ++-- 3 files changed, 43 insertions(+), 15 deletions(-) diff --git a/slicer/slicer/modelParts.h b/slicer/slicer/modelParts.h index 7ed713f..7cc6332 100644 --- a/slicer/slicer/modelParts.h +++ b/slicer/slicer/modelParts.h @@ -115,6 +115,17 @@ namespace Slicer { virtual const Metadata & GetMetadata() const = 0; }; +#define templateMODELPARTFOR(Type, ModelPart) \ + template ModelPartPtr ModelPartFor(Type & t); \ + template ModelPartPtr ModelPartFor(Type * t); + templateMODELPARTFOR(IceInternal::Handle, ModelPartForClass); + templateMODELPARTFOR(std::vector, ModelPartForSequence); + templateMODELPARTFOR(std::list, ModelPartForSequence); + template ModelPartPtr ModelPartFor(std::map & t); + template ModelPartPtr ModelPartFor(std::map * t); + templateMODELPARTFOR(T, ModelPartForStruct); +#undef templateMODELPARTFOR + class ModelPart : public IceUtil::Shared { public: virtual ~ModelPart() = default; @@ -403,17 +414,27 @@ namespace Slicer { }; template - class ModelPartForClassRoot : public ModelPartForClass { + class ModelPartForRoot : public ModelPart { public: - ModelPartForClassRoot() : - ModelPartForClass(ModelObject) + ModelPartForRoot() : + ModelObject(new T()), + owned(true), + mp(ModelPartFor(*ModelObject)) + { + } + + ModelPartForRoot(T & o) : + ModelObject(&o), + owned(false), + mp(ModelPartFor(*ModelObject)) { } - ModelPartForClassRoot(T o) : - ModelPartForClass(ModelObject) + ~ModelPartForRoot() { - ModelObject = o; + if (owned) { + delete ModelObject; + } } virtual ModelPartPtr GetChild(const std::string & name, const HookFilter &) override @@ -421,24 +442,31 @@ namespace Slicer { if (!name.empty() && name != rootName) { throw IncorrectElementName(rootName); } - ModelPartForClass::Create(); - return new ModelPartForClass(ModelObject); + mp->Create(); + return mp; } virtual void OnEachChild(const ChildHandler & ch) override { - ch(rootName, new ModelPartForClass(ModelObject), NULL); + ch(rootName, mp, NULL); } - typename T::element_type * GetModel() override + T & GetModel() { - return ModelObject.get(); + return *ModelObject; } virtual bool HasValue() const override { return ModelObject; } + virtual ModelPartType GetType() const override + { + return mp->GetType(); + } + private: - T ModelObject; + T * ModelObject; + bool owned; + ModelPartPtr mp; static std::string rootName; }; diff --git a/slicer/slicer/parser.cpp b/slicer/slicer/parser.cpp index cd11bff..b8f3f89 100644 --- a/slicer/slicer/parser.cpp +++ b/slicer/slicer/parser.cpp @@ -145,7 +145,7 @@ namespace Slicer { fprintf(cpp, "template<>\n"); auto name = metaDataValue("slicer:root:", c->getMetaData()); - fprintf(cpp, "std::string ModelPartForClassRoot< %s >::rootName(\"%s\");\n\n", + fprintf(cpp, "std::string ModelPartForRoot< %s >::rootName(\"%s\");\n\n", typeToString(decl).c_str(), name ? name->c_str() : c->name().c_str()); diff --git a/slicer/slicer/slicer.h b/slicer/slicer/slicer.h index 250f0a7..878a1e6 100644 --- a/slicer/slicer/slicer.h +++ b/slicer/slicer/slicer.h @@ -10,7 +10,7 @@ namespace Slicer { IceInternal::Handle Deserialize(SerializerParams & ... sp) { - IceUtil::Handle>> root = new ModelPartForClassRoot>(); + IceUtil::Handle>> root = new ModelPartForRoot>(); DeserializerPtr deserializer = new Deserializer(sp ...); deserializer->Deserialize(root); return root->GetModel(); @@ -20,7 +20,7 @@ namespace Slicer { void Serialize(IceInternal::Handle object, SerializerParams & ... sp) { - IceUtil::Handle>> root = new ModelPartForClassRoot>(object); + IceUtil::Handle>> root = new ModelPartForRoot>(object); SerializerPtr serializer = new Serializer(sp ...); serializer->Serialize(root); } -- cgit v1.2.3