From f1347e820c8262bec27cac1e98f5eaf8e6767d5b Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 30 Nov 2014 03:08:33 +0000 Subject: Support for automatically determining a model part implementation --- slicer/slicer/modelParts.cpp | 13 ++++++ slicer/slicer/modelParts.h | 26 +++++++++++ slicer/test/Jamfile.jam | 13 ++++++ slicer/test/compilation.cpp | 102 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 154 insertions(+) create mode 100644 slicer/test/compilation.cpp diff --git a/slicer/slicer/modelParts.cpp b/slicer/slicer/modelParts.cpp index 80d90cf..480ec1d 100644 --- a/slicer/slicer/modelParts.cpp +++ b/slicer/slicer/modelParts.cpp @@ -90,5 +90,18 @@ namespace Slicer { { return emptyMetadata; } + +#define MODELPARTFOR(Type, ModelPart) \ + ModelPartPtr ModelPartFor(Type & t) { return new ModelPart< Type >(t); } \ + ModelPartPtr ModelPartFor(Type * t) { return new ModelPart< Type >(t); } + MODELPARTFOR(std::string, ModelPartForSimple); + MODELPARTFOR(bool, ModelPartForSimple); + MODELPARTFOR(Ice::Float, ModelPartForSimple); + MODELPARTFOR(Ice::Double, ModelPartForSimple); + MODELPARTFOR(Ice::Byte, ModelPartForSimple); + MODELPARTFOR(Ice::Short, ModelPartForSimple); + MODELPARTFOR(Ice::Int, ModelPartForSimple); + MODELPARTFOR(Ice::Long, ModelPartForSimple); +#undef MODELPARTFOR } diff --git a/slicer/slicer/modelParts.h b/slicer/slicer/modelParts.h index da118bd..bd3889b 100644 --- a/slicer/slicer/modelParts.h +++ b/slicer/slicer/modelParts.h @@ -546,6 +546,32 @@ namespace Slicer { static std::string pairName; static Metadata metadata; }; + + // Templates for automatically determining a model part implementation +#define templateMODELPARTFOR(Type, ModelPart) \ + template ModelPartPtr ModelPartFor(Type & t) { return new ModelPart< Type >(t); } \ + template ModelPartPtr ModelPartFor(Type * t) { return new ModelPart< Type >(t); } +#define MODELPARTFOR(Type, ModelPart) \ + ModelPartPtr ModelPartFor(Type & t); \ + ModelPartPtr ModelPartFor(Type * t); + + templateMODELPARTFOR(IceInternal::Handle, ModelPartForClass); + templateMODELPARTFOR(std::vector, ModelPartForSequence); + templateMODELPARTFOR(std::list, ModelPartForSequence); + template ModelPartPtr ModelPartFor(std::map & t) { return new ModelPartForDictionary< std::map >(t); } \ + template ModelPartPtr ModelPartFor(std::map * t) { return new ModelPartForDictionary< std::map >(t); } \ + MODELPARTFOR(std::string, ModelPartForSimple); + MODELPARTFOR(bool, ModelPartForSimple); + MODELPARTFOR(Ice::Float, ModelPartForSimple); + MODELPARTFOR(Ice::Double, ModelPartForSimple); + MODELPARTFOR(Ice::Byte, ModelPartForSimple); + MODELPARTFOR(Ice::Short, ModelPartForSimple); + MODELPARTFOR(Ice::Int, ModelPartForSimple); + MODELPARTFOR(Ice::Long, ModelPartForSimple); + // Everything else is a struct? + templateMODELPARTFOR(T, ModelPartForStruct); +#undef templateMODELPARTFOR +#undef MODELPARTFOR } #endif diff --git a/slicer/test/Jamfile.jam b/slicer/test/Jamfile.jam index ffedb00..dde7362 100644 --- a/slicer/test/Jamfile.jam +++ b/slicer/test/Jamfile.jam @@ -53,6 +53,19 @@ unit-test preprocess : ../slicer//slicer ; +unit-test compilation : + compilation.cpp + : + .. + ../slicer//slicer + types + preprocess + slicer-test + common + .. + ../slicer//slicer + ; + unit-test serializers : serializers.cpp : diff --git a/slicer/test/compilation.cpp b/slicer/test/compilation.cpp new file mode 100644 index 0000000..d48afd0 --- /dev/null +++ b/slicer/test/compilation.cpp @@ -0,0 +1,102 @@ +#define BOOST_TEST_MODULE compilation +#include + +#include +#include + +namespace std { + ostream & operator<<(ostream & strm, const type_info & ti) + { + strm << ti.name(); + return strm; + } +} + +#define TypeTest(Var, Expr, Explicit, Expected) \ + Var obj = Expr; \ + Slicer::ModelPartPtr mpp = new Slicer::Explicit(obj); \ + BOOST_REQUIRE_EQUAL(Slicer::Expected, mpp->GetType()); \ + \ + Slicer::ModelPartPtr autoMpp = Slicer::ModelPartFor(obj); \ + BOOST_REQUIRE_EQUAL(Slicer::Expected, autoMpp->GetType()); \ + \ + Slicer::ModelPartPtr autoPtrMpp = Slicer::ModelPartFor(&obj); \ + BOOST_REQUIRE_EQUAL(Slicer::Expected, autoPtrMpp->GetType()); \ +\ + BOOST_TEST_MESSAGE(typeid(*mpp.get())); \ + BOOST_REQUIRE_EQUAL(typeid(*mpp.get()), typeid(*autoMpp.get())); \ + BOOST_REQUIRE_EQUAL(typeid(*mpp.get()), typeid(*autoPtrMpp.get())); + +#define StackTypeTest(Var, Explicit, Expected) \ + TypeTest(Var, Var(), Explicit, Expected) + +BOOST_AUTO_TEST_CASE( compile_auto_modelpart_type_class ) +{ + TypeTest(TestModule::BuiltInsPtr, new TestModule::BuiltIns(), ModelPartForClass, mpt_Complex); +} + +BOOST_AUTO_TEST_CASE( compile_auto_modelpart_type_sequenceclasses ) +{ + StackTypeTest(TestModule::Classes, ModelPartForSequence, mpt_Sequence); +} + +BOOST_AUTO_TEST_CASE( compile_auto_modelpart_type_sequencestructs ) +{ + StackTypeTest(TestModule::Structs, ModelPartForSequence, mpt_Sequence); +} + +BOOST_AUTO_TEST_CASE( compile_auto_modelpart_type_mapclasses ) +{ + StackTypeTest(TestModule::ClassMap, ModelPartForDictionary, mpt_Dictionary); +} + +BOOST_AUTO_TEST_CASE( compile_auto_modelpart_type_mapstructs ) +{ + StackTypeTest(TestModule::StructMap, ModelPartForDictionary, mpt_Dictionary); +} + +BOOST_AUTO_TEST_CASE( compile_auto_modelpart_type_bi_string ) +{ + StackTypeTest(std::string, ModelPartForSimple, mpt_Simple); +} + +BOOST_AUTO_TEST_CASE( compile_auto_modelpart_type_bi_bool ) +{ + StackTypeTest(bool, ModelPartForSimple, mpt_Simple); +} + +BOOST_AUTO_TEST_CASE( compile_auto_modelpart_type_bi_float ) +{ + StackTypeTest(Ice::Float, ModelPartForSimple, mpt_Simple); +} + +BOOST_AUTO_TEST_CASE( compile_auto_modelpart_type_bi_double ) +{ + StackTypeTest(Ice::Double, ModelPartForSimple, mpt_Simple); +} + +BOOST_AUTO_TEST_CASE( compile_auto_modelpart_type_bi_byte ) +{ + StackTypeTest(Ice::Byte, ModelPartForSimple, mpt_Simple); +} + +BOOST_AUTO_TEST_CASE( compile_auto_modelpart_type_bi_short ) +{ + StackTypeTest(Ice::Short, ModelPartForSimple, mpt_Simple); +} + +BOOST_AUTO_TEST_CASE( compile_auto_modelpart_type_bi_int ) +{ + StackTypeTest(Ice::Int, ModelPartForSimple, mpt_Simple); +} + +BOOST_AUTO_TEST_CASE( compile_auto_modelpart_type_bi_long ) +{ + StackTypeTest(Ice::Long, ModelPartForSimple, mpt_Simple); +} + +BOOST_AUTO_TEST_CASE( compile_auto_modelpart_type_struct ) +{ + StackTypeTest(TestModule::StructType, ModelPartForStruct, mpt_Complex); +} + -- cgit v1.2.3