From a53e4a57565d3375a978effbbc32d3eed6aac7e3 Mon Sep 17 00:00:00 2001 From: Shauren Date: Sat, 7 Jan 2023 22:38:21 +0100 Subject: Core/Misc: Replace enable_if overload selection with if constexpr --- src/common/Utilities/Containers.h | 76 ++++++++++++++------------------------- src/common/Utilities/Util.cpp | 2 +- src/common/Utilities/Util.h | 19 ++++++++-- 3 files changed, 44 insertions(+), 53 deletions(-) (limited to 'src/common/Utilities') diff --git a/src/common/Utilities/Containers.h b/src/common/Utilities/Containers.h index b9a42a7ab3b..85e52887655 100644 --- a/src/common/Utilities/Containers.h +++ b/src/common/Utilities/Containers.h @@ -19,6 +19,7 @@ #define TRINITY_CONTAINERS_H #include "Define.h" +#include "MapUtils.h" #include "Random.h" #include #include @@ -29,18 +30,6 @@ namespace Trinity { - template - constexpr inline T* AddressOrSelf(T* ptr) - { - return ptr; - } - - template - constexpr inline T* AddressOrSelf(T& not_ptr) - { - return std::addressof(not_ptr); - } - template class CheckedBufferOutputIterator { @@ -252,55 +241,44 @@ namespace Trinity return false; } - /** - * Returns a pointer to mapped value (or the value itself if map stores pointers) - */ - template - inline auto MapGetValuePtr(M& map, typename M::key_type const& key) -> decltype(AddressOrSelf(map.find(key)->second)) - { - auto itr = map.find(key); - return itr != map.end() ? AddressOrSelf(itr->second) : nullptr; - } - - template class M, class... Rest> - inline void MultimapErasePair(M& multimap, K const& key, V const& value) + namespace Impl { - auto range = multimap.equal_range(key); - for (auto itr = range.first; itr != range.second;) + template + void EraseIfMoveAssignable(Container& c, Predicate p) { - if (itr->second == value) - itr = multimap.erase(itr); - else - ++itr; + auto wpos = c.begin(); + for (auto rpos = c.begin(), end = c.end(); rpos != end; ++rpos) + { + if (!p(*rpos)) + { + if (rpos != wpos) + std::swap(*rpos, *wpos); + ++wpos; + } + } + c.erase(wpos, c.end()); } - } - template - std::enable_if_t().begin())>, void> EraseIf(Container& c, Predicate p) - { - auto wpos = c.begin(); - for (auto rpos = c.begin(), end = c.end(); rpos != end; ++rpos) + template + void EraseIfNotMoveAssignable(Container& c, Predicate p) { - if (!p(*rpos)) + for (auto it = c.begin(); it != c.end();) { - if (rpos != wpos) - std::swap(*rpos, *wpos); - ++wpos; + if (p(*it)) + it = c.erase(it); + else + ++it; } } - c.erase(wpos, c.end()); } template - std::enable_if_t().begin())>, void> EraseIf(Container& c, Predicate p) + void EraseIf(Container& c, Predicate p) { - for (auto it = c.begin(); it != c.end();) - { - if (p(*it)) - it = c.erase(it); - else - ++it; - } + if constexpr (std::is_move_assignable_v) + Impl::EraseIfMoveAssignable(c, std::ref(p)); + else + Impl::EraseIfNotMoveAssignable(c, std::ref(p)); } /** diff --git a/src/common/Utilities/Util.cpp b/src/common/Utilities/Util.cpp index 0bdce5ee8f6..269784a0bfd 100644 --- a/src/common/Utilities/Util.cpp +++ b/src/common/Utilities/Util.cpp @@ -893,7 +893,7 @@ bool StringCompareLessI(std::string_view a, std::string_view b) return std::lexicographical_compare(a.begin(), a.end(), b.begin(), b.end(), [](char c1, char c2) { return std::tolower(c1) < std::tolower(c2); }); } -std::string GetTypeName(std::type_info const& info) +std::string Trinity::Impl::GetTypeName(std::type_info const& info) { return boost::core::demangle(info.name()); } diff --git a/src/common/Utilities/Util.h b/src/common/Utilities/Util.h index caf8dd88bb1..57523ca558f 100644 --- a/src/common/Utilities/Util.h +++ b/src/common/Utilities/Util.h @@ -485,11 +485,24 @@ Ret* Coalesce(T1* first, T*... rest) return static_cast(first); } -TC_COMMON_API std::string GetTypeName(std::type_info const&); +namespace Trinity +{ +namespace Impl +{ + TC_COMMON_API std::string GetTypeName(std::type_info const&); +} + template -std::string GetTypeName() { return GetTypeName(typeid(T)); } +std::string GetTypeName() { return Impl::GetTypeName(typeid(T)); } template -std::enable_if_t, std::type_info>, std::string> GetTypeName(T&& v) { return GetTypeName(typeid(v)); } +std::string GetTypeName(T&& v) +{ + if constexpr (std::is_same_v, std::type_info>) + return Impl::GetTypeName(v); + else + return Impl::GetTypeName(typeid(v)); +} +} template struct NonDefaultConstructible -- cgit v1.2.3