Cbeam
Loading...
Searching...
No Matches
find.hpp
Go to the documentation of this file.
1/*
2Copyright (c) 2025 acrion innovations GmbH
3Authors: Stefan Zipproth, s.zipproth@acrion.ch
4
5This file is part of Cbeam, see https://github.com/acrion/cbeam and https://cbeam.org
6
7Cbeam is offered under a commercial and under the AGPL license.
8For commercial licensing, contact us at https://acrion.ch/sales. For AGPL licensing, see below.
9
10AGPL licensing:
11
12Cbeam is free software: you can redistribute it and/or modify
13it under the terms of the GNU Affero General Public License as published by
14the Free Software Foundation, either version 3 of the License, or
15(at your option) any later version.
16
17Cbeam is distributed in the hope that it will be useful,
18but WITHOUT ANY WARRANTY; without even the implied warranty of
19MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20GNU Affero General Public License for more details.
21
22You should have received a copy of the GNU Affero General Public License
23along with Cbeam. If not, see <https://www.gnu.org/licenses/>.
24*/
25
26#pragma once
27
28#include <cstddef> // for std::size_t
29
30#include <map> // for std::map
31#include <variant> // for std::variant, std::get_if, std::variant_alternative_t, std::visit
32
33namespace cbeam::container
34{
49 template <typename Key, typename Value, typename... VariantTypes>
50 bool key_exists(const std::map<std::variant<VariantTypes...>, Value>& t, const Key& key)
51 {
52 for (const auto& pair : t)
53 {
54 bool exists = std::visit([&key](auto&& lhs) -> bool
55 {
56 if constexpr (std::is_same_v<decltype(lhs), Key>)
57 {
58 return lhs == key;
59 }
60 return false; },
61 pair.first);
62 if (exists)
63 {
64 return true;
65 }
66 }
67 return false;
68 }
69
77 template <typename T, typename... Types>
78 T get_value_or_default(const std::variant<Types...>& value) noexcept
79 {
80 T* ptr = std::get_if<T>(const_cast<std::variant<Types...>*>(&value));
81 if (ptr)
82 {
83 return *ptr;
84 }
85 else
86 {
87 return {};
88 }
89 }
90
98 template <std::size_t T, typename... Types>
99 auto get_value_or_default(const std::variant<Types...>& value) noexcept
100 {
101 using variant_type = std::variant<Types...>;
102 using target_type = std::variant_alternative_t<T, variant_type>;
103
104 const target_type* ptr = std::get_if<T>(&value);
105 if (ptr)
106 {
107 return *ptr;
108 }
109 else
110 {
111 return target_type{};
112 }
113 }
114}
Offers advanced container types with unique approaches to stability and interprocess sharing....
Definition buffer.hpp:44
bool key_exists(const std::map< std::variant< VariantTypes... >, Value > &t, const Key &key)
Checks if a std::map using std::variant as a key contains a specific key.
Definition find.hpp:50
T get_value_or_default(const std::variant< Types... > &value) noexcept
Definition find.hpp:78