TGUI  0.9.5
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 m_ptr = other.m_ptr ? std::make_unique<T>(*other.m_ptr) : nullptr;
117 return *this;
118 }
119
120 Optional& operator=(Optional&& val) noexcept = default;
121
122 bool operator==(std::nullptr_t) const noexcept
123 {
124 return m_ptr == nullptr;
125 }
126
127 bool operator!=(std::nullptr_t) const noexcept
128 {
129 return m_ptr != nullptr;
130 }
131
132 explicit operator bool() const noexcept
133 {
134 return m_ptr != nullptr;
135 }
136
137 const T& value() const
138 {
139 return *m_ptr;
140 }
141
142 T& value()
143 {
144 return *m_ptr;
145 }
146
147 private:
148 std::unique_ptr<T> m_ptr;
149 };
150#endif
151}
152
153#endif // TGUI_OPTIONAL_HPP
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:36