From 3a0b6f5886080bd6e868ea851b35cb42765b5a01 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 23 Sep 2016 22:15:22 +0100 Subject: Inherit a common model storage base --- slicer/slicer/modelParts.h | 7 ++++ slicer/slicer/modelPartsTypes.h | 39 +++++--------------- slicer/slicer/modelPartsTypes.impl.h | 70 ++++++++++++++++++------------------ slicer/tool/parser.cpp | 14 ++++---- 4 files changed, 58 insertions(+), 72 deletions(-) diff --git a/slicer/slicer/modelParts.h b/slicer/slicer/modelParts.h index c9a0995..d3e56db 100644 --- a/slicer/slicer/modelParts.h +++ b/slicer/slicer/modelParts.h @@ -167,6 +167,13 @@ namespace Slicer { static const std::string & ToModelTypeName(const std::string &); }; + template + class DLL_PUBLIC ModelPartModel { + protected: + ModelPartModel(T & m) : Model(m) { } + T & Model; + }; + template inline ModelPartPtr ModelPartFor(T & t) __attribute__ ((deprecated)); template inline ModelPartPtr ModelPartFor(T & t) { return ModelPart::CreateFor(t); } diff --git a/slicer/slicer/modelPartsTypes.h b/slicer/slicer/modelPartsTypes.h index 8af3653..d5e687d 100644 --- a/slicer/slicer/modelPartsTypes.h +++ b/slicer/slicer/modelPartsTypes.h @@ -15,7 +15,7 @@ namespace Slicer { }; template - class DLL_PUBLIC ModelPartForSimple : public ModelPartForSimpleBase { + class DLL_PUBLIC ModelPartForSimple : public ModelPartForSimpleBase, protected ModelPartModel { public: typedef T element_type; @@ -23,9 +23,6 @@ namespace Slicer { virtual void SetValue(ValueSourcePtr s) override; virtual void GetValue(ValueTargetPtr s) override; - - private: - T & Member; }; class DLL_PUBLIC ModelPartForConvertedBase : public ModelPart { @@ -39,7 +36,7 @@ namespace Slicer { }; template - class DLL_PUBLIC ModelPartForConverted : public ModelPartForConvertedBase { + class DLL_PUBLIC ModelPartForConverted : public ModelPartForConvertedBase, protected ModelPartModel { public: typedef T element_type; @@ -47,13 +44,10 @@ namespace Slicer { virtual void SetValue(ValueSourcePtr s) override; virtual void GetValue(ValueTargetPtr s) override; - - private: - T & Member; }; template - class DLL_PUBLIC ModelPartForOptional : public ModelPart { + class DLL_PUBLIC ModelPartForOptional : public ModelPart, protected ModelPartModel > { public: ModelPartForOptional(IceUtil::Optional< typename T::element_type > & h); virtual void OnEachChild(const ChildHandler & ch) override; @@ -72,8 +66,7 @@ namespace Slicer { virtual const Metadata & GetMetadata() const override; - private: - IceUtil::Optional< typename T::element_type > & OptionalMember; + protected: ModelPartPtr modelPart; }; @@ -140,7 +133,7 @@ namespace Slicer { }; template - class DLL_PUBLIC ModelPartForClass : public ModelPartForComplex { + class DLL_PUBLIC ModelPartForClass : public ModelPartForComplex, protected ModelPartModel { public: typedef T element_type; @@ -159,13 +152,10 @@ namespace Slicer { virtual IceUtil::Optional GetTypeIdProperty() const override; static const std::string typeIdProperty; - - private: - T & ModelObject; }; template - class DLL_PUBLIC ModelPartForStruct : public ModelPartForComplex { + class DLL_PUBLIC ModelPartForStruct : public ModelPartForComplex, protected ModelPartModel { public: typedef T element_type; @@ -174,9 +164,6 @@ namespace Slicer { T * GetModel() override; virtual bool HasValue() const override; - - private: - T & ModelObject; }; class DLL_PUBLIC ModelPartForEnumBase : public ModelPart { @@ -190,7 +177,7 @@ namespace Slicer { }; template - class DLL_PUBLIC ModelPartForEnum : public ModelPartForEnumBase { + class DLL_PUBLIC ModelPartForEnum : public ModelPartForEnumBase, protected ModelPartModel { public: typedef T element_type; typedef boost::bimap Enumerations; @@ -207,9 +194,6 @@ namespace Slicer { static const Enumerations enumerations; DLL_PUBLIC static const std::string & lookup(T); DLL_PUBLIC static T lookup(const std::string &); - - private: - T & modelPart; }; class DLL_PUBLIC ModelPartForSequenceBase : public ModelPart { @@ -220,7 +204,7 @@ namespace Slicer { }; template - class DLL_PUBLIC ModelPartForSequence : public ModelPartForSequenceBase { + class DLL_PUBLIC ModelPartForSequence : public ModelPartForSequenceBase, protected ModelPartModel { public: typedef T element_type; @@ -239,8 +223,6 @@ namespace Slicer { private: ModelPartPtr elementModelPart(typename T::value_type &) const; - - T & sequence; }; template @@ -264,7 +246,7 @@ namespace Slicer { }; template - class DLL_PUBLIC ModelPartForDictionary : public ModelPartForDictionaryBase { + class DLL_PUBLIC ModelPartForDictionary : public ModelPartForDictionaryBase, protected ModelPartModel { public: typedef T element_type; @@ -280,9 +262,6 @@ namespace Slicer { static const Metadata metadata; static const std::string pairName; - - private: - T & dictionary; }; } diff --git a/slicer/slicer/modelPartsTypes.impl.h b/slicer/slicer/modelPartsTypes.impl.h index 201e90f..e38d307 100644 --- a/slicer/slicer/modelPartsTypes.impl.h +++ b/slicer/slicer/modelPartsTypes.impl.h @@ -64,43 +64,43 @@ namespace Slicer { // ModelPartForSimple template ModelPartForSimple::ModelPartForSimple(T & h) : - Member(h) + ModelPartModel(h) { } template void ModelPartForSimple::SetValue(ValueSourcePtr s) { - s->set(Member); + s->set(this->Model); } template void ModelPartForSimple::GetValue(ValueTargetPtr s) { - s->get(Member); + s->get(this->Model); } // ModelPartForConverted template ModelPartForConverted::ModelPartForConverted(T & h) : - Member(h) + ModelPartModel(h) { } // ModelPartForOptional template ModelPartForOptional::ModelPartForOptional(IceUtil::Optional< typename T::element_type > & h) : - OptionalMember(h) + ModelPartModel >(h) { - if (OptionalMember) { - modelPart = new T(*OptionalMember); + if (this->Model) { + modelPart = new T(*this->Model); } } template void ModelPartForOptional::OnEachChild(const ChildHandler & ch) { - if (OptionalMember) { + if (this->Model) { modelPart->OnEachChild(ch); } } @@ -108,7 +108,7 @@ namespace Slicer { template void ModelPartForOptional::Complete() { - if (OptionalMember) { + if (this->Model) { modelPart->Complete(); } } @@ -116,9 +116,9 @@ namespace Slicer { template void ModelPartForOptional::Create() { - if (!OptionalMember) { - OptionalMember = typename T::element_type(); - modelPart = new T(*OptionalMember); + if (!this->Model) { + this->Model = typename T::element_type(); + modelPart = new T(*this->Model); modelPart->Create(); } } @@ -126,7 +126,7 @@ namespace Slicer { template ChildRefPtr ModelPartForOptional::GetAnonChildRef(const HookFilter & flt) { - if (OptionalMember) { + if (this->Model) { return modelPart->GetAnonChildRef(flt); } return NULL; @@ -135,7 +135,7 @@ namespace Slicer { template ChildRefPtr ModelPartForOptional::GetChildRef(const std::string & name, const HookFilter & flt) { - if (OptionalMember) { + if (this->Model) { return modelPart->GetChildRef(name, flt); } return NULL; @@ -144,7 +144,7 @@ namespace Slicer { template void ModelPartForOptional::SetValue(ValueSourcePtr s) { - if (OptionalMember) { + if (this->Model) { modelPart->SetValue(s); } } @@ -152,9 +152,9 @@ namespace Slicer { template void ModelPartForOptional::GetValue(ValueTargetPtr s) { - if (!OptionalMember) { - OptionalMember = typename T::element_type(); - modelPart = new T(*OptionalMember); + if (!this->Model) { + this->Model = typename T::element_type(); + modelPart = new T(*this->Model); } modelPart->GetValue(s); } @@ -162,7 +162,7 @@ namespace Slicer { template bool ModelPartForOptional::HasValue() const { - return OptionalMember && modelPart->HasValue(); + return this->Model && modelPart->HasValue(); } template @@ -224,20 +224,20 @@ namespace Slicer { // ModelPartForClass template ModelPartForClass::ModelPartForClass(T & h) : - ModelObject(h) + ModelPartModel(h) { } template void ModelPartForClass::Create() { - ModelObject = new typename T::element_type(); + this->Model = new typename T::element_type(); } template typename T::element_type * ModelPartForClass::GetModel() { - return ModelObject.get(); + return this->Model.get(); } template @@ -247,13 +247,13 @@ namespace Slicer { if (ref == classRefMap()->end()) { throw UnknownType(name); } - return ref->second(&this->ModelObject); + return ref->second(&this->Model); } template bool ModelPartForClass::HasValue() const { - return ModelObject; + return this->Model; } template @@ -265,14 +265,14 @@ namespace Slicer { // ModelPartForStruct template ModelPartForStruct::ModelPartForStruct(T & o) : - ModelObject(o) + ModelPartModel(o) { } template T * ModelPartForStruct::GetModel() { - return &ModelObject; + return &this->Model; } template @@ -284,7 +284,7 @@ namespace Slicer { // ModelPartForEnum template ModelPartForEnum::ModelPartForEnum(T & s) : - modelPart(s) + ModelPartModel(s) { } @@ -297,14 +297,14 @@ namespace Slicer { // ModelPartForSequence template ModelPartForSequence::ModelPartForSequence(T & s) : - sequence(s) + ModelPartModel(s) { } template void ModelPartForSequence::OnEachChild(const ChildHandler & ch) { - for(auto & element : sequence) { + for(auto & element : this->Model) { ch(elementName, elementModelPart(element), NULL); } } @@ -312,8 +312,8 @@ namespace Slicer { template ChildRefPtr ModelPartForSequence::GetAnonChildRef(const HookFilter &) { - sequence.push_back(typename element_type::value_type()); - return new ImplicitChildRef(ModelPart::CreateFor(sequence.back())); + this->Model.push_back(typename element_type::value_type()); + return new ImplicitChildRef(ModelPart::CreateFor(this->Model.back())); } template @@ -359,14 +359,14 @@ namespace Slicer { // ModelPartForDictionary template ModelPartForDictionary::ModelPartForDictionary(T & d) : - dictionary(d) + ModelPartModel(d) { } template void ModelPartForDictionary::OnEachChild(const ChildHandler & ch) { - for (auto & pair : dictionary) { + for (auto & pair : this->Model) { ch(pairName, new ModelPartForStruct(pair), NULL); } } @@ -374,7 +374,7 @@ namespace Slicer { template ChildRefPtr ModelPartForDictionary::GetAnonChildRef(const HookFilter &) { - return new ImplicitChildRef(new ModelPartForDictionaryElementInserter(dictionary)); + return new ImplicitChildRef(new ModelPartForDictionaryElementInserter(this->Model)); } template @@ -383,7 +383,7 @@ namespace Slicer { if (name != pairName) { throw IncorrectElementName(name); } - return new ImplicitChildRef(new ModelPartForDictionaryElementInserter(dictionary)); + return new ImplicitChildRef(new ModelPartForDictionaryElementInserter(this->Model)); } template diff --git a/slicer/tool/parser.cpp b/slicer/tool/parser.cpp index 56fe4cd..425e688 100644 --- a/slicer/tool/parser.cpp +++ b/slicer/tool/parser.cpp @@ -63,7 +63,7 @@ namespace Slicer { fprintbf(cpp, "\t\t%s tmp;\n", conversion.ExchangeType); fprintbf(cpp, "\t\tvspt->set(tmp);\n"); - fprintbf(cpp, "\t\tMember = %s(tmp);\n", + fprintbf(cpp, "\t\tModel = %s(tmp);\n", conversion.ConvertToModelFunc); fprintbf(cpp, "\t\treturn;\n"); fprintbf(cpp, "\t}\n"); @@ -72,7 +72,7 @@ namespace Slicer { if (!dm->hasMetaData("slicer:nodefaultconversion")) { fprintbf(cpp, "\tif (auto vspt = dynamic_cast *>(vsp.get())) {\n", Slice::typeToString(type)); - fprintbf(cpp, "\t\tvspt->set(Member);\n"); + fprintbf(cpp, "\t\tvspt->set(Model);\n"); fprintbf(cpp, "\t\treturn;\n"); fprintbf(cpp, "\t}\n"); } @@ -91,7 +91,7 @@ namespace Slicer { for (const auto & conversion : conversions) { fprintbf(cpp, "\tif (auto vtpt = dynamic_cast *>(vtp.get())) {\n", conversion.ExchangeType); - fprintbf(cpp, "\t\tvtpt->get(%s(Member));\n", + fprintbf(cpp, "\t\tvtpt->get(%s(Model));\n", conversion.ConvertToExchangeFunc); fprintbf(cpp, "\t\treturn;\n"); fprintbf(cpp, "\t}\n"); @@ -100,7 +100,7 @@ namespace Slicer { if (!dm->hasMetaData("slicer:nodefaultconversion")) { fprintbf(cpp, "\tif (auto vtpt = dynamic_cast *>(vtp.get())) {\n", Slice::typeToString(type)); - fprintbf(cpp, "\t\tvtpt->get(Member);\n"); + fprintbf(cpp, "\t\tvtpt->get(Model);\n"); fprintbf(cpp, "\t\treturn;\n"); fprintbf(cpp, "\t}\n"); } @@ -222,7 +222,7 @@ namespace Slicer { fprintbf(cpp, "template<> DLL_PUBLIC\nTypeId\nModelPartForClass< %s >::GetTypeId() const\n{\n", typeToString(decl)); - fprintbf(cpp, "\tauto id = ModelObject->ice_id();\n"); + fprintbf(cpp, "\tauto id = this->Model->ice_id();\n"); fprintbf(cpp, "\treturn (id == \"%s\") ? TypeId() : ModelPart::ToExchangeTypeName(id);\n}\n\n", c->scoped()); @@ -352,11 +352,11 @@ namespace Slicer { fprintbf(cpp, "template<> DLL_PUBLIC\nvoid ModelPartForEnum< %s >::SetValue(ValueSourcePtr s) {\n\ std::string val;\n\ s->set(val);\n\ - modelPart = lookup(val);\n\ + this->Model = lookup(val);\n\ }\n\n", e->scoped()); fprintbf(cpp, "template<> DLL_PUBLIC\nvoid ModelPartForEnum< %s >::GetValue(ValueTargetPtr s) {\n\ - s->get(lookup(modelPart));\n\ + s->get(lookup(this->Model));\n\ }\n\n", e->scoped()); -- cgit v1.2.3