TGUI 1.11
Loading...
Searching...
No Matches
Variant.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2025 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 #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 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 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
112 template <typename T>
113 Variant(T&& value) : // NOLINT(bugprone-forwarding-reference-overload)
114#if TGUI_COMPILED_WITH_CPP_VER >= 17
115 m_variant{std::forward<T>(value)}
116#else
117 m_any{std::forward<T>(value)}
118#endif
119 {
120 }
121
127 template <typename T>
128 TGUI_NODISCARD T& get()
129 {
130#if TGUI_COMPILED_WITH_CPP_VER >= 17
131 return std::get<T>(m_variant);
132#else
133 return m_any.as<T>();
134#endif
135 }
136
142 template <typename T>
143 TGUI_NODISCARD const T& get() const
144 {
145#if TGUI_COMPILED_WITH_CPP_VER >= 17
146 return std::get<T>(m_variant);
147#else
148 return m_any.as<T>();
149#endif
150 }
151
152/*
158 template <std::size_t Index>
159 auto& get()
160 {
161#if TGUI_COMPILED_WITH_CPP_VER >= 17
162 return std::get<Index>(m_variant);
163#else
164 return priv::IndexInEmulatedVariantHelper<FirstType, OtherTypes...>::getByIndex(m_any, Index, 0);
165#endif
166 }
167
173 template <std::size_t Index>
174 const auto& get() const
175 {
176#if TGUI_COMPILED_WITH_CPP_VER >= 17
177 return std::get<Index>(m_variant);
178#else
179 return priv::IndexInEmulatedVariantHelper<FirstType, OtherTypes...>::getByIndex(m_any, Index, 0);
180#endif
181 }
182
186 std::size_t index() const
187 {
188#if TGUI_COMPILED_WITH_CPP_VER >= 17
189 return m_variant.index();
190#else
191 return priv::IndexInEmulatedVariantHelper<FirstType, OtherTypes...>::findIndex(m_any, 0);
192#endif
193 }
194*/
195
197 private:
198
199#if TGUI_COMPILED_WITH_CPP_VER >= 17
200 std::variant<Types...> m_variant;
201#else
202 Any m_any;
203#endif
204 };
205
207}
208
210
211#endif // TGUI_VARIANT_HPP
212
Variant()
Default constructor.
Definition Variant.hpp:98
const T & get() const
Retrieve the value in the variant.
Definition Variant.hpp:143
Variant(T &&value)
Construct the variant with an initial value.
Definition Variant.hpp:113
T & get()
Retrieve the value in the variant.
Definition Variant.hpp:128
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:36