From 7286ca071504c4e3ddf1303c9606af6a0db2ff5e Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 16 Jul 2017 13:01:07 +0100 Subject: Switch ModelPartModel's internal model reference to pointer (breaking change for anything that extends a ModelPart) --- slicer/slicer/modelParts.h | 5 ++- slicer/slicer/modelPartsTypes.h | 24 +++++------ slicer/slicer/modelPartsTypes.impl.h | 82 ++++++++++++++++++------------------ slicer/test/conversions.cpp | 10 +++-- slicer/test/conversions.h | 4 +- slicer/tool/parser.cpp | 12 +++--- 6 files changed, 70 insertions(+), 67 deletions(-) diff --git a/slicer/slicer/modelParts.h b/slicer/slicer/modelParts.h index 8fce1e3..26df8e3 100644 --- a/slicer/slicer/modelParts.h +++ b/slicer/slicer/modelParts.h @@ -179,8 +179,9 @@ namespace Slicer { template class DLL_PUBLIC ModelPartModel { protected: - ModelPartModel(T & m) : Model(m) { } - T & Model; + ModelPartModel() : Model(nullptr) { } + ModelPartModel(T * m) : Model(m) { } + T * Model; }; class DLL_PUBLIC ModelPartForRootBase : public ModelPart { diff --git a/slicer/slicer/modelPartsTypes.h b/slicer/slicer/modelPartsTypes.h index cbd953e..9c8f49e 100644 --- a/slicer/slicer/modelPartsTypes.h +++ b/slicer/slicer/modelPartsTypes.h @@ -12,7 +12,7 @@ namespace Slicer { template class DLL_PUBLIC ModelPartForRoot : public ModelPartForRootBase { public: - ModelPartForRoot(T & o); + ModelPartForRoot(T * o); const std::string & GetRootName() const override; virtual bool HasValue() const override; @@ -40,7 +40,7 @@ namespace Slicer { public: typedef T element_type; - ModelPartForSimple(T & h); + ModelPartForSimple(T * h); virtual void SetValue(ValueSourcePtr s) override; virtual void GetValue(ValueTargetPtr s) override; @@ -61,7 +61,7 @@ namespace Slicer { public: typedef T element_type; - ModelPartForConverted(T & h); + ModelPartForConverted(T * h); virtual void SetValue(ValueSourcePtr s) override; virtual void GetValue(ValueTargetPtr s) override; @@ -86,7 +86,7 @@ namespace Slicer { template class DLL_PUBLIC ModelPartForOptional : public ModelPartForOptionalBase, protected ModelPartModel > { public: - ModelPartForOptional(IceUtil::Optional< typename T::element_type > & h); + ModelPartForOptional(IceUtil::Optional< typename T::element_type > * h); virtual void Create() override; virtual void GetValue(ValueTargetPtr s) override; virtual ModelPartType GetType() const override; @@ -130,7 +130,7 @@ namespace Slicer { ModelPartPtr Get(T * t) const override { - return t ? new MP(const_cast::type &>(t->*member)) : NULL; + return t ? new MP(const_cast::type *>(&(t->*member))) : NULL; } private: @@ -172,7 +172,7 @@ namespace Slicer { public: typedef IceInternal::Handle element_type; - ModelPartForClass(element_type & h); + ModelPartForClass(element_type * h); virtual void Create() override; @@ -202,7 +202,7 @@ namespace Slicer { public: typedef T element_type; - ModelPartForStruct(T & o); + ModelPartForStruct(T * o); T * GetModel() override; @@ -225,7 +225,7 @@ namespace Slicer { typedef T element_type; typedef boost::bimap Enumerations; - ModelPartForEnum(T & s); + ModelPartForEnum(T * s); virtual const Metadata & GetMetadata() const override; @@ -251,7 +251,7 @@ namespace Slicer { public: typedef T element_type; - ModelPartForSequence(T & s); + ModelPartForSequence(T * s); virtual void OnEachChild(const ChildHandler & ch) override; @@ -271,14 +271,14 @@ namespace Slicer { template class DLL_PUBLIC ModelPartForDictionaryElementInserter : public ModelPartForStruct { public: - ModelPartForDictionaryElementInserter(T & d); + ModelPartForDictionaryElementInserter(T * d); virtual void Complete() override; typename T::value_type value; private: - T & dictionary; + T * dictionary; }; class DLL_PUBLIC ModelPartForDictionaryBase : public ModelPart { @@ -293,7 +293,7 @@ namespace Slicer { public: typedef T element_type; - ModelPartForDictionary(T & d); + ModelPartForDictionary(T * d); virtual void OnEachChild(const ChildHandler & ch) override; diff --git a/slicer/slicer/modelPartsTypes.impl.h b/slicer/slicer/modelPartsTypes.impl.h index 08c14b2..6811de3 100644 --- a/slicer/slicer/modelPartsTypes.impl.h +++ b/slicer/slicer/modelPartsTypes.impl.h @@ -12,10 +12,10 @@ template class BaseModelPart; \ template class ModelPartForRoot; \ template class ModelPartForRoot< IceUtil::Optional >; \ - template<> ModelPartPtr ModelPart::CreateFor(Type & s) { return new ModelPartType(s); } \ - template<> ModelPartPtr ModelPart::CreateFor(IceUtil::Optional & s) { return new ModelPartForOptional(s); } \ - template<> ModelPartForRootPtr ModelPart::CreateRootFor(Type & s) { return new ModelPartForRoot(s); } \ - template<> ModelPartForRootPtr ModelPart::CreateRootFor(IceUtil::Optional & s) { return new ModelPartForRoot >(s); } \ + template<> ModelPartPtr ModelPart::CreateFor(Type & s) { return new ModelPartType(&s); } \ + template<> ModelPartPtr ModelPart::CreateFor(IceUtil::Optional & s) { return new ModelPartForOptional(&s); } \ + template<> ModelPartForRootPtr ModelPart::CreateRootFor(Type & s) { return new ModelPartForRoot(&s); } \ + template<> ModelPartForRootPtr ModelPart::CreateRootFor(IceUtil::Optional & s) { return new ModelPartForRoot >(&s); } \ #define MODELPARTFOR(Type, ModelPartType) \ CUSTOMMODELPARTFOR(Type, ModelPartType, ModelPartType) @@ -23,9 +23,9 @@ namespace Slicer { // ModelPartForRoot template - ModelPartForRoot::ModelPartForRoot(T & o) : - ModelPartForRootBase(ModelPart::CreateFor(o)), - ModelObject(&o) + ModelPartForRoot::ModelPartForRoot(T * o) : + ModelPartForRootBase(ModelPart::CreateFor(*o)), + ModelObject(o) { } @@ -132,7 +132,7 @@ namespace Slicer { // ModelPartForSimple template - ModelPartForSimple::ModelPartForSimple(T & h) : + ModelPartForSimple::ModelPartForSimple(T * h) : ModelPartModel(h) { } @@ -140,44 +140,44 @@ namespace Slicer { template void ModelPartForSimple::SetValue(ValueSourcePtr s) { - s->set(this->Model); + s->set(*this->Model); } template void ModelPartForSimple::GetValue(ValueTargetPtr s) { - s->get(this->Model); + s->get(*this->Model); } // ModelPartForConverted template - ModelPartForConverted::ModelPartForConverted(T & h) : + ModelPartForConverted::ModelPartForConverted(T * h) : ModelPartModel(h) { } // ModelPartForOptional template - ModelPartForOptional::ModelPartForOptional(IceUtil::Optional< typename T::element_type > & h) : + ModelPartForOptional::ModelPartForOptional(IceUtil::Optional< typename T::element_type > * h) : ModelPartModel >(h) { - if (this->Model) { - modelPart = new T(*this->Model); + if (*this->Model) { + modelPart = new T(&**this->Model); } } template bool ModelPartForOptional::hasModel() const { - return this->Model; + return *this->Model; } template void ModelPartForOptional::Create() { - if (!this->Model) { - this->Model = typename T::element_type(); - modelPart = new T(*this->Model); + if (!*this->Model) { + *this->Model = typename T::element_type(); + modelPart = new T(&**this->Model); modelPart->Create(); } } @@ -185,9 +185,9 @@ namespace Slicer { template void ModelPartForOptional::GetValue(ValueTargetPtr s) { - if (!this->Model) { - this->Model = typename T::element_type(); - modelPart = new T(*this->Model); + if (!*this->Model) { + *this->Model = typename T::element_type(); + modelPart = new T(&**this->Model); } modelPart->GetValue(s); } @@ -237,7 +237,7 @@ namespace Slicer { // ModelPartForClass template - ModelPartForClass::ModelPartForClass(element_type & h) : + ModelPartForClass::ModelPartForClass(element_type * h) : ModelPartModel(h) { } @@ -245,25 +245,25 @@ namespace Slicer { template void ModelPartForClass::Create() { - this->Model = new T(); + *this->Model = new T(); } template T * ModelPartForClass::GetModel() { - return this->Model.get(); + return this->Model->get(); } template ModelPartPtr ModelPartForClass::GetSubclassModelPart(const std::string & name) { - return ModelPartForComplexBase::getSubclassModelPart(name, &this->Model); + return ModelPartForComplexBase::getSubclassModelPart(name, this->Model); } template bool ModelPartForClass::HasValue() const { - return this->Model; + return *this->Model; } template @@ -275,7 +275,7 @@ namespace Slicer { template ModelPartPtr ModelPartForClass::CreateModelPart(void * p) { - return new ModelPartForClass(*static_cast(p)); + return new ModelPartForClass(static_cast(p)); } template @@ -303,12 +303,12 @@ namespace Slicer { TypeId ModelPartForClass::GetTypeId() const { - return ModelPartForComplexBase::GetTypeId(this->Model->ice_id(), *className); + return ModelPartForComplexBase::GetTypeId((*this->Model)->ice_id(), *className); } // ModelPartForStruct template - ModelPartForStruct::ModelPartForStruct(T & o) : + ModelPartForStruct::ModelPartForStruct(T * o) : ModelPartModel(o) { } @@ -316,7 +316,7 @@ namespace Slicer { template T * ModelPartForStruct::GetModel() { - return &this->Model; + return this->Model; } template @@ -327,7 +327,7 @@ namespace Slicer { // ModelPartForEnum template - ModelPartForEnum::ModelPartForEnum(T & s) : + ModelPartForEnum::ModelPartForEnum(T * s) : ModelPartModel(s) { } @@ -340,7 +340,7 @@ namespace Slicer { // ModelPartForSequence template - ModelPartForSequence::ModelPartForSequence(T & s) : + ModelPartForSequence::ModelPartForSequence(T * s) : ModelPartModel(s) { } @@ -348,7 +348,7 @@ namespace Slicer { template void ModelPartForSequence::OnEachChild(const ChildHandler & ch) { - for(auto & element : this->Model) { + for(auto & element : *this->Model) { ch(elementName, elementModelPart(element), NULL); } } @@ -356,8 +356,8 @@ namespace Slicer { template ChildRefPtr ModelPartForSequence::GetAnonChildRef(const HookFilter &) { - this->Model.push_back(typename element_type::value_type()); - return new ImplicitChildRef(ModelPart::CreateFor(this->Model.back())); + this->Model->push_back(typename element_type::value_type()); + return new ImplicitChildRef(ModelPart::CreateFor(this->Model->back())); } template @@ -388,8 +388,8 @@ namespace Slicer { // ModelPartForDictionaryElementInserter template - ModelPartForDictionaryElementInserter::ModelPartForDictionaryElementInserter(T & d) : - ModelPartForStruct(value), + ModelPartForDictionaryElementInserter::ModelPartForDictionaryElementInserter(T * d) : + ModelPartForStruct(&value), dictionary(d) { } @@ -397,12 +397,12 @@ namespace Slicer { template void ModelPartForDictionaryElementInserter::Complete() { - dictionary.insert(value); + dictionary->insert(value); } // ModelPartForDictionary template - ModelPartForDictionary::ModelPartForDictionary(T & d) : + ModelPartForDictionary::ModelPartForDictionary(T * d) : ModelPartModel(d) { } @@ -410,8 +410,8 @@ namespace Slicer { template void ModelPartForDictionary::OnEachChild(const ChildHandler & ch) { - for (auto & pair : this->Model) { - ch(pairName, new ModelPartForStruct(pair), NULL); + for (auto & pair : *this->Model) { + ch(pairName, new ModelPartForStruct(&pair), NULL); } } diff --git a/slicer/test/conversions.cpp b/slicer/test/conversions.cpp index 63de1de..3df53b3 100644 --- a/slicer/test/conversions.cpp +++ b/slicer/test/conversions.cpp @@ -108,7 +108,7 @@ namespace Slicer { namespace TestModule { int completions = 0; - AbValidator::AbValidator(ClassTypePtr & m) : + AbValidator::AbValidator(ClassTypePtr * m) : Slicer::ModelPartForClass(m) { } @@ -116,7 +116,8 @@ namespace TestModule { void AbValidator::Complete() { - if (this->Model->a == 0 || this->Model->b == 0) { + const auto & M = *this->Model; + if (M->a == 0 || M->b == 0) { // LCOV_EXCL_START throw std::runtime_error("Mock error"); // LCOV_EXCL_STOP @@ -125,7 +126,7 @@ namespace TestModule { completions += 1; } - MonthValidator::MonthValidator(::Ice::Short & m) : + MonthValidator::MonthValidator(::Ice::Short * m) : Slicer::ModelPartForSimple<::Ice::Short>(m) { } @@ -133,7 +134,8 @@ namespace TestModule { void MonthValidator::Complete() { - if (this->Model < 1 || this->Model > 12) { + const auto & M = *this->Model; + if (M < 1 || M > 12) { // LCOV_EXCL_START throw std::runtime_error("This date smells fishy."); // LCOV_EXCL_STOP diff --git a/slicer/test/conversions.h b/slicer/test/conversions.h index 62a9c4b..76dcd97 100644 --- a/slicer/test/conversions.h +++ b/slicer/test/conversions.h @@ -11,14 +11,14 @@ namespace TestModule { class DLL_PUBLIC AbValidator : public Slicer::ModelPartForClass { public: - AbValidator(ClassTypePtr &); + AbValidator(ClassTypePtr *); void Complete() override; }; class DLL_PUBLIC MonthValidator : public Slicer::ModelPartForSimple<::Ice::Short> { public: - MonthValidator(::Ice::Short &); + MonthValidator(::Ice::Short *); void Complete() override; }; diff --git a/slicer/tool/parser.cpp b/slicer/tool/parser.cpp index 5302044..43feace 100644 --- a/slicer/tool/parser.cpp +++ b/slicer/tool/parser.cpp @@ -60,7 +60,7 @@ namespace Slicer { fprintbf(cpp, "\t\t%s tmp;\n", conversion.ExchangeType); fprintbf(cpp, "\t\tvspt->set(tmp);\n"); - fprintbf(cpp, "\t\tModel = %s(tmp);\n", + fprintbf(cpp, "\t\t*Model = %s(tmp);\n", conversion.ConvertToModelFunc); fprintbf(cpp, "\t\treturn;\n"); fprintbf(cpp, "\t}\n"); @@ -69,7 +69,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(Model);\n"); + fprintbf(cpp, "\t\tvspt->set(*Model);\n"); fprintbf(cpp, "\t\treturn;\n"); fprintbf(cpp, "\t}\n"); } @@ -85,7 +85,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(Model));\n", + fprintbf(cpp, "\t\tvtpt->get(%s(*Model));\n", conversion.ConvertToExchangeFunc); fprintbf(cpp, "\t\treturn;\n"); fprintbf(cpp, "\t}\n"); @@ -94,7 +94,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(Model);\n"); + fprintbf(cpp, "\t\tvtpt->get(*Model);\n"); fprintbf(cpp, "\t\treturn;\n"); fprintbf(cpp, "\t}\n"); } @@ -330,11 +330,11 @@ namespace Slicer { fprintbf(cpp, "template<> DLL_PUBLIC\nvoid ModelPartForEnum< %s >::SetValue(ValueSourcePtr s) {\n\ std::string val;\n\ s->set(val);\n\ - this->Model = 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(this->Model));\n\ + s->get(lookup(*this->Model));\n\ }\n\n", e->scoped()); -- cgit v1.2.3