TGUI  0.9-dev
Optional.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2020 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 
36 namespace 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(std::nullptr_t) noexcept :
49  m_ptr(nullptr)
50  {
51  }
52 
53  Optional(const T& val) noexcept :
54  m_ptr(std::make_unique<T>(val))
55  {
56  }
57 
58  Optional(T&& val) noexcept :
59  m_ptr(std::make_unique<T>(std::move(val)))
60  {
61  }
62 
63  Optional(const Optional& other) :
64  m_ptr(other.m_ptr ? std::make_unique<T>(*other.m_ptr) : nullptr)
65  {
66  }
67 
68  Optional(Optional&& other) noexcept = default;
69 
70  template<typename... Args>
71  void emplace(Args&&... args)
72  {
73  m_ptr = std::make_unique<T>(args...);
74  }
75 
76  T& operator*()
77  {
78  return *m_ptr;
79  }
80 
81  T* operator->() noexcept
82  {
83  return m_ptr.get();
84  }
85 
86  Optional& operator=(std::nullptr_t) noexcept
87  {
88  m_ptr = nullptr;
89  return *this;
90  }
91 
92  Optional& operator=(const T& val) noexcept
93  {
94  m_ptr = std::make_unique<T>(val);
95  return *this;
96  }
97 
98  Optional& operator=(T&& val) noexcept
99  {
100  m_ptr = std::make_unique<T>(std::move(val));
101  return *this;
102  }
103 
104  Optional& operator=(const Optional& other) noexcept
105  {
106  m_ptr = other.m_ptr ? std::make_unique<T>(*other.m_ptr) : nullptr;
107  return *this;
108  }
109 
110  Optional& operator=(Optional&& val) noexcept = default;
111 
112  bool operator==(std::nullptr_t) const noexcept
113  {
114  return m_ptr == nullptr;
115  }
116 
117  bool operator!=(std::nullptr_t) const noexcept
118  {
119  return m_ptr != nullptr;
120  }
121 
122  explicit operator bool() const noexcept
123  {
124  return m_ptr != nullptr;
125  }
126 
127  const T& value() const
128  {
129  return *m_ptr;
130  }
131 
132  T& value()
133  {
134  return *m_ptr;
135  }
136 
137  private:
138  std::unique_ptr<T> m_ptr;
139  };
140 #endif
141 }
142 
143 #endif // TGUI_OPTIONAL_HPP
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:35