The Battle for Wesnoth  1.19.0-dev
variant.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2021 - 2024
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #pragma once
16 
17 /**
18  * @file
19  *
20  * MacOS doesn't support std::visit when targing MacOS < 10.14 (currently we target 10.11).
21  * This provides a wrapper around the STL variant API on all platforms except MacOS, which
22  * instead utilizes boost::variant.
23  */
24 
25 #ifdef __APPLE__
26 #define USING_BOOST_VARIANT
27 #endif
28 
29 #ifndef USING_BOOST_VARIANT
30 #include <variant>
31 #else
32 #include <boost/variant.hpp>
33 #endif
34 
35 namespace utils
36 {
37 #ifndef USING_BOOST_VARIANT
38 
39 using std::get;
40 using std::get_if;
41 using std::holds_alternative;
42 using std::monostate;
43 using std::variant;
44 using std::visit;
45 
46 #else
47 
48 using boost::get;
49 using boost::variant;
50 
51 using monostate = boost::blank;
52 
53 template<typename... Args>
54 inline auto visit(Args&&... args)
55 {
56  return boost::apply_visitor(std::forward<Args>(args)...);
57 }
58 
59 template<typename Ret, typename... Types>
60 inline bool holds_alternative(const boost::variant<Types...>& variant)
61 {
62  return boost::get<Ret>(&variant) != nullptr;
63 }
64 
65 template<typename Ret, typename... Types>
66 inline Ret* get_if(boost::variant<Types...>* variant)
67 {
68  return boost::get<Ret>(variant);
69 }
70 
71 template<typename Ret, typename... Types>
72 inline const Ret* get_if(const boost::variant<Types...>* variant)
73 {
74  return boost::get<Ret>(variant);
75 }
76 
77 #endif
78 
79 template<typename... Types>
80 std::size_t variant_index(const variant<Types...>& var)
81 {
82 #ifndef USING_BOOST_VARIANT
83  return var.index();
84 #else
85  return var.which();
86 #endif
87 }
88 
89 } // namespace utils
CURSOR_TYPE get()
Definition: cursor.cpp:216
std::size_t variant_index(const variant< Types... > &var)
Definition: variant.hpp:80
V::result_t apply_visitor(typename V::param_t state, T &&... args)
Helper function to apply the result of a specified visitor to a variable_info object.