TGUI  1.3.0
Loading...
Searching...
No Matches
Optional.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_OPTIONAL_HPP
26#define TGUI_OPTIONAL_HPP
27
28#include <TGUI/Config.hpp>
29
30#if !TGUI_EXPERIMENTAL_USE_STD_MODULE
31 #if TGUI_COMPILED_WITH_CPP_VER >= 17
32 #include <optional>
33 #else
34 #include <memory>
35 #endif
36#endif
37
38TGUI_MODULE_EXPORT namespace tgui
39{
40#if TGUI_COMPILED_WITH_CPP_VER >= 17
41 template<typename T>
42 using Optional = std::optional<T>;
43#else
44 template<typename T>
45 class Optional
46 {
47 public:
48 Optional() noexcept = default;
49
50 Optional(const T& val) noexcept :
51 m_ptr(std::make_unique<T>(val))
52 {
53 }
54
55 Optional(T&& val) noexcept :
56 m_ptr(std::make_unique<T>(std::move(val)))
57 {
58 }
59
60 Optional(const Optional& other) :
61 m_ptr(other.m_ptr ? std::make_unique<T>(*other.m_ptr) : nullptr)
62 {
63 }
64
65 Optional(Optional&& other) noexcept = default;
66
67 template<typename... Args>
68 void emplace(Args&&... args)
69 {
70 m_ptr = std::make_unique<T>(args...);
71 }
72
73 void reset() noexcept
74 {
75 m_ptr = nullptr;
76 }
77
78 T& operator*()
79 {
80 return *m_ptr;
81 }
82
83 const T& operator*() const
84 {
85 return *m_ptr;
86 }
87
88 T* operator->() noexcept
89 {
90 return m_ptr.get();
91 }
92
93 const T* operator->() const noexcept
94 {
95 return m_ptr.get();
96 }
97
98 Optional& operator=(std::nullptr_t) noexcept
99 {
100 m_ptr = nullptr;
101 return *this;
102 }
103
104 Optional& operator=(const T& val) noexcept
105 {
106 m_ptr = std::make_unique<T>(val);
107 return *this;
108 }
109
110 Optional& operator=(T&& val) noexcept
111 {
112 m_ptr = std::make_unique<T>(std::move(val));
113 return *this;
114 }
115
116 Optional& operator=(const Optional& other) noexcept
117 {
118 if (this == &other)
119 return *this;
120
121 m_ptr = other.m_ptr ? std::make_unique<T>(*other.m_ptr) : nullptr;
122 return *this;
123 }
124
125 Optional& operator=(Optional&& val) noexcept = default;
126
127 TGUI_NODISCARD bool operator==(std::nullptr_t) const noexcept
128 {
129 return m_ptr == nullptr;
130 }
131
132 TGUI_NODISCARD bool operator!=(std::nullptr_t) const noexcept
133 {
134 return m_ptr != nullptr;
135 }
136
137 explicit operator bool() const noexcept
138 {
139 return m_ptr != nullptr;
140 }
141
142 TGUI_NODISCARD bool has_value() const noexcept
143 {
144 return m_ptr != nullptr;
145 }
146
147 TGUI_NODISCARD const T& value() const
148 {
149 return *m_ptr;
150 }
151
152 TGUI_NODISCARD T& value()
153 {
154 return *m_ptr;
155 }
156
157 private:
158 std::unique_ptr<T> m_ptr;
159 };
160#endif
161}
162
163#endif // TGUI_OPTIONAL_HPP
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:39