TGUI  0.10-beta
Variant.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2022 Bruno Van de Velde (vdv_b@tgui.eu)
5//
6// This software is provided 'as-is', without any express or implied warranty.
7// In no event will the authors be held liable for any damages arising from the use of this software.
8//
9// Permission is granted to anyone to use this software for any purpose,
10// including commercial applications, and to alter it and redistribute it freely,
11// subject to the following restrictions:
12//
13// 1. The origin of this software must not be misrepresented;
14// you must not claim that you wrote the original software.
15// If you use this software in a product, an acknowledgment
16// in the product documentation would be appreciated but is not required.
17//
18// 2. Altered source versions must be plainly marked as such,
19// and must not be misrepresented as being the original software.
20//
21// 3. This notice may not be removed or altered from any source distribution.
22//
24
25
26#ifndef TGUI_VARIANT_HPP
27#define TGUI_VARIANT_HPP
28
29#if TGUI_COMPILED_WITH_CPP_VER >= 17
30 #include <variant>
31#else
32 #include <TGUI/Any.hpp>
33 #include <TGUI/Exception.hpp>
34#endif
35
37
38namespace tgui
39{
40/*
41#if TGUI_COMPILED_WITH_CPP_VER < 17
42 namespace priv
43 {
44 template<typename... NoTypesLeft>
45 struct IndexInEmulatedVariantHelper
46 {
47 static std::size_t findIndex(const Any&, std::size_t)
48 {
49 // We should never pass here, it means that the Any object didn't hold anything.
50 throw tgui::Exception("tgui::Variant::index() called on uninitialized variant!");
51 }
52
53 static int getByIndex(const Any& any, std::size_t wantedIndex, std::size_t index)
54 {
55 throw tgui::Exception("tgui::Variant::get() called with too high index!");
56 }
57 };
58
59 template<typename FirstType, typename... OtherTypes>
60 struct IndexInEmulatedVariantHelper<FirstType, OtherTypes...>
61 {
62 static std::size_t findIndex(const Any& any, std::size_t index)
63 {
64 if (any.is<FirstType>())
65 return index;
66 else
67 return IndexInEmulatedVariantHelper<OtherTypes...>::findIndex(any, index + 1);
68 }
69
70 static decltype(auto) getByIndex(const Any& any, std::size_t wantedIndex, std::size_t index)
71 {
72 if (index == wantedIndex)
73 return any.as<FirstType>();
74 else
75 return IndexInEmulatedVariantHelper<OtherTypes...>::getByIndex(any, wantedIndex, index + 1);
76 }
77 };
78 }
79#endif
80*/
81
86#if TGUI_COMPILED_WITH_CPP_VER >= 17
87 template <typename... Types>
88#else
89 template <typename FirstType, typename... OtherTypes>
90#endif
91 class Variant
92 {
93 public:
94
99#if TGUI_COMPILED_WITH_CPP_VER >= 17
100 m_variant{}
101#else
102 m_any{FirstType{}}
103#endif
104 {
105 }
106
107
113 template <typename T>
114 Variant(const T& value) :
115#if TGUI_COMPILED_WITH_CPP_VER >= 17
116 m_variant{value}
117#else
118 m_any{value}
119#endif
120 {
121 }
122
123
129 template <typename T>
130 T& get()
131 {
132#if TGUI_COMPILED_WITH_CPP_VER >= 17
133 return std::get<T>(m_variant);
134#else
135 return m_any.as<T>();
136#endif
137 }
138
139
145 template <typename T>
146 const T& get() const
147 {
148#if TGUI_COMPILED_WITH_CPP_VER >= 17
149 return std::get<T>(m_variant);
150#else
151 return m_any.as<T>();
152#endif
153 }
154
155/*
161 template <std::size_t Index>
162 auto& get()
163 {
164#if TGUI_COMPILED_WITH_CPP_VER >= 17
165 return std::get<Index>(m_variant);
166#else
167 return priv::IndexInEmulatedVariantHelper<FirstType, OtherTypes...>::getByIndex(m_any, Index, 0);
168#endif
169 }
170
171
177 template <std::size_t Index>
178 const auto& get() const
179 {
180#if TGUI_COMPILED_WITH_CPP_VER >= 17
181 return std::get<Index>(m_variant);
182#else
183 return priv::IndexInEmulatedVariantHelper<FirstType, OtherTypes...>::getByIndex(m_any, Index, 0);
184#endif
185 }
186
187
191 std::size_t index() const
192 {
193#if TGUI_COMPILED_WITH_CPP_VER >= 17
194 return m_variant.index();
195#else
196 return priv::IndexInEmulatedVariantHelper<FirstType, OtherTypes...>::findIndex(m_any, 0);
197#endif
198 }
199*/
200
202 private:
203
204#if TGUI_COMPILED_WITH_CPP_VER >= 17
205 std::variant<Types...> m_variant;
206#else
207 Any m_any;
208#endif
209 };
210
212}
213
215
216#endif // TGUI_VARIANT_HPP
217
Definition: Variant.hpp:92
Variant()
Default constructor.
Definition: Variant.hpp:98
const T & get() const
Retrieve the value in the variant.
Definition: Variant.hpp:146
T & get()
Retrieve the value in the variant.
Definition: Variant.hpp:130
Variant(const T &value)
Construct the variant with an initial value.
Definition: Variant.hpp:114
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36