From 248b83144ab7facb9a124a1f5c2d1806412cc27e Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 21 Aug 2023 01:19:46 +0100 Subject: Make remaining non-root ModelParts on the stack --- slicer/slicer/modelParts.h | 11 +-- slicer/slicer/modelPartsTypes.cpp | 2 +- slicer/slicer/modelPartsTypes.h | 7 +- slicer/slicer/modelPartsTypes.impl.h | 92 ++++++++++-------- slicer/test/compilation.cpp | 182 ++++++++++++++++++----------------- slicer/test/conversions.cpp | 6 +- slicer/test/serializers.cpp | 6 +- slicer/tool/parser.cpp | 14 +-- 8 files changed, 169 insertions(+), 151 deletions(-) diff --git a/slicer/slicer/modelParts.h b/slicer/slicer/modelParts.h index da929ba..de6e89f 100644 --- a/slicer/slicer/modelParts.h +++ b/slicer/slicer/modelParts.h @@ -78,7 +78,6 @@ namespace Slicer { class ModelPartForRootBase; class HookCommon; - using ModelPartPtr = std::shared_ptr; using ModelPartUPtr = std::unique_ptr; using ModelPartParam = any_ptr; using ModelPartForRootParam = any_ptr; @@ -139,9 +138,9 @@ namespace Slicer { ModelPart & operator=(const ModelPart &) = delete; ModelPart & operator=(ModelPart &&) = delete; - template static ModelPartPtr Make(typename MP::element_type * t); - template static ModelPartPtr CreateFor(T & t); - template static ModelPartPtr CreateFor(Default &&); + template static void Make(typename MP::element_type * t, const ModelPartHandler &); + template static void CreateFor(T & t, const ModelPartHandler &); + template static void CreateFor(Default &&, const ModelPartHandler &); template static void OnRootFor(T & t, const ModelPartRootHandler &); virtual void OnEachChild(const ChildHandler &); @@ -171,7 +170,7 @@ namespace Slicer { class DLL_PUBLIC ModelPartForRootBase : public ModelPart { public: - explicit ModelPartForRootBase(ModelPartPtr mp); + explicit ModelPartForRootBase(ModelPartParam mp); virtual const std::string & GetRootName() const = 0; bool OnAnonChild(const SubPartHandler &, const HookFilter &) override; @@ -183,7 +182,7 @@ namespace Slicer { virtual void Read(::Ice::InputStream &) = 0; void OnContained(const ModelPartHandler &) override; - ModelPartPtr mp; + ModelPartParam mp; }; } diff --git a/slicer/slicer/modelPartsTypes.cpp b/slicer/slicer/modelPartsTypes.cpp index da38efe..673265e 100644 --- a/slicer/slicer/modelPartsTypes.cpp +++ b/slicer/slicer/modelPartsTypes.cpp @@ -108,7 +108,7 @@ namespace Slicer { } // ModelPartForRootBase - ModelPartForRootBase::ModelPartForRootBase(ModelPartPtr m) : mp(std::move(m)) { } + ModelPartForRootBase::ModelPartForRootBase(ModelPartParam m) : mp(m) { } bool ModelPartForRootBase::OnAnonChild(const SubPartHandler & h, const HookFilter &) diff --git a/slicer/slicer/modelPartsTypes.h b/slicer/slicer/modelPartsTypes.h index 838bbd0..f8a8814 100644 --- a/slicer/slicer/modelPartsTypes.h +++ b/slicer/slicer/modelPartsTypes.h @@ -26,7 +26,7 @@ namespace Slicer { template class ModelPartForRoot : public ModelPartForRootBase { public: - explicit ModelPartForRoot(T * o); + explicit ModelPartForRoot(T * o, ModelPartParam mp); const std::string & GetRootName() const override; bool HasValue() const override; @@ -269,9 +269,6 @@ namespace Slicer { static const Metadata metadata; static const std::string elementName; - - private: - // ModelPartPtr elementModelPart(typename T::value_type &) const; }; template @@ -356,7 +353,7 @@ namespace Slicer { template class ModelPartForStreamRoot : public ModelPartForStreamRootBase { public: - explicit ModelPartForStreamRoot(Stream * s); + using ModelPartForStreamRootBase::ModelPartForStreamRootBase; const std::string & GetRootName() const override; }; diff --git a/slicer/slicer/modelPartsTypes.impl.h b/slicer/slicer/modelPartsTypes.impl.h index 393809f..dc6ff07 100644 --- a/slicer/slicer/modelPartsTypes.impl.h +++ b/slicer/slicer/modelPartsTypes.impl.h @@ -33,47 +33,53 @@ namespace Ice { } #define CUSTOMMODELPARTFOR(Type, BaseModelPart, ModelPartType) \ - template<> DLL_PUBLIC ModelPartPtr ModelPart::Make(typename ModelPartType::element_type * t) \ + template<> \ + DLL_PUBLIC void ModelPart::Make( \ + typename ModelPartType::element_type * t, const ModelPartHandler & h) \ { \ - return std::make_shared(t); \ + h(ModelPartType(t)); \ } \ template<> \ - DLL_PUBLIC ModelPartPtr ModelPart::Make>( \ - Ice::optional * t) \ + DLL_PUBLIC void ModelPart::Make>( \ + Ice::optional * t, const ModelPartHandler & h) \ { \ - return std::make_shared>(t); \ + h(ModelPartForOptional(t)); \ } \ - template<> DLL_PUBLIC ModelPartPtr ModelPart::CreateFor(Default &&) \ + template<> DLL_PUBLIC void ModelPart::CreateFor(Default &&, const ModelPartHandler & h) \ { \ - return Make(nullptr); \ + return Make(nullptr, h); \ } \ - template<> DLL_PUBLIC ModelPartPtr ModelPart::CreateFor(Default> &&) \ + template<> DLL_PUBLIC void ModelPart::CreateFor(Default> &&, const ModelPartHandler & h) \ { \ - return Make>(nullptr); \ + return Make>(nullptr, h); \ } \ - template<> DLL_PUBLIC ModelPartPtr ModelPart::CreateFor(Type & s) \ + template<> DLL_PUBLIC void ModelPart::CreateFor(Type & s, const ModelPartHandler & h) \ { \ - return Make(&s); \ + return Make(&s, h); \ } \ - template<> DLL_PUBLIC ModelPartPtr ModelPart::CreateFor(const Type & s) \ + template<> DLL_PUBLIC void ModelPart::CreateFor(const Type & s, const ModelPartHandler & h) \ { \ - return CreateFor(const_cast(s)); \ + return CreateFor(const_cast(s), h); \ } \ - template<> DLL_PUBLIC ModelPartPtr ModelPart::CreateFor(Ice::optional & s) \ + template<> DLL_PUBLIC void ModelPart::CreateFor(Ice::optional & s, const ModelPartHandler & h) \ { \ - return Make>(&s); \ + return Make>(&s, h); \ } \ - template<> DLL_PUBLIC ModelPartPtr ModelPart::CreateFor(const Ice::optional & s) \ + template<> DLL_PUBLIC void ModelPart::CreateFor(const Ice::optional & s, const ModelPartHandler & h) \ { \ - return CreateFor(const_cast &>(s)); \ + return CreateFor(const_cast &>(s), h); \ } \ template<> DLL_PUBLIC void ModelPart::OnRootFor(Type & s, const ModelPartRootHandler & h) \ { \ - h(ModelPartForRoot(&s)); \ + return CreateFor(s, [&s, &h](auto && mp) { \ + h(ModelPartForRoot(&s, mp)); \ + }); \ } \ template<> DLL_PUBLIC void ModelPart::OnRootFor(Ice::optional & s, const ModelPartRootHandler & h) \ { \ - h(ModelPartForRoot>(&s)); \ + return CreateFor(s, [&s, &h](auto && mp) { \ + h(ModelPartForRoot>(&s, mp)); \ + }); \ } \ template<> DLL_PUBLIC void ModelPart::OnRootFor(const Type & s, const ModelPartRootHandler & h) \ { \ @@ -94,7 +100,8 @@ namespace Ice { DLL_PUBLIC void \ ModelPart::OnRootFor(const StreamImpl & stream, const ModelPartRootHandler & h) \ { \ - h(ModelPartForStreamRoot(const_cast(&stream))); \ + ModelPartForStream strm(const_cast(&stream)); \ + h(ModelPartForStreamRoot(strm)); \ } \ } @@ -111,7 +118,7 @@ namespace Ice { namespace Slicer { // ModelPartForRoot template - ModelPartForRoot::ModelPartForRoot(T * o) : ModelPartForRootBase(ModelPart::CreateFor(*o)), ModelObject(o) + ModelPartForRoot::ModelPartForRoot(T * o, ModelPartParam omp) : ModelPartForRootBase(omp), ModelObject(o) { } @@ -347,7 +354,9 @@ namespace Slicer { ModelPartForComplex::OnEachChild(const ChildHandler & ch) { for (const auto & h : hooks()) { - h->apply(ch, h->Get(GetModel())); + h->On(GetModel(), [h, &ch](auto && mp) { + h->apply(ch, mp); + }); } } @@ -361,8 +370,9 @@ namespace Slicer { }); if (itr != range.end()) { const auto & h = *itr; - auto model = GetModel(); - ch(h->Get(model), h->GetMetadata()); + h->On(GetModel(), [h, &ch](auto && mp) { + ch(mp, h->GetMetadata()); + }); return true; } return false; @@ -394,7 +404,7 @@ namespace Slicer { public: using HookCommon::HookCommon; - virtual ModelPartPtr Get(T * t) const = 0; + virtual void On(T * t, const ModelPartHandler &) const = 0; }; template @@ -417,13 +427,13 @@ namespace Slicer { return hookMetadata; } - ModelPartPtr - Get(T * t) const override + void + On(T * t, const ModelPartHandler & h) const override { if (t) { - return Make(&(t->*member)); + return Make(&(t->*member), h); } - return Make(nullptr); + return Make(nullptr, h); } private: @@ -480,7 +490,7 @@ namespace Slicer { void ModelPartForClass::CreateModelPart(void * p, const ModelPartHandler & h) { - return h(::Slicer::ModelPart::CreateFor(*static_cast(p))); + return ::Slicer::ModelPart::CreateFor(*static_cast(p), h); } template @@ -599,7 +609,9 @@ namespace Slicer { { BOOST_ASSERT(this->Model); for (auto & element : *this->Model) { - ch(elementName, ModelPart::CreateFor(element), NULL); + ModelPart::CreateFor(element, [&ch](auto && mp) { + ch(elementName, mp, nullptr); + }); } } @@ -608,7 +620,9 @@ namespace Slicer { ModelPartForSequence::OnAnonChild(const SubPartHandler & ch, const HookFilter &) { BOOST_ASSERT(this->Model); - ch(ModelPart::CreateFor(this->Model->emplace_back()), emptyMetadata); + ModelPart::CreateFor(this->Model->emplace_back(), [&ch](auto && mp) { + ch(mp, emptyMetadata); + }); return true; } @@ -630,7 +644,7 @@ namespace Slicer { void ModelPartForSequence::OnContained(const ModelPartHandler & h) { - return h(ModelPart::CreateFor(Default {})); + return ModelPart::CreateFor(Default {}, h); } // ModelPartForDictionaryElementInserter @@ -699,7 +713,7 @@ namespace Slicer { void ModelPartForStream::OnContained(const ModelPartHandler & h) { - return h(ModelPart::CreateFor(Default {})); + ModelPart::CreateFor(Default {}, h); } template @@ -708,16 +722,12 @@ namespace Slicer { { BOOST_ASSERT(this->Model); this->Model->Produce([&ch](const T & element) { - ch(ModelPartForSequence>::elementName, ModelPart::CreateFor(element), NULL); + ModelPart::CreateFor(element, [&ch](auto && mp) { + ch(ModelPartForSequence>::elementName, mp, nullptr); + }); }); } - template - ModelPartForStreamRoot::ModelPartForStreamRoot(Stream * s) : - ModelPartForStreamRootBase(std::make_shared>(s)) - { - } - template const std::string & ModelPartForStreamRoot::GetRootName() const diff --git a/slicer/test/compilation.cpp b/slicer/test/compilation.cpp index ba7c568..14bf1d9 100644 --- a/slicer/test/compilation.cpp +++ b/slicer/test/compilation.cpp @@ -23,22 +23,24 @@ BOOST_TEST_DONT_PRINT_LOG_VALUE(std::type_info) BOOST_TEST_DONT_PRINT_LOG_VALUE(Slicer::ModelPartType) // LCOV_EXCL_STOP -#define TypeTest(Var, Expr, Explicit, Expected) \ +#define TypeTest(Var, Expr, Explicit, Expected, ...) \ Var obj = Expr; \ - Slicer::ModelPartPtr mpp = Slicer::ModelPart::CreateFor(obj); \ - BOOST_REQUIRE_EQUAL(Slicer::Expected, mpp->GetType()); \ + Slicer::ModelPart::CreateFor(obj, [](auto && mpp) { \ + BOOST_REQUIRE_EQUAL(Slicer::Expected, mpp->GetType()); \ \ - BOOST_TEST_CONTEXT(#Var) { \ - auto mppvalue = mpp.get(); \ - auto amppvalue = mpp.get(); \ - auto apmppvalue = mpp.get(); \ - BOOST_TEST_CONTEXT(typeid(*mppvalue).name()) { \ - BOOST_REQUIRE_EQUAL(typeid(*mppvalue), typeid(*amppvalue)); \ - BOOST_REQUIRE_EQUAL(typeid(*mppvalue), typeid(*apmppvalue)); \ + BOOST_TEST_CONTEXT(#Var) { \ + auto mppvalue = mpp.get(); \ + auto amppvalue = mpp.get(); \ + auto apmppvalue = mpp.get(); \ + BOOST_TEST_CONTEXT(typeid(*mppvalue).name()) { \ + BOOST_REQUIRE_EQUAL(typeid(*mppvalue), typeid(*amppvalue)); \ + BOOST_REQUIRE_EQUAL(typeid(*mppvalue), typeid(*apmppvalue)); \ + } \ } \ - } + __VA_ARGS__ \ + }); -#define StackTypeTest(Var, Explicit, Expected) TypeTest(Var, Var(), Explicit, Expected) +#define StackTypeTest(Var, Explicit, Expected, ...) TypeTest(Var, Var(), Explicit, Expected, __VA_ARGS__) constexpr auto DontCall = [](auto &&...) { BOOST_ERROR("Shouldn't be called"); @@ -47,8 +49,7 @@ constexpr auto DontCall = [](auto &&...) { BOOST_AUTO_TEST_CASE(compile_auto_modelpart_type_class) { TypeTest(TestModule::BuiltInsPtr, std::make_shared(), ModelPartForClass, - ModelPartType::Complex); - BOOST_CHECK_THROW(mpp->OnContained(DontCall), std::logic_error); + ModelPartType::Complex, { BOOST_CHECK_THROW(mpp->OnContained(DontCall), std::logic_error); }); } static void @@ -64,42 +65,45 @@ hookHandler(std::vector * names, const std::string & name, Slicer:: BOOST_AUTO_TEST_CASE(compile_auto_modelpart_type_sequenceclasses) { - StackTypeTest(TestModule::Classes, ModelPartForSequence, ModelPartType::Sequence); - mpp->OnContained([](auto && cmpp) { - BOOST_REQUIRE(cmpp); - BOOST_REQUIRE_EQUAL(Slicer::ModelPartType::Complex, cmpp->GetType()); - std::vector names; - cmpp->OnEachChild([&](auto && PH1, auto && PH2, auto && PH3) { - hookHandler(&names, PH1, PH2, PH3); + StackTypeTest(TestModule::Classes, ModelPartForSequence, ModelPartType::Sequence, { + mpp->OnContained([](auto && cmpp) { + BOOST_REQUIRE(cmpp); + BOOST_REQUIRE_EQUAL(Slicer::ModelPartType::Complex, cmpp->GetType()); + std::vector names; + cmpp->OnEachChild([&](auto && PH1, auto && PH2, auto && PH3) { + hookHandler(&names, PH1, PH2, PH3); + }); + BOOST_REQUIRE_EQUAL(2, names.size()); + BOOST_REQUIRE_EQUAL("a", names.front()); + BOOST_REQUIRE_EQUAL("b", names.back()); }); - 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, ModelPartType::Sequence); - mpp->OnContained([](auto && cmpp) { - BOOST_REQUIRE(cmpp); - BOOST_REQUIRE_EQUAL(Slicer::ModelPartType::Complex, cmpp->GetType()); - std::vector names; - cmpp->OnEachChild([&](auto && PH1, auto && PH2, auto && PH3) { - hookHandler(&names, PH1, PH2, PH3); + StackTypeTest(TestModule::Structs, ModelPartForSequence, ModelPartType::Sequence, { + mpp->OnContained([](auto && cmpp) { + BOOST_REQUIRE(cmpp); + BOOST_REQUIRE_EQUAL(Slicer::ModelPartType::Complex, cmpp->GetType()); + std::vector names; + cmpp->OnEachChild([&](auto && PH1, auto && PH2, auto && PH3) { + hookHandler(&names, PH1, PH2, PH3); + }); + BOOST_REQUIRE_EQUAL(2, names.size()); + BOOST_REQUIRE_EQUAL("a", names.front()); + BOOST_REQUIRE_EQUAL("b", names.back()); }); - 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, ModelPartType::Dictionary); - mpp->OnContained([](auto && cmpp) { - BOOST_REQUIRE(cmpp); - BOOST_REQUIRE_EQUAL(Slicer::ModelPartType::Complex, cmpp->GetType()); + StackTypeTest(TestModule::ClassMap, ModelPartForDictionary, ModelPartType::Dictionary, { + mpp->OnContained([](auto && cmpp) { + BOOST_REQUIRE(cmpp); + BOOST_REQUIRE_EQUAL(Slicer::ModelPartType::Complex, cmpp->GetType()); + }); }); } @@ -110,124 +114,130 @@ BOOST_AUTO_TEST_CASE(compile_auto_modelpart_type_mapstructs) BOOST_AUTO_TEST_CASE(compile_auto_modelpart_type_bi_string) { - StackTypeTest(std::string, ModelPartForSimple, ModelPartType::Simple); - BOOST_CHECK_THROW(mpp->OnContained(DontCall), std::logic_error); + StackTypeTest(std::string, ModelPartForSimple, ModelPartType::Simple, + { BOOST_CHECK_THROW(mpp->OnContained(DontCall), std::logic_error); }); } BOOST_AUTO_TEST_CASE(compile_auto_modelpart_type_bi_bool) { - StackTypeTest(bool, ModelPartForSimple, ModelPartType::Simple); - BOOST_CHECK_THROW(mpp->OnContained(DontCall), std::logic_error); + StackTypeTest(bool, ModelPartForSimple, ModelPartType::Simple, + { BOOST_CHECK_THROW(mpp->OnContained(DontCall), std::logic_error); }); } BOOST_AUTO_TEST_CASE(compile_auto_modelpart_type_bi_float) { - StackTypeTest(Ice::Float, ModelPartForSimple, ModelPartType::Simple); - BOOST_CHECK_THROW(mpp->OnContained(DontCall), std::logic_error); + StackTypeTest(Ice::Float, ModelPartForSimple, ModelPartType::Simple, + { BOOST_CHECK_THROW(mpp->OnContained(DontCall), std::logic_error); }); } BOOST_AUTO_TEST_CASE(compile_auto_modelpart_type_bi_double) { - StackTypeTest(Ice::Double, ModelPartForSimple, ModelPartType::Simple); - BOOST_CHECK_THROW(mpp->OnContained(DontCall), std::logic_error); + StackTypeTest(Ice::Double, ModelPartForSimple, ModelPartType::Simple, + { BOOST_CHECK_THROW(mpp->OnContained(DontCall), std::logic_error); }); } BOOST_AUTO_TEST_CASE(compile_auto_modelpart_type_bi_byte) { - StackTypeTest(Ice::Byte, ModelPartForSimple, ModelPartType::Simple); - BOOST_CHECK_THROW(mpp->OnContained(DontCall), std::logic_error); + StackTypeTest(Ice::Byte, ModelPartForSimple, ModelPartType::Simple, + { BOOST_CHECK_THROW(mpp->OnContained(DontCall), std::logic_error); }); } BOOST_AUTO_TEST_CASE(compile_auto_modelpart_type_bi_short) { - StackTypeTest(Ice::Short, ModelPartForSimple, ModelPartType::Simple); - BOOST_CHECK_THROW(mpp->OnContained(DontCall), std::logic_error); + StackTypeTest(Ice::Short, ModelPartForSimple, ModelPartType::Simple, + { BOOST_CHECK_THROW(mpp->OnContained(DontCall), std::logic_error); }); } BOOST_AUTO_TEST_CASE(compile_auto_modelpart_type_bi_int) { - StackTypeTest(Ice::Int, ModelPartForSimple, ModelPartType::Simple); - BOOST_CHECK_THROW(mpp->OnContained(DontCall), std::logic_error); + StackTypeTest(Ice::Int, ModelPartForSimple, ModelPartType::Simple, + { BOOST_CHECK_THROW(mpp->OnContained(DontCall), std::logic_error); }); } BOOST_AUTO_TEST_CASE(compile_auto_modelpart_type_bi_long) { - StackTypeTest(Ice::Long, ModelPartForSimple, ModelPartType::Simple); - BOOST_CHECK_THROW(mpp->OnContained(DontCall), std::logic_error); + StackTypeTest(Ice::Long, ModelPartForSimple, ModelPartType::Simple, + { BOOST_CHECK_THROW(mpp->OnContained(DontCall), std::logic_error); }); } BOOST_AUTO_TEST_CASE(compile_auto_modelpart_type_struct) { - StackTypeTest(TestModule::StructType, ModelPartForStruct, ModelPartType::Complex); - BOOST_CHECK_THROW(mpp->OnContained(DontCall), std::logic_error); + StackTypeTest(TestModule::StructType, ModelPartForStruct, ModelPartType::Complex, + { BOOST_CHECK_THROW(mpp->OnContained(DontCall), std::logic_error); }); } BOOST_AUTO_TEST_CASE(compile_auto_modelpart_type_enum) { - StackTypeTest(TestModule::SomeNumbers, ModelPartForEnum, ModelPartType::Simple); - BOOST_CHECK_THROW(mpp->OnContained(DontCall), std::logic_error); + StackTypeTest(TestModule::SomeNumbers, ModelPartForEnum, ModelPartType::Simple, + { BOOST_CHECK_THROW(mpp->OnContained(DontCall), std::logic_error); }); } BOOST_AUTO_TEST_CASE(normalClassTypeId) { TestModule::BasePtr base = std::make_shared(1); BOOST_REQUIRE(base); - auto a = Slicer::ModelPart::CreateFor(base); - BOOST_REQUIRE(a); - auto baseType = a->GetTypeId(); - BOOST_REQUIRE(!baseType); + Slicer::ModelPart::CreateFor(base, [](auto && a) { + BOOST_REQUIRE(a); + auto baseType = a->GetTypeId(); + BOOST_REQUIRE(!baseType); + }); } BOOST_AUTO_TEST_CASE(normalSubClassTypeId) { TestModule::BasePtr base = std::make_shared(1, 2); BOOST_REQUIRE(base); - auto a = Slicer::ModelPart::CreateFor(base); - BOOST_REQUIRE(a); - auto baseType = a->GetTypeId(); - BOOST_REQUIRE(baseType); - BOOST_REQUIRE_EQUAL(*baseType, "::TestModule::D1"); + Slicer::ModelPart::CreateFor(base, [](auto && a) { + BOOST_REQUIRE(a); + auto baseType = a->GetTypeId(); + BOOST_REQUIRE(baseType); + BOOST_REQUIRE_EQUAL(*baseType, "::TestModule::D1"); + }); } BOOST_AUTO_TEST_CASE(normalSubSubClassTypeId) { TestModule::BasePtr base = std::make_shared(1, 2, 3); BOOST_REQUIRE(base); - auto a = Slicer::ModelPart::CreateFor(base); - BOOST_REQUIRE(a); - auto baseType = a->GetTypeId(); - BOOST_REQUIRE(baseType); - BOOST_REQUIRE_EQUAL(*baseType, "::TestModule::D3"); + Slicer::ModelPart::CreateFor(base, [](auto && a) { + BOOST_REQUIRE(a); + auto baseType = a->GetTypeId(); + BOOST_REQUIRE(baseType); + BOOST_REQUIRE_EQUAL(*baseType, "::TestModule::D3"); + }); } BOOST_AUTO_TEST_CASE(localClassTypeId) { Locals::LocalClassPtr base = std::make_shared(1, "One"); BOOST_REQUIRE(base); - auto a = Slicer::ModelPart::CreateFor(base); - BOOST_REQUIRE(a); - auto baseType = a->GetTypeId(); - BOOST_REQUIRE(!baseType); + Slicer::ModelPart::CreateFor(base, [](auto && a) { + BOOST_REQUIRE(a); + auto baseType = a->GetTypeId(); + BOOST_REQUIRE(!baseType); + }); } BOOST_AUTO_TEST_CASE(localSubClassTypeId) { Locals::LocalClassPtr base = std::make_shared(1, "One", 3.1); BOOST_REQUIRE(base); - auto a = Slicer::ModelPart::CreateFor(base); - BOOST_REQUIRE(a); - auto baseType = a->GetTypeId(); - BOOST_REQUIRE(baseType); - BOOST_REQUIRE_EQUAL(*baseType, "::Locals::LocalSubClass"); + Slicer::ModelPart::CreateFor(base, [](auto && a) { + BOOST_REQUIRE(a); + auto baseType = a->GetTypeId(); + BOOST_REQUIRE(baseType); + BOOST_REQUIRE_EQUAL(*baseType, "::Locals::LocalSubClass"); + }); } BOOST_AUTO_TEST_CASE(localSubSubClassTypeId) { Locals::LocalClassPtr base = std::make_shared(1, "One", 3.1, 1); BOOST_REQUIRE(base); - auto a = Slicer::ModelPart::CreateFor(base); - BOOST_REQUIRE(a); - auto baseType = a->GetTypeId(); - BOOST_REQUIRE(baseType); - BOOST_REQUIRE_EQUAL(*baseType, "::Locals::LocalSub2Class"); + Slicer::ModelPart::CreateFor(base, [](auto && a) { + BOOST_REQUIRE(a); + auto baseType = a->GetTypeId(); + BOOST_REQUIRE(baseType); + BOOST_REQUIRE_EQUAL(*baseType, "::Locals::LocalSub2Class"); + }); } diff --git a/slicer/test/conversions.cpp b/slicer/test/conversions.cpp index a296197..193cd4f 100644 --- a/slicer/test/conversions.cpp +++ b/slicer/test/conversions.cpp @@ -175,9 +175,9 @@ namespace TestModule { namespace Slicer { template<> - DLL_PUBLIC ModelPartPtr - ModelPart::Make(::Ice::Short * m) + DLL_PUBLIC void + ModelPart::Make(::Ice::Short * m, const ModelPartHandler & h) { - return std::make_shared(m); + return h(TestModule::MonthValidator(m)); } } diff --git a/slicer/test/serializers.cpp b/slicer/test/serializers.cpp index 6a41cdd..f02dbaf 100644 --- a/slicer/test/serializers.cpp +++ b/slicer/test/serializers.cpp @@ -710,6 +710,8 @@ BOOST_AUTO_TEST_CASE(enum_lookups) BOOST_AUTO_TEST_CASE(sequence_element_in_same_slice_link_bug) { // Link error when sequence element type defined in same slice. - BOOST_CHECK(Slicer::ModelPart::Make>(nullptr)); - BOOST_CHECK(Slicer::ModelPart::Make>(nullptr)); + BOOST_CHECK_NO_THROW( + Slicer::ModelPart::Make>(nullptr, [](auto &&) {})); + BOOST_CHECK_NO_THROW( + Slicer::ModelPart::Make>(nullptr, [](auto &&) {})); } diff --git a/slicer/tool/parser.cpp b/slicer/tool/parser.cpp index 28252b5..55a06fe 100644 --- a/slicer/tool/parser.cpp +++ b/slicer/tool/parser.cpp @@ -238,13 +238,13 @@ namespace Slicer { fprintbf(cpp, "\tconversion_fail(\"%s\");\n", Slice::typeToString(type)); fprintbf(cpp, "}\n\n"); - fprintbf(cpp, "\ttemplate<> DLL_PUBLIC ModelPartPtr ModelPart::Make<"); + fprintbf(cpp, "\ttemplate<> DLL_PUBLIC void ModelPart::Make<"); createModelPartForConverted(type, c->scoped(), dm); fprintbf(cpp, ">(typename "); createModelPartForConverted(type, c->scoped(), dm); - fprintbf(cpp, "::element_type * t) { return std::make_shared<"); + fprintbf(cpp, "::element_type * t, const ModelPartHandler & h) { return h("); createModelPartForConverted(type, c->scoped(), dm); - fprintbf(cpp, ">(t); } \n"); + fprintbf(cpp, "(t)); } \n"); } } @@ -444,9 +444,9 @@ namespace Slicer { if (auto cmp = md.value("slicer:custommodelpart:")) { fprintbf(cpp, "CUSTOMMODELPARTFOR(%s, %s< %s >, %s)\n\n", Slice::typeToString(decl), getBasicModelPart(decl), c->scoped(), CppName {*cmp}); - fprintbf(cpp, "\ttemplate<> DLL_PUBLIC ModelPartPtr ModelPart::Make<%s<%s> >(%s * t)", + fprintbf(cpp, "\ttemplate<> DLL_PUBLIC void ModelPart::Make<%s<%s> >(%s * t, const ModelPartHandler & h)", getBasicModelPart(decl), c->scoped(), Slice::typeToString(decl)); - fprintbf(cpp, "{ return std::make_shared<%s>(t); } \n", CppName {*cmp}); + fprintbf(cpp, "{ return h(%s(t)); } \n", CppName {*cmp}); } else { fprintbf(cpp, "CUSTOMMODELPARTFOR(%s, ModelPartForClass<%s>, ModelPartForClass<%s>)\n\n", @@ -764,9 +764,9 @@ namespace Slicer { if (auto cmp = metadata.value("slicer:custommodelpart:")) { fprintbf(cpp, "CUSTOMMODELPARTFOR(%s, %s< %s >, %s)\n\n", type, getBasicModelPart(stype), type, CppName {*cmp}); - fprintbf(cpp, "\ttemplate<> DLL_PUBLIC ModelPartPtr ModelPart::Make<%s<%s>>(%s * t)", + fprintbf(cpp, "\ttemplate<> DLL_PUBLIC void ModelPart::Make<%s<%s>>(%s * t, const ModelPartHandler & h)", getBasicModelPart(stype), type, type); - fprintbf(cpp, "{ return std::make_shared<%s>(t); } \n", CppName {*cmp}); + fprintbf(cpp, "{ return h(%s(t)); } \n", CppName {*cmp}); } else { fprintbf(cpp, "MODELPARTFOR(%s, %s)\n\n", type, getBasicModelPart(stype)); -- cgit v1.2.3