diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2015-01-06 19:48:26 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2015-01-06 19:48:26 +0000 |
commit | 4fe7df35051600bbcb05b0ebc98efcb90c891cb5 (patch) | |
tree | c04134eb9b30ffed1f4920a139aa493be2a68eaf | |
parent | Allow conversions to be specified on a class/struct (diff) | |
download | slicer-4fe7df35051600bbcb05b0ebc98efcb90c891cb5.tar.bz2 slicer-4fe7df35051600bbcb05b0ebc98efcb90c891cb5.tar.xz slicer-4fe7df35051600bbcb05b0ebc98efcb90c891cb5.zip |
Replace ModelPartForClassRoot with ModelPartForRoot which should work with any type for a root
-rw-r--r-- | slicer/slicer/modelParts.h | 52 | ||||
-rw-r--r-- | slicer/slicer/parser.cpp | 2 | ||||
-rw-r--r-- | 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 <class T> ModelPartPtr ModelPartFor(Type & t); \ + template <class T> ModelPartPtr ModelPartFor(Type * t); + templateMODELPARTFOR(IceInternal::Handle<T>, ModelPartForClass); + templateMODELPARTFOR(std::vector<T>, ModelPartForSequence); + templateMODELPARTFOR(std::list<T>, ModelPartForSequence); + template <class K, class V> ModelPartPtr ModelPartFor(std::map<K, V> & t); + template <class K, class V> ModelPartPtr ModelPartFor(std::map<K, V> * t); + templateMODELPARTFOR(T, ModelPartForStruct); +#undef templateMODELPARTFOR + class ModelPart : public IceUtil::Shared { public: virtual ~ModelPart() = default; @@ -403,17 +414,27 @@ namespace Slicer { }; template<typename T> - class ModelPartForClassRoot : public ModelPartForClass<T> { + class ModelPartForRoot : public ModelPart { public: - ModelPartForClassRoot() : - ModelPartForClass<T>(ModelObject) + ModelPartForRoot() : + ModelObject(new T()), + owned(true), + mp(ModelPartFor(*ModelObject)) + { + } + + ModelPartForRoot(T & o) : + ModelObject(&o), + owned(false), + mp(ModelPartFor(*ModelObject)) { } - ModelPartForClassRoot(T o) : - ModelPartForClass<T>(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<T>::Create(); - return new ModelPartForClass<T>(ModelObject); + mp->Create(); + return mp; } virtual void OnEachChild(const ChildHandler & ch) override { - ch(rootName, new ModelPartForClass<T>(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<Object> Deserialize(SerializerParams & ... sp) { - IceUtil::Handle<ModelPartForClassRoot<IceInternal::Handle<Object>>> root = new ModelPartForClassRoot<IceInternal::Handle<Object>>(); + IceUtil::Handle<ModelPartForRoot<IceInternal::Handle<Object>>> root = new ModelPartForRoot<IceInternal::Handle<Object>>(); DeserializerPtr deserializer = new Deserializer(sp ...); deserializer->Deserialize(root); return root->GetModel(); @@ -20,7 +20,7 @@ namespace Slicer { void Serialize(IceInternal::Handle<Object> object, SerializerParams & ... sp) { - IceUtil::Handle<ModelPartForClassRoot<IceInternal::Handle<Object>>> root = new ModelPartForClassRoot<IceInternal::Handle<Object>>(object); + IceUtil::Handle<ModelPartForRoot<IceInternal::Handle<Object>>> root = new ModelPartForRoot<IceInternal::Handle<Object>>(object); SerializerPtr serializer = new Serializer(sp ...); serializer->Serialize(root); } |