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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include "opentelemetry/version.h"
#include <cstddef>
#include <memory>
#include <utility>
#include <variant>
OPENTELEMETRY_BEGIN_NAMESPACE
// Standard Type aliases in nostd namespace
namespace nostd
{
using std::get_if;
using std::monostate;
using std::variant_alternative_t;
// nostd::variant<...>
template <class... _Types>
using variant = std::variant<_Types...>;
template <class... _Types>
using variant_size = std::variant_size<_Types...>;
using monostate = std::monostate;
#if defined(__APPLE__) && defined(_LIBCPP_USE_AVAILABILITY_APPLE)
// Apple Platforms provide std::bad_variant_access only in newer versions of OS.
// To keep API compatible with any version of OS - we are providing our own
// implementation of nostd::bad_variant_access exception.
# if __EXCEPTIONS
// nostd::bad_variant_access
class bad_variant_access : public std::exception
{
public:
virtual const char *what() const noexcept override { return "bad_variant_access"; }
};
[[noreturn]] inline void throw_bad_variant_access()
{
throw bad_variant_access{};
}
# endif
# if __EXCEPTIONS
# define THROW_BAD_VARIANT_ACCESS throw_bad_variant_access()
# else
# define THROW_BAD_VARIANT_ACCESS std::terminate()
# endif
//
// nostd::get<...> for Apple Clang
//
template <typename T, class... Types>
constexpr auto get_type = [](auto &&t) constexpr -> decltype(auto)
{
auto v = t;
auto result = std::get_if<T>(&v); // TODO: optimize with std::forward(t) if t is not rvalue
if (result)
{
return *result;
}
THROW_BAD_VARIANT_ACCESS;
return *result;
};
template <std::size_t I, class... Types>
constexpr auto get_index = [](auto &&t) constexpr -> decltype(auto)
{
auto v = t;
auto result = std::get_if<I>(&v); // TODO: optimize with std::forward(t) if t is not rvalue
if (result)
{
return *result;
}
THROW_BAD_VARIANT_ACCESS;
return *result;
};
template <std::size_t I, class... Types>
constexpr std::variant_alternative_t<I, std::variant<Types...>> &get(std::variant<Types...> &v)
{
return get_index<I, Types...>(v);
};
template <std::size_t I, class... Types>
constexpr std::variant_alternative_t<I, std::variant<Types...>> &&get(std::variant<Types...> &&v)
{
return get_index<I, Types...>(std::forward<decltype(v)>(v));
};
template <std::size_t I, class... Types>
constexpr const std::variant_alternative_t<I, std::variant<Types...>> &get(
const std::variant<Types...> &v)
{
return get_index<I, Types...>(v);
};
template <std::size_t I, class... Types>
constexpr const std::variant_alternative_t<I, std::variant<Types...>> &&get(
const std::variant<Types...> &&v)
{
return get_index<I, Types...>(std::forward<decltype(v)>(v));
};
template <class T, class... Types>
constexpr T &get(std::variant<Types...> &v)
{
return get_type<T, Types...>(v);
};
template <class T, class... Types>
constexpr T /*&&*/ get(std::variant<Types...> &&v)
{
return get_type<T, Types...>(v);
};
template <class T, class... Types>
constexpr const T &get(const std::variant<Types...> &v)
{
return get_type<T, Types...>(v);
};
template <class T, class... Types>
constexpr const T &&get(const std::variant<Types...> &&v)
{
return get_type<T, Types...>(std::forward<decltype(v)>(v));
};
template <class _Callable, class... _Variants>
constexpr auto visit(_Callable &&_Obj, _Variants &&... _Args)
{
// Ref:
// https://stackoverflow.com/questions/52310835/xcode-10-call-to-unavailable-function-stdvisit
return std::__variant_detail::__visitation::__variant::__visit_value(_Obj, _Args...);
};
#else
using std::bad_variant_access;
template <std::size_t I, class... Types>
constexpr std::variant_alternative_t<I, std::variant<Types...>> &get(std::variant<Types...> &v)
{
return std::get<I, Types...>(v);
};
template <std::size_t I, class... Types>
constexpr std::variant_alternative_t<I, std::variant<Types...>> &&get(std::variant<Types...> &&v)
{
return std::get<I, Types...>(std::forward<decltype(v)>(v));
};
template <std::size_t I, class... Types>
constexpr const std::variant_alternative_t<I, std::variant<Types...>> &get(
const std::variant<Types...> &v)
{
return std::get<I, Types...>(v);
};
template <std::size_t I, class... Types>
constexpr const std::variant_alternative_t<I, std::variant<Types...>> &&get(
const std::variant<Types...> &&v)
{
return std::get<I, Types...>(std::forward<decltype(v)>(v));
};
template <class T, class... Types>
constexpr T &get(std::variant<Types...> &v)
{
return std::get<T, Types...>(v);
};
template <class T, class... Types>
constexpr T &&get(std::variant<Types...> &&v)
{
return std::get<T, Types...>(std::forward<decltype(v)>(v));
};
template <class T, class... Types>
constexpr const T &get(const std::variant<Types...> &v)
{
return std::get<T, Types...>(v);
};
template <class T, class... Types>
constexpr const T &&get(const std::variant<Types...> &&v)
{
return std::get<T, Types...>(std::forward<decltype(v)>(v));
};
template <class _Callable, class... _Variants>
constexpr auto visit(_Callable &&_Obj, _Variants &&... _Args)
{
return std::visit<_Callable, _Variants...>(static_cast<_Callable &&>(_Obj),
static_cast<_Variants &&>(_Args)...);
};
#endif
/*
# if _HAS_CXX20
template <class _Ret, class _Callable, class... _Variants>
constexpr _Ret visit(_Callable &&_Obj, _Variants &&... _Args)
{
return std::visit<_Ret, _Callable, _Variants...>(
static_cast<_Callable &&>(_Obj),
static_cast<_Variants &&>(_Args)...);
};
# endif
*/
// nostd::holds_alternative
template <std::size_t I, typename... Ts>
inline constexpr bool holds_alternative(const variant<Ts...> &v) noexcept
{
return v.index() == I;
}
template <typename T, typename... Ts>
inline constexpr bool holds_alternative(const variant<Ts...> &v) noexcept
{
return std::holds_alternative<T, Ts...>(v);
}
} // namespace nostd
OPENTELEMETRY_END_NAMESPACE
|