From acc9fee883103466fc1277926772e0f5dbd79c8a Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Wed, 31 Aug 2016 23:27:41 +0100 Subject: Centralize and publicly expose string/enum lookup functions as helpers --- slicer/slicer/modelPartsTypes.h | 2 ++ slicer/slicer/modelPartsTypes.impl.h | 20 ++++++++++++++++++++ slicer/slicer/parser.cpp | 14 ++++++-------- 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/slicer/slicer/modelPartsTypes.h b/slicer/slicer/modelPartsTypes.h index c2e8cfe..0886ed6 100644 --- a/slicer/slicer/modelPartsTypes.h +++ b/slicer/slicer/modelPartsTypes.h @@ -205,6 +205,8 @@ namespace Slicer { static const Metadata metadata; static const Enumerations enumerations; + DLL_PUBLIC static const std::string & lookup(T); + DLL_PUBLIC static T lookup(const std::string &); private: T & modelPart; diff --git a/slicer/slicer/modelPartsTypes.impl.h b/slicer/slicer/modelPartsTypes.impl.h index 7f91508..d598bbf 100644 --- a/slicer/slicer/modelPartsTypes.impl.h +++ b/slicer/slicer/modelPartsTypes.impl.h @@ -320,6 +320,26 @@ namespace Slicer { return metadata; } + template + T ModelPartForEnum::lookup(const std::string & val) + { + auto i = enumerations.right.find(val); + if (i == enumerations.right.end()) { + throw InvalidEnumerationValue(val, typeid(T).name()); + } + return i->second; + } + + template + const std::string & ModelPartForEnum::lookup(T val) + { + auto i = enumerations.left.find(val); + if (i == enumerations.left.end()) { + throw InvalidEnumerationValue((::Ice::Int)val, typeid(T).name()); + } + return i->second; + } + // ModelPartForDictionaryElementInserter template ModelPartForDictionaryElementInserter::ModelPartForDictionaryElementInserter(T & d) : diff --git a/slicer/slicer/parser.cpp b/slicer/slicer/parser.cpp index 6a5019b..efe53dd 100644 --- a/slicer/slicer/parser.cpp +++ b/slicer/slicer/parser.cpp @@ -358,18 +358,12 @@ namespace Slicer { fprintbf(cpp, "template<>\nvoid ModelPartForEnum< %s >::SetValue(ValueSourcePtr s) {\n\ std::string val;\n\ s->set(val);\n\ - auto i = enumerations.right.find(val);\n\ - if (i == enumerations.right.end()) throw InvalidEnumerationValue(val, \"%s\");\n\ - modelPart = i->second;\n\ + modelPart = lookup(val);\n\ }\n\n", - e->scoped(), e->scoped()); fprintbf(cpp, "template<>\nvoid ModelPartForEnum< %s >::GetValue(ValueTargetPtr s) {\n\ - auto i = enumerations.left.find(modelPart);\n\ - if (i == enumerations.left.end()) throw InvalidEnumerationValue((::Ice::Int)modelPart, \"%s\");\n\ - s->get(i->second);\n\ + s->get(lookup(modelPart));\n\ }\n\n", - e->scoped(), e->scoped()); auto name = metaDataValue("slicer:root:", e->getMetaData()); @@ -377,6 +371,10 @@ namespace Slicer { fprintbf(cpp, "MODELPARTFOR(%s, ModelPartForEnum);\n\n", e->scoped()); + fprintbf(cpp, "template %s ModelPartForEnum< %s >::lookup(const std::string &);\n\n", + e->scoped(), e->scoped()); + fprintbf(cpp, "template const std::string & ModelPartForEnum< %s >::lookup(%s);\n\n", + e->scoped(), e->scoped()); } void -- cgit v1.2.3