From 07d0f654d56e6b9aaffa6a2998dc9fb1d50cc747 Mon Sep 17 00:00:00 2001 From: Kitzunu <24550914+Kitzunu@users.noreply.github.com> Date: Sun, 30 May 2021 06:19:37 +0200 Subject: [PATCH] feat(Core/Containers): MapGetValuePtr (#6111) * feat(Core/Containers): MapGetValuePtr * Added MapGetValuePtr to allow writing `if (Val* v = MapGetValuePtr(map, key))` (cherry picked from commit https://github.com/TrinityCore/TrinityCore/commit/a1e3b54e076bf0361d23ace53703a4e501354d7c) Co-Authored-By: Shauren --- src/common/Utilities/Containers.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/common/Utilities/Containers.h b/src/common/Utilities/Containers.h index 486713f51..89eb0ccbe 100644 --- a/src/common/Utilities/Containers.h +++ b/src/common/Utilities/Containers.h @@ -165,6 +165,16 @@ namespace acore::Containers return SelectRandomWeightedContainerElement(container, weights); } + /** + * 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; + } + /* * @fn void acore::Containers::RandomShuffle(C& container) *