TGUI  1.2.0
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
26#ifndef TGUI_VARIANT_HPP
27#define TGUI_VARIANT_HPP
28
29#include <TGUI/Config.hpp>
30#if TGUI_COMPILED_WITH_CPP_VER >= 17
31 #if !TGUI_EXPERIMENTAL_USE_STD_MODULE
32 #include <variant>
33 #endif
34#else
35 #include <TGUI/Any.hpp>
36 #include <TGUI/Exception.hpp>
37#endif
38
40
41TGUI_MODULE_EXPORT namespace tgui
42{
43/*
44#if TGUI_COMPILED_WITH_CPP_VER < 17
45 namespace priv
46 {
47 template<typename... NoTypesLeft>
48 struct IndexInEmulatedVariantHelper
49 {
50 static std::size_t findIndex(const Any&, std::size_t)
51 {
52 // We should never pass here, it means that the Any object didn't hold anything.
53 throw Exception("tgui::Variant::index() called on uninitialized variant!");
54 }
55
56 static int getByIndex(const Any& any, std::size_t wantedIndex, std::size_t index)
57 {
58 throw Exception("tgui::Variant::get() called with too high index!");
59 }
60 };
61
62 template<typename FirstType, typename... OtherTypes>
63 struct IndexInEmulatedVariantHelper<FirstType, OtherTypes...>
64 {
65 static std::size_t findIndex(const Any& any, std::size_t index)
66 {
67 if (any.is<FirstType>())
68 return index;
69 else
70 return IndexInEmulatedVariantHelper<OtherTypes...>::findIndex(any, index + 1);
71 }
72
73 static decltype(auto) getByIndex(const Any& any, std::size_t wantedIndex, std::size_t index)
74 {
75 if (index == wantedIndex)
76 return any.as<FirstType>();
77 else
78 return IndexInEmulatedVariantHelper<OtherTypes...>::getByIndex(any, wantedIndex, index + 1);
79 }
80 };
81 }
82#endif
83*/
84
89#if TGUI_COMPILED_WITH_CPP_VER >= 17
90 template <typename... Types>
91#else
92 template <typename FirstType, typename... OtherTypes>
93#endif
94 class Variant
95 {
96 public:
97
102#if TGUI_COMPILED_WITH_CPP_VER >= 17
103 m_variant{}
104#else
105 m_any{FirstType{}}
106#endif
107 {
108 }
109
110
116 template <typename T>
117 Variant(T&& value) : // NOLINT(bugprone-forwarding-reference-overload)
118#if TGUI_COMPILED_WITH_CPP_VER >= 17
119 m_variant{std::forward<T>(value)}
120#else
121 m_any{std::forward<T>(value)}
122#endif
123 {
124 }
125
126
132 template <typename T>
133 TGUI_NODISCARD T& get()
134 {
135#if TGUI_COMPILED_WITH_CPP_VER >= 17
136 return std::get<T>(m_variant);
137#else
138 return m_any.as<T>();
139#endif
140 }
141
142
148 template <typename T>
149 TGUI_NODISCARD const T& get() const
150 {
151#if TGUI_COMPILED_WITH_CPP_VER >= 17
152 return std::get<T>(m_variant);
153#else
154 return m_any.as<T>();
155#endif
156 }
157
158/*
164 template <std::size_t Index>
165 auto& get()
166 {
167#if TGUI_COMPILED_WITH_CPP_VER >= 17
168 return std::get<Index>(m_variant);
169#else
170 return priv::IndexInEmulatedVariantHelper<FirstType, OtherTypes...>::getByIndex(m_any, Index, 0);
171#endif
172 }
173
174
180 template <std::size_t Index>
181 const auto& get() const
182 {
183#if TGUI_COMPILED_WITH_CPP_VER >= 17
184 return std::get<Index>(m_variant);
185#else
186 return priv::IndexInEmulatedVariantHelper<FirstType, OtherTypes...>::getByIndex(m_any, Index, 0);
187#endif
188 }
189
190
194 std::size_t index() const
195 {
196#if TGUI_COMPILED_WITH_CPP_VER >= 17
197 return m_variant.index();
198#else
199 return priv::IndexInEmulatedVariantHelper<FirstType, OtherTypes...>::findIndex(m_any, 0);
200#endif
201 }
202*/
203
205 private:
206
207#if TGUI_COMPILED_WITH_CPP_VER >= 17
208 std::variant<Types...> m_variant;
209#else
210 Any m_any;
211#endif
212 };
213
215}
216
218
219#endif // TGUI_VARIANT_HPP
220
Definition Variant.hpp:95
TGUI_NODISCARD const T & get() const
Retrieve the value in the variant.
Definition Variant.hpp:149
Variant()
Default constructor.
Definition Variant.hpp:101
TGUI_NODISCARD T & get()
Retrieve the value in the variant.
Definition Variant.hpp:133
Variant(T &&value)
Construct the variant with an initial value.
Definition Variant.hpp:117
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:39