aboutsummaryrefslogtreecommitdiff
path: root/src/common/Utilities
diff options
context:
space:
mode:
authorShauren <shauren.trinity@gmail.com>2023-01-07 22:38:21 +0100
committerShauren <shauren.trinity@gmail.com>2023-01-07 22:38:21 +0100
commita53e4a57565d3375a978effbbc32d3eed6aac7e3 (patch)
tree070e7bd084fde9b62769c7ad1e4dbb52e82a10d9 /src/common/Utilities
parent7830e5a7a1b93b0cd083baa3b70a0cfeb475f5f5 (diff)
Core/Misc: Replace enable_if overload selection with if constexpr
Diffstat (limited to 'src/common/Utilities')
-rw-r--r--src/common/Utilities/Containers.h76
-rw-r--r--src/common/Utilities/Util.cpp2
-rw-r--r--src/common/Utilities/Util.h19
3 files changed, 44 insertions, 53 deletions
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 <algorithm>
#include <iterator>
@@ -29,18 +30,6 @@
namespace Trinity
{
- template<class T>
- constexpr inline T* AddressOrSelf(T* ptr)
- {
- return ptr;
- }
-
- template<class T>
- constexpr inline T* AddressOrSelf(T& not_ptr)
- {
- return std::addressof(not_ptr);
- }
-
template <class T>
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<class M>
- 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 K, class V, template<class, class, class...> class M, class... Rest>
- inline void MultimapErasePair(M<K, V, Rest...>& multimap, K const& key, V const& value)
+ namespace Impl
{
- auto range = multimap.equal_range(key);
- for (auto itr = range.first; itr != range.second;)
+ template <typename Container, typename Predicate>
+ 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 <typename Container, typename Predicate>
- std::enable_if_t<std::is_move_assignable_v<decltype(*std::declval<Container>().begin())>, void> EraseIf(Container& c, Predicate p)
- {
- auto wpos = c.begin();
- for (auto rpos = c.begin(), end = c.end(); rpos != end; ++rpos)
+ template <typename Container, typename Predicate>
+ 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 <typename Container, typename Predicate>
- std::enable_if_t<!std::is_move_assignable_v<decltype(*std::declval<Container>().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<decltype(*c.begin())>)
+ 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<Ret*>(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 <typename T>
-std::string GetTypeName() { return GetTypeName(typeid(T)); }
+std::string GetTypeName() { return Impl::GetTypeName(typeid(T)); }
template <typename T>
-std::enable_if_t<!std::is_same_v<std::decay_t<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::remove_cv_t<T>, std::type_info>)
+ return Impl::GetTypeName(v);
+ else
+ return Impl::GetTypeName(typeid(v));
+}
+}
template<typename T>
struct NonDefaultConstructible