farm-ng-core
variant_utils.h
Go to the documentation of this file.
1 // Copyright 2022, farm-ng inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
17 #include <variant>
18 namespace farm_ng {
19 
20 /// https://en.cppreference.com/w/cpp/utility/variant/visit
21 template <class... Ts>
22 struct Overload : Ts... {
23  using Ts::operator()...;
24 };
25 
26 template <class... Ts>
27 Overload(Ts...) -> Overload<Ts...>;
28 
29 /// https://en.cppreference.com/w/cpp/utility/variant/visit
30 template <class>
31 inline bool constexpr AlwaysFalse = false;
32 
33 /// https://gist.github.com/s3rvac/d1f30364ce1f732d75ef0c89a1c8c1ef
34 template <class Variant, typename... Alternatives>
35 decltype(auto) visit_variant(
36  Variant&& variant, Alternatives&&... alternatives) {
37  return std::visit(
38  Overload{std::forward<Alternatives>(alternatives)...},
39  std::forward<Variant>(variant));
40 }
41 
42 // https://stackoverflow.com/a/41171291
43 template <class T, class Variant>
44 struct HasType;
45 
46 template <class T, class... Us>
47 struct HasType<T, std::variant<Us...>>
48  : std::disjunction<std::is_same<T, Us>...> {};
49 
50 template <class T, class Variant>
52 
53 } // namespace farm_ng
farm_ng
Definition: backtrace.cpp:102
farm_ng::AlwaysFalse
constexpr bool AlwaysFalse
https://en.cppreference.com/w/cpp/utility/variant/visit
Definition: variant_utils.h:31
farm_ng::Overload
https://en.cppreference.com/w/cpp/utility/variant/visit
Definition: variant_utils.h:22
farm_ng::has_type_v
constexpr bool has_type_v
Definition: variant_utils.h:51
farm_ng::Overload
Overload(Ts...) -> Overload< Ts... >
farm_ng::HasType
Definition: variant_utils.h:44
farm_ng::visit_variant
decltype(auto) visit_variant(Variant &&variant, Alternatives &&... alternatives)
https://gist.github.com/s3rvac/d1f30364ce1f732d75ef0c89a1c8c1ef
Definition: variant_utils.h:35