Debian Codegraph @

Source/Core/Common/VariantUtil.h

raw symbols SCIP
1 // Copyright 2018 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #pragma once
5
6 #include <variant>
7
8 namespace detail
9 {
10 template <typename... From>
11 struct VariantCastProxy
12 {
13 const std::variant<From...>& v;
14
15 template <typename... To>
16 operator std::variant<To...>() const
17 {
18 return std::visit([](auto&& arg) { return std::variant<To...>{arg}; }, v);
19 }
20 };
21 } // namespace detail
22
23 template <typename... From>
24 auto VariantCast(const std::variant<From...>& v)
25 {
26 return detail::VariantCastProxy<From...>{v};
27 }
28
29 template <class... Ts>
30 struct overloaded : Ts...
31 {
32 using Ts::operator()...;
33 };
34
35 template <class... Ts>
36 overloaded(Ts...) -> overloaded<Ts...>;
37
38 // Visits a functor with a variant_alternative of the given index.
39 // e.g. WithVariantAlternative<variant<int, float>>(1, func) calls func<float>()
40 // An out-of-bounds index causes no visitation.
41 template <typename Variant, std::size_t I = 0, typename Func>
42 void WithVariantAlternative(std::size_t index, Func&& func)
43 {
44 if constexpr (I < std::variant_size_v<Variant>)
45 {
46 if (index == 0)
47 func.template operator()<std::variant_alternative_t<I, Variant>>();
48 else
49 WithVariantAlternative<Variant, I + 1>(index - 1, std::forward<Func>(func));
50 }
51 }