From cccc507ce9ecc7a5902e162473b8945323230389 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 16 Jul 2017 14:24:44 +0100 Subject: Support getting a ModelPart for the elements in a container (with no actual model) --- slicer/slicer/modelParts.cpp | 6 ++++++ slicer/slicer/modelParts.h | 3 +++ slicer/slicer/modelPartsTypes.h | 6 +++++- slicer/slicer/modelPartsTypes.impl.h | 16 +++++++++++--- slicer/test/compilation.cpp | 41 ++++++++++++++++++++++++++++++++++++ 5 files changed, 68 insertions(+), 4 deletions(-) diff --git a/slicer/slicer/modelParts.cpp b/slicer/slicer/modelParts.cpp index 6a521cb..2a9594b 100644 --- a/slicer/slicer/modelParts.cpp +++ b/slicer/slicer/modelParts.cpp @@ -118,6 +118,12 @@ namespace Slicer { return false; } + ModelPartPtr + ModelPart::GetContainedModelPart() + { + return this; + } + HookCommon::HookCommon(const std::string & n) : name(n) { diff --git a/slicer/slicer/modelParts.h b/slicer/slicer/modelParts.h index 26df8e3..ce5ab5e 100644 --- a/slicer/slicer/modelParts.h +++ b/slicer/slicer/modelParts.h @@ -150,6 +150,8 @@ namespace Slicer { public: virtual ~ModelPart() = default; + template + static ModelPartPtr CreateFor(); template static ModelPartPtr CreateFor(T & t); template @@ -171,6 +173,7 @@ namespace Slicer { virtual bool HasValue() const = 0; virtual const Metadata & GetMetadata() const; virtual bool IsOptional() const; + virtual ModelPartPtr GetContainedModelPart(); static const std::string & ToExchangeTypeName(const std::string &); static const std::string & ToModelTypeName(const std::string &); diff --git a/slicer/slicer/modelPartsTypes.h b/slicer/slicer/modelPartsTypes.h index 9c8f49e..ca63638 100644 --- a/slicer/slicer/modelPartsTypes.h +++ b/slicer/slicer/modelPartsTypes.h @@ -130,7 +130,7 @@ namespace Slicer { ModelPartPtr Get(T * t) const override { - return t ? new MP(const_cast::type *>(&(t->*member))) : NULL; + return new MP(t ? const_cast::type *>(&(t->*member)) : NULL); } private: @@ -261,6 +261,8 @@ namespace Slicer { virtual const Metadata & GetMetadata() const override; + virtual ModelPartPtr GetContainedModelPart() override; + static const Metadata metadata; static const std::string elementName; @@ -303,6 +305,8 @@ namespace Slicer { virtual const Metadata & GetMetadata() const override; + virtual ModelPartPtr GetContainedModelPart() override; + static const Metadata metadata; static const std::string pairName; }; diff --git a/slicer/slicer/modelPartsTypes.impl.h b/slicer/slicer/modelPartsTypes.impl.h index c8407c9..43adc1e 100644 --- a/slicer/slicer/modelPartsTypes.impl.h +++ b/slicer/slicer/modelPartsTypes.impl.h @@ -12,6 +12,7 @@ template class BaseModelPart; \ template class ModelPartForRoot; \ template class ModelPartForRoot< IceUtil::Optional >; \ + template<> ModelPartPtr ModelPart::CreateFor() { return new ModelPartType(nullptr); } \ 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); } \ @@ -258,8 +259,7 @@ namespace Slicer { template T * ModelPartForClass::GetModel() { - BOOST_ASSERT(this->Model); - return this->Model->get(); + return this->Model ? this->Model->get() : nullptr; } template @@ -327,7 +327,6 @@ namespace Slicer { template T * ModelPartForStruct::GetModel() { - BOOST_ASSERT(this->Model); return this->Model; } @@ -400,6 +399,12 @@ namespace Slicer { return metadata; } + template + ModelPartPtr ModelPartForSequence::GetContainedModelPart() + { + return ModelPart::CreateFor(); + } + // ModelPartForDictionaryElementInserter template ModelPartForDictionaryElementInserter::ModelPartForDictionaryElementInserter(T * d) : @@ -453,6 +458,11 @@ namespace Slicer { return metadata; } + template + ModelPartPtr ModelPartForDictionary::GetContainedModelPart() + { + return new ModelPartForStruct(nullptr); + } } #endif diff --git a/slicer/test/compilation.cpp b/slicer/test/compilation.cpp index da13652..bea08a5 100644 --- a/slicer/test/compilation.cpp +++ b/slicer/test/compilation.cpp @@ -27,21 +27,52 @@ BOOST_TEST_DONT_PRINT_LOG_VALUE(std::type_info); BOOST_AUTO_TEST_CASE( compile_auto_modelpart_type_class ) { TypeTest(TestModule::BuiltInsPtr, new TestModule::BuiltIns(), ModelPartForClass, mpt_Complex); + BOOST_REQUIRE_EQUAL(mpp.get(), mpp->GetContainedModelPart().get()); } +void +hookHandler(std::vector * names, const std::string & name, Slicer::ModelPartPtr mpp, Slicer::HookCommonPtr h) +{ + names->push_back(name); + BOOST_REQUIRE(mpp); + BOOST_REQUIRE(mpp->GetContainedModelPart()); + BOOST_REQUIRE(h); + BOOST_REQUIRE_EQUAL(h->name, name); +} + + BOOST_AUTO_TEST_CASE( compile_auto_modelpart_type_sequenceclasses ) { StackTypeTest(TestModule::Classes, ModelPartForSequence, mpt_Sequence); + auto cmpp = mpp->GetContainedModelPart(); + BOOST_REQUIRE(cmpp); + BOOST_REQUIRE_EQUAL(Slicer::mpt_Complex, cmpp->GetType()); + std::vector names; + cmpp->OnEachChild(boost::bind(&hookHandler, &names, _1, _2, _3)); + BOOST_REQUIRE_EQUAL(2, names.size()); + BOOST_REQUIRE_EQUAL("a", names.front()); + BOOST_REQUIRE_EQUAL("b", names.back()); } BOOST_AUTO_TEST_CASE( compile_auto_modelpart_type_sequencestructs ) { StackTypeTest(TestModule::Structs, ModelPartForSequence, mpt_Sequence); + auto cmpp = mpp->GetContainedModelPart(); + BOOST_REQUIRE(cmpp); + BOOST_REQUIRE_EQUAL(Slicer::mpt_Complex, cmpp->GetType()); + std::vector names; + cmpp->OnEachChild(boost::bind(&hookHandler, &names, _1, _2, _3)); + BOOST_REQUIRE_EQUAL(2, names.size()); + BOOST_REQUIRE_EQUAL("a", names.front()); + BOOST_REQUIRE_EQUAL("b", names.back()); } BOOST_AUTO_TEST_CASE( compile_auto_modelpart_type_mapclasses ) { StackTypeTest(TestModule::ClassMap, ModelPartForDictionary, mpt_Dictionary); + auto cmpp = mpp->GetContainedModelPart(); + BOOST_REQUIRE(cmpp); + BOOST_REQUIRE_EQUAL(Slicer::mpt_Complex, cmpp->GetType()); } BOOST_AUTO_TEST_CASE( compile_auto_modelpart_type_mapstructs ) @@ -52,50 +83,60 @@ BOOST_AUTO_TEST_CASE( compile_auto_modelpart_type_mapstructs ) BOOST_AUTO_TEST_CASE( compile_auto_modelpart_type_bi_string ) { StackTypeTest(std::string, ModelPartForSimple, mpt_Simple); + BOOST_REQUIRE_EQUAL(mpp.get(), mpp->GetContainedModelPart().get()); } BOOST_AUTO_TEST_CASE( compile_auto_modelpart_type_bi_bool ) { StackTypeTest(bool, ModelPartForSimple, mpt_Simple); + BOOST_REQUIRE_EQUAL(mpp.get(), mpp->GetContainedModelPart().get()); } BOOST_AUTO_TEST_CASE( compile_auto_modelpart_type_bi_float ) { StackTypeTest(Ice::Float, ModelPartForSimple, mpt_Simple); + BOOST_REQUIRE_EQUAL(mpp.get(), mpp->GetContainedModelPart().get()); } BOOST_AUTO_TEST_CASE( compile_auto_modelpart_type_bi_double ) { StackTypeTest(Ice::Double, ModelPartForSimple, mpt_Simple); + BOOST_REQUIRE_EQUAL(mpp.get(), mpp->GetContainedModelPart().get()); } BOOST_AUTO_TEST_CASE( compile_auto_modelpart_type_bi_byte ) { StackTypeTest(Ice::Byte, ModelPartForSimple, mpt_Simple); + BOOST_REQUIRE_EQUAL(mpp.get(), mpp->GetContainedModelPart().get()); } BOOST_AUTO_TEST_CASE( compile_auto_modelpart_type_bi_short ) { StackTypeTest(Ice::Short, ModelPartForSimple, mpt_Simple); + BOOST_REQUIRE_EQUAL(mpp.get(), mpp->GetContainedModelPart().get()); } BOOST_AUTO_TEST_CASE( compile_auto_modelpart_type_bi_int ) { StackTypeTest(Ice::Int, ModelPartForSimple, mpt_Simple); + BOOST_REQUIRE_EQUAL(mpp.get(), mpp->GetContainedModelPart().get()); } BOOST_AUTO_TEST_CASE( compile_auto_modelpart_type_bi_long ) { StackTypeTest(Ice::Long, ModelPartForSimple, mpt_Simple); + BOOST_REQUIRE_EQUAL(mpp.get(), mpp->GetContainedModelPart().get()); } BOOST_AUTO_TEST_CASE( compile_auto_modelpart_type_struct ) { StackTypeTest(TestModule::StructType, ModelPartForStruct, mpt_Complex); + BOOST_REQUIRE_EQUAL(mpp.get(), mpp->GetContainedModelPart().get()); } BOOST_AUTO_TEST_CASE( compile_auto_modelpart_type_enum ) { StackTypeTest(TestModule::SomeNumbers, ModelPartForEnum, mpt_Simple); + BOOST_REQUIRE_EQUAL(mpp.get(), mpp->GetContainedModelPart().get()); } -- cgit v1.2.3