diff options
Diffstat (limited to 'src/libutil/cxx/util.hxx')
-rw-r--r-- | src/libutil/cxx/util.hxx | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/src/libutil/cxx/util.hxx b/src/libutil/cxx/util.hxx index fa9aa1802..11c134d1f 100644 --- a/src/libutil/cxx/util.hxx +++ b/src/libutil/cxx/util.hxx @@ -21,6 +21,7 @@ #include <memory> #include <array> #include <string_view> +#include <optional> /* * Common C++ utilities @@ -76,6 +77,20 @@ constexpr auto array_of(T&&... t) -> std::array<V, sizeof...(T)> return {{ std::forward<T>(t)... }}; } +template<class C, class K, class V = typename C::mapped_type, typename std::enable_if_t< + std::is_constructible_v<typename C::key_type, K> + && std::is_constructible_v<typename C::mapped_type, V>, bool> = false> +constexpr auto find_map(const C &c, const K &k) -> std::optional<std::reference_wrapper<const V>> +{ + auto f = c.find(k); + + if (f != c.end()) { + return std::cref<V>(f->second); + } + + return std::nullopt; +} + } #endif //RSPAMD_UTIL_HXX |