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