aboutsummaryrefslogtreecommitdiffstats
path: root/util/generic/mapfindptr.h
blob: bb6dc617552f3b8cc1520542bc40e4e380caadb0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#pragma once

#include <util/system/compiler.h>

#include <type_traits>
#include <utility>

/** MapFindPtr usage:

if (T* value = MapFindPtr(myMap, someKey) {
    Cout << *value;
}

*/

template <class Map, class K>
inline auto MapFindPtr(Map& map Y_LIFETIME_BOUND, const K& key) {
    auto i = map.find(key);

    return (i == map.end() ? nullptr : &i->second);
}

template <class Map, class K>
inline auto MapFindPtr(const Map& map Y_LIFETIME_BOUND, const K& key) {
    auto i = map.find(key);

    return (i == map.end() ? nullptr : &i->second);
}

/** helper for THashMap/TMap */
template <class Derived>
struct TMapOps {
    template <class K>
    inline auto FindPtr(const K& key) Y_LIFETIME_BOUND {
        return MapFindPtr(static_cast<Derived&>(*this), key);
    }

    template <class K>
    inline auto FindPtr(const K& key) const Y_LIFETIME_BOUND {
        return MapFindPtr(static_cast<const Derived&>(*this), key);
    }

    template <class K, class DefaultValue>
    inline auto Value(const K& key, DefaultValue&& defaultValue) const {
        auto found = FindPtr(key);
        return found ? *found : std::forward<DefaultValue>(defaultValue);
    }

    template <class K, class V>
    inline const V& ValueRef(const K& key, V& defaultValue Y_LIFETIME_BOUND) const Y_LIFETIME_BOUND {
        static_assert(std::is_same<std::remove_const_t<V>, typename Derived::mapped_type>::value, "Passed default value must have the same type as the underlying map's mapped_type.");

        if (auto found = FindPtr(key)) {
            return *found;
        }
        return defaultValue;
    }

    template <class K, class V>
    inline const V& ValueRef(const K& key, V&& defaultValue Y_LIFETIME_BOUND) const Y_LIFETIME_BOUND = delete;
};