TGUI  1.6.1
Loading...
Searching...
No Matches
Variant.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2024 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#ifndef TGUI_VARIANT_HPP
26#define TGUI_VARIANT_HPP
27
28#include <TGUI/Config.hpp>
29#if TGUI_COMPILED_WITH_CPP_VER >= 17
30 #if !TGUI_EXPERIMENTAL_USE_STD_MODULE
31 #include <variant>
32 #endif
33#else
34 #include <TGUI/Any.hpp>
35 #include <TGUI/Exception.hpp>
36#endif
37
39
40TGUI_MODULE_EXPORT namespace tgui
41{
42/*
43#if TGUI_COMPILED_WITH_CPP_VER < 17
44 namespace priv
45 {
46 template<typename... NoTypesLeft>
47 struct IndexInEmulatedVariantHelper
48 {
49 static std::size_t findIndex(const Any&, std::size_t)
50 {
51 // We should never pass here, it means that the Any object didn't hold anything.
52 throw Exception("tgui::Variant::index() called on uninitialized variant!");
53 }
54
55 static int getByIndex(const Any& any, std::size_t wantedIndex, std::size_t index)
56 {
57 throw Exception("tgui::Variant::get() called with too high index!");
58 }
59 };
60
61 template<typename FirstType, typename... OtherTypes>
62 struct IndexInEmulatedVariantHelper<FirstType, OtherTypes...>
63 {
64 static std::size_t findIndex(const Any& any, std::size_t index)
65 {
66 if (any.is<FirstType>())
67 return index;
68 else
69 return IndexInEmulatedVariantHelper<OtherTypes...>::findIndex(any, index + 1);
70 }
71
72 static decltype(auto) getByIndex(const Any& any, std::size_t wantedIndex, std::size_t index)
73 {
74 if (index == wantedIndex)
75 return any.as<FirstType>();
76 else
77 return IndexInEmulatedVariantHelper<OtherTypes...>::getByIndex(any, wantedIndex, index + 1);
78 }
79 };
80 }
81#endif
82*/
83
88#if TGUI_COMPILED_WITH_CPP_VER >= 17
89 template <typename... Types>
90#else
91 template <typename FirstType, typename... OtherTypes>
92#endif
93 class Variant
94 {
95 public:
96
101#if TGUI_COMPILED_WITH_CPP_VER >= 17
102 m_variant{}
103#else
104 m_any{FirstType{}}
105#endif
106 {
107 }
108
114 template <typename T>
115 Variant(T&& value) : // NOLINT(bugprone-forwarding-reference-overload)
116#if TGUI_COMPILED_WITH_CPP_VER >= 17
117 m_variant{std::forward<T>(value)}
118#else
119 m_any{std::forward<T>(value)}
120#endif
121 {
122 }
123
129 template <typename T>
130 TGUI_NODISCARD 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
144 template <typename T>
145 TGUI_NODISCARD const T& get() const
146 {
147#if TGUI_COMPILED_WITH_CPP_VER >= 17
148 return std::get<T>(m_variant);
149#else
150 return m_any.as<T>();
151#endif
152 }
153
154/*
160 template <std::size_t Index>
161 auto& get()
162 {
163#if TGUI_COMPILED_WITH_CPP_VER >= 17
164 return std::get<Index>(m_variant);
165#else
166 return priv::IndexInEmulatedVariantHelper<FirstType, OtherTypes...>::getByIndex(m_any, Index, 0);
167#endif
168 }
169
175 template <std::size_t Index>
176 const auto& get() const
177 {
178#if TGUI_COMPILED_WITH_CPP_VER >= 17
179 return std::get<Index>(m_variant);
180#else
181 return priv::IndexInEmulatedVariantHelper<FirstType, OtherTypes...>::getByIndex(m_any, Index, 0);
182#endif
183 }
184
188 std::size_t index() const
189 {
190#if TGUI_COMPILED_WITH_CPP_VER >= 17
191 return m_variant.index();
192#else
193 return priv::IndexInEmulatedVariantHelper<FirstType, OtherTypes...>::findIndex(m_any, 0);
194#endif
195 }
196*/
197
199 private:
200
201#if TGUI_COMPILED_WITH_CPP_VER >= 17
202 std::variant<Types...> m_variant;
203#else
204 Any m_any;
205#endif
206 };
207
209}
210
212
213#endif // TGUI_VARIANT_HPP
214
Definition Variant.hpp:94
Variant()
Default constructor.
Definition Variant.hpp:100
const T & get() const
Retrieve the value in the variant.
Definition Variant.hpp:145
Variant(T &&value)
Construct the variant with an initial value.
Definition Variant.hpp:115
T & get()
Retrieve the value in the variant.
Definition Variant.hpp:130
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:38