TrinityCore
Loading...
Searching...
No Matches
MapUtils.h
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef TRINITYCORE_MAP_UTILS_H
19#define TRINITYCORE_MAP_UTILS_H
20
21#include <memory> // std::to_address, std::addressof
22#include <type_traits>
23
24namespace Trinity::Containers
25{
26template <typename M>
27concept Map = requires (M)
28{
29 typename M::key_type;
30 typename M::mapped_type;
31};
32
36template <Map M>
37inline auto MapGetValuePtr(M& map, typename M::key_type const& key)
38{
39 using mapped_type = typename M::mapped_type;
40
41 auto itr = map.find(key);
42 if constexpr (std::is_pointer_v<mapped_type>)
43 return itr != map.end() ? itr->second : nullptr; // raw pointer
44 else if constexpr (requires(mapped_type const& p) { p.operator->(); })
45 {
46 // smart pointers
47 if constexpr (std::is_copy_constructible_v<mapped_type>)
48 return itr != map.end() ? itr->second : nullptr; // copyable (like shared_ptr)
49 else
50 return itr != map.end() ? std::to_address(itr->second) : nullptr; // non-copyable unique_ptr like, unwrap it to raw pointer
51 }
52 else
53 return itr != map.end() ? std::addressof(itr->second) : nullptr; // value
54}
55
56template <Map M>
57void MultimapErasePair(M& multimap, typename M::key_type const& key, typename M::mapped_type const& value)
58{
59 auto range = multimap.equal_range(key);
60 for (auto itr = range.first; itr != range.second;)
61 {
62 if (itr->second == value)
63 itr = multimap.erase(itr);
64 else
65 ++itr;
66 }
67}
68
72inline constexpr auto MapKey = []<typename Pair>(Pair&& pair) constexpr -> decltype(auto) { return (std::forward<Pair>(pair).first); /*Parentheses required for decltype(auto) to deduce a reference*/ };
73
77inline constexpr auto MapValue = []<typename Pair>(Pair&& pair) constexpr -> decltype(auto) { return (std::forward<Pair>(pair).second); /*Parentheses required for decltype(auto) to deduce a reference*/
78};
79}
80#endif // TRINITYCORE_MAP_UTILS_H
constexpr auto MapKey
Definition MapUtils.h:72
constexpr auto MapValue
Definition MapUtils.h:77
auto MapGetValuePtr(M &map, typename M::key_type const &key)
Definition MapUtils.h:37
void MultimapErasePair(M &multimap, typename M::key_type const &key, typename M::mapped_type const &value)
Definition MapUtils.h:57