The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
variant.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2021 - 2025
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
35namespace utils
36{
37#ifndef USING_BOOST_VARIANT
38
39using std::get;
40using std::get_if;
41using std::holds_alternative;
42using std::monostate;
43using std::variant;
44using std::visit;
45
46#else
47
48using boost::get;
49using boost::variant;
50
51using monostate = boost::blank;
52
53template<typename... Args>
54inline auto visit(Args&&... args)
55{
56 return boost::apply_visitor(std::forward<Args>(args)...);
57}
58
59template<typename Ret, typename... Types>
60inline bool holds_alternative(const boost::variant<Types...>& variant)
61{
62 return boost::get<Ret>(&variant) != nullptr;
63}
64
65template<typename Ret, typename... Types>
66inline Ret* get_if(boost::variant<Types...>* variant)
67{
68 return boost::get<Ret>(variant);
69}
70
71template<typename Ret, typename... Types>
72inline const Ret* get_if(const boost::variant<Types...>* variant)
73{
74 return boost::get<Ret>(variant);
75}
76
77#endif
78
79template<typename... Types>
80std::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
std::size_t variant_index(const variant< Types... > &var)
Definition variant.hpp:80