summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2017-07-16 14:24:44 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2017-07-16 14:24:44 +0100
commitcccc507ce9ecc7a5902e162473b8945323230389 (patch)
treed621ff3d37885f4ba739640c10a8a25d259be027
parentGroup enum functions properly (diff)
downloadslicer-cccc507ce9ecc7a5902e162473b8945323230389.tar.bz2
slicer-cccc507ce9ecc7a5902e162473b8945323230389.tar.xz
slicer-cccc507ce9ecc7a5902e162473b8945323230389.zip
Support getting a ModelPart for the elements in a container (with no actual model)
-rw-r--r--slicer/slicer/modelParts.cpp6
-rw-r--r--slicer/slicer/modelParts.h3
-rw-r--r--slicer/slicer/modelPartsTypes.h6
-rw-r--r--slicer/slicer/modelPartsTypes.impl.h16
-rw-r--r--slicer/test/compilation.cpp41
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
@@ -151,6 +151,8 @@ namespace Slicer {
virtual ~ModelPart() = default;
template<typename T>
+ static ModelPartPtr CreateFor();
+ template<typename T>
static ModelPartPtr CreateFor(T & t);
template<typename T>
static ModelPartForRootPtr CreateRootFor(T & t);
@@ -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<typename std::remove_const<MT>::type *>(&(t->*member))) : NULL;
+ return new MP(t ? const_cast<typename std::remove_const<MT>::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<Type>; \
template class ModelPartForRoot< IceUtil::Optional<Type> >; \
+ template<> ModelPartPtr ModelPart::CreateFor<Type>() { return new ModelPartType(nullptr); } \
template<> ModelPartPtr ModelPart::CreateFor(Type & s) { return new ModelPartType(&s); } \
template<> ModelPartPtr ModelPart::CreateFor(IceUtil::Optional<Type> & s) { return new ModelPartForOptional<ModelPartType>(&s); } \
template<> ModelPartForRootPtr ModelPart::CreateRootFor(Type & s) { return new ModelPartForRoot<Type>(&s); } \
@@ -258,8 +259,7 @@ namespace Slicer {
template<typename T>
T * ModelPartForClass<T>::GetModel()
{
- BOOST_ASSERT(this->Model);
- return this->Model->get();
+ return this->Model ? this->Model->get() : nullptr;
}
template<typename T>
@@ -327,7 +327,6 @@ namespace Slicer {
template<typename T>
T * ModelPartForStruct<T>::GetModel()
{
- BOOST_ASSERT(this->Model);
return this->Model;
}
@@ -400,6 +399,12 @@ namespace Slicer {
return metadata;
}
+ template<typename T>
+ ModelPartPtr ModelPartForSequence<T>::GetContainedModelPart()
+ {
+ return ModelPart::CreateFor<typename T::value_type>();
+ }
+
// ModelPartForDictionaryElementInserter
template<typename T>
ModelPartForDictionaryElementInserter<T>::ModelPartForDictionaryElementInserter(T * d) :
@@ -453,6 +458,11 @@ namespace Slicer {
return metadata;
}
+ template<typename T>
+ ModelPartPtr ModelPartForDictionary<T>::GetContainedModelPart()
+ {
+ return new ModelPartForStruct<typename T::value_type>(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<std::string> * 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<std::string> 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<std::string> 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());
}