From f1e51b001d8359d6350d0244972051b41bf7217f Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 26 Apr 2020 13:37:41 +0100 Subject: Remvoe some duplication in enums --- slicer/slicer/modelPartsTypes.impl.h | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/slicer/slicer/modelPartsTypes.impl.h b/slicer/slicer/modelPartsTypes.impl.h index 5153b2e..0c1a92d 100644 --- a/slicer/slicer/modelPartsTypes.impl.h +++ b/slicer/slicer/modelPartsTypes.impl.h @@ -555,12 +555,15 @@ namespace Slicer { } // ModelPartForEnum + template + using EnumPair = std::pair; + template class ModelPartForEnum::Enumerations : public boost::multi_index_container< - std::pair, + EnumPair, boost::multi_index::indexed_by< - boost::multi_index::ordered_unique, const T, &std::pair::first>>, - boost::multi_index::ordered_unique, const std::string, &std::pair::second>, std::less<>> + boost::multi_index::ordered_unique, const T, &EnumPair::first>>, + boost::multi_index::ordered_unique, const std::string, &EnumPair::second>, std::less<>> >> { }; @@ -576,26 +579,29 @@ namespace Slicer { return metadata; } + template + inline const auto & ModelPartForEnumLookup(const typename ModelPartForEnum::Enumerations & enumerations, + const V & val, R EnumPair::* rv) + { + const auto & side = enumerations.template get(); + if (auto i = side.find(val); i != side.end()) { + return (*i).*rv; + } + throw Ex(ExP(val), typeid(T).name()); + } + template T ModelPartForEnum::lookup(const std::string_view & val) { - auto & right = enumerations.template get<1>(); - auto i = right.find(val); - if (i == right.end()) { - throw InvalidEnumerationSymbol(std::string(val), typeid(T).name()); - } - return i->first; + return ModelPartForEnumLookup<1, InvalidEnumerationSymbol, std::string, T>(enumerations, val, + &EnumPair::first); } template const std::string & ModelPartForEnum::lookup(T val) { - auto & left = enumerations.template get<0>(); - auto i = left.find(val); - if (i == left.end()) { - throw InvalidEnumerationValue((::Ice::Int)val, typeid(T).name()); - } - return i->second; + return ModelPartForEnumLookup<0, InvalidEnumerationValue, ::Ice::Int, T>(enumerations, val, + &EnumPair::second); } template -- cgit v1.2.3