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