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