From e483ddac186e6192c5f6f076f912c57428f17fef Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 20 Nov 2020 19:28:06 +0000 Subject: Remove templateness of enum find, unnecessary, overloading better --- slicer/slicer/enum-test.cpp | 10 +++++----- slicer/slicer/enumMap.h | 28 +++++++++++++--------------- slicer/slicer/modelPartsTypes.h | 2 +- slicer/slicer/modelPartsTypes.impl.h | 12 +++++------- 4 files changed, 24 insertions(+), 28 deletions(-) diff --git a/slicer/slicer/enum-test.cpp b/slicer/slicer/enum-test.cpp index 43e191a..04ca9df 100644 --- a/slicer/slicer/enum-test.cpp +++ b/slicer/slicer/enum-test.cpp @@ -16,9 +16,9 @@ namespace test { static_assert(em.arr[1].nameStr == &two); static_assert(em.arr[2].value == Es::three); - static_assert(em.find("one")->value == Es::one); - static_assert(em.find("three")->value == Es::three); - static_assert(em.find(Es::one)->name == "one"); - static_assert(em.find(Es::three)->name == "three"); - static_assert(!em.find("four")); + static_assert(em.find("one")->value == Es::one); + static_assert(em.find("three")->value == Es::three); + static_assert(em.find(Es::one)->name == "one"); + static_assert(em.find(Es::three)->name == "three"); + static_assert(!em.find("four")); } diff --git a/slicer/slicer/enumMap.h b/slicer/slicer/enumMap.h index 8f63819..72c1511 100644 --- a/slicer/slicer/enumMap.h +++ b/slicer/slicer/enumMap.h @@ -6,11 +6,6 @@ #include namespace Slicer { - enum class EnumMapKey { - Value, - Name, - }; - template class EnumMap { public: struct Node { @@ -19,20 +14,23 @@ namespace Slicer { const std::string * nameStr {}; }; - template [[nodiscard]] constexpr inline const Node * - find(const T & v) const noexcept + find(std::string_view v) const noexcept { for (auto b = begin; b != end; b++) { - if constexpr (Key == EnumMapKey::Value) { - if (b->value == v) { - return b; - } + if (b->name == v) { + return b; } - else if constexpr (Key == EnumMapKey::Name) { - if (b->name == v) { - return b; - } + } + return nullptr; + } + + [[nodiscard]] constexpr inline const Node * + find(E v) const noexcept + { + for (auto b = begin; b != end; b++) { + if (b->value == v) { + return b; } } return nullptr; diff --git a/slicer/slicer/modelPartsTypes.h b/slicer/slicer/modelPartsTypes.h index de0bc29..2d959ea 100644 --- a/slicer/slicer/modelPartsTypes.h +++ b/slicer/slicer/modelPartsTypes.h @@ -232,7 +232,7 @@ namespace Slicer { static const Metadata metadata; static constexpr const EnumMap & enumerations(); DLL_PUBLIC static const std::string & lookup(T); - DLL_PUBLIC static T lookup(const std::string_view &); + DLL_PUBLIC static T lookup(std::string_view); }; class DLL_PUBLIC ModelPartForSequenceBase : public ModelPart { diff --git a/slicer/slicer/modelPartsTypes.impl.h b/slicer/slicer/modelPartsTypes.impl.h index be23e1f..346a894 100644 --- a/slicer/slicer/modelPartsTypes.impl.h +++ b/slicer/slicer/modelPartsTypes.impl.h @@ -518,11 +518,11 @@ namespace Slicer { return metadata; } - template + template inline const auto * ModelPartForEnumLookup(const EnumMap & enumerations, const V & val) { - if (auto i = enumerations.template find(val)) { + if (auto i = enumerations.find(val)) { return i; } throw Ex(ExP(val), typeid(T).name()); @@ -530,18 +530,16 @@ namespace Slicer { template T - ModelPartForEnum::lookup(const std::string_view & val) + ModelPartForEnum::lookup(std::string_view val) { - return ModelPartForEnumLookup(enumerations(), val) - ->value; + return ModelPartForEnumLookup(enumerations(), val)->value; } template const std::string & ModelPartForEnum::lookup(T val) { - return *ModelPartForEnumLookup(enumerations(), val) - ->nameStr; + return *ModelPartForEnumLookup(enumerations(), val)->nameStr; } template -- cgit v1.2.3