Cbeam
Loading...
Searching...
No Matches
find.hpp
Go to the documentation of this file.
1
/*
2
Copyright (c) 2025 acrion innovations GmbH
3
Authors: Stefan Zipproth, s.zipproth@acrion.ch
4
5
This file is part of Cbeam, see https://github.com/acrion/cbeam and https://cbeam.org
6
7
Cbeam is offered under a commercial and under the AGPL license.
8
For commercial licensing, contact us at https://acrion.ch/sales. For AGPL licensing, see below.
9
10
AGPL licensing:
11
12
Cbeam is free software: you can redistribute it and/or modify
13
it under the terms of the GNU Affero General Public License as published by
14
the Free Software Foundation, either version 3 of the License, or
15
(at your option) any later version.
16
17
Cbeam is distributed in the hope that it will be useful,
18
but WITHOUT ANY WARRANTY; without even the implied warranty of
19
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20
GNU Affero General Public License for more details.
21
22
You should have received a copy of the GNU Affero General Public License
23
along 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
33
namespace
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
}
cbeam::container
Offers advanced container types with unique approaches to stability and interprocess sharing....
Definition
buffer.hpp:44
cbeam::container::key_exists
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
cbeam::container::get_value_or_default
T get_value_or_default(const std::variant< Types... > &value) noexcept
Definition
find.hpp:78
include
cbeam
container
find.hpp
Generated by
1.13.2