You.i Engine
YiOptional.h
Go to the documentation of this file.
1 // © You i Labs Inc. 2000-2020. All rights reserved.
2 #ifndef _YI_OPTIONAL_H_
3 #define _YI_OPTIONAL_H_
4 
5 #include "utility/YiError.h"
6 
7 #include <utility>
8 
9 #if YI_FULLY_SUPPORTS_CPP14_CONSTEXPR
10 # define YI_CONSTEXPR constexpr
11 #else
12 # define YI_CONSTEXPR
13 #endif
14 
26 {
27 };
28 
37 
57 template<typename T>
59 {
60  template<typename U>
61  friend class CYIOptional;
62 
63  static_assert(!std::is_const<T>::value, "CYIOptional may not be used with const types.");
64  static_assert(!std::is_volatile<T>::value, "CYIOptional may not be used with volatile types.");
65  static_assert(!std::is_reference<T>::value, "CYIOptional may not be used with reference types.");
66 
67 public:
71  constexpr CYIOptional();
73 
77  YI_CONSTEXPR CYIOptional(const CYIOptional &other);
78  template<typename U>
80 
84  YI_CONSTEXPR CYIOptional(CYIOptional &&other) noexcept(std::is_nothrow_move_constructible<T>::value);
85  template<typename U>
87 
91  constexpr CYIOptional(const T &value);
92  template<typename U, typename = typename std::enable_if<std::is_convertible<U, T>::value, char>::type>
93  constexpr CYIOptional(const U &value);
94 
98  constexpr CYIOptional(T &&value);
99  template<typename U, typename = typename std::enable_if<std::is_convertible<U, T>::value, char>::type>
100  constexpr CYIOptional(U &&value);
101 
102  ~CYIOptional();
103 
108 
112  template<typename U, typename = typename std::enable_if<std::is_convertible<U, T>::value, char>::type>
113  YI_CONSTEXPR CYIOptional &operator=(const U &value);
114 
121 
125  template<typename U, typename = typename std::enable_if<std::is_convertible<U, T>::value, char>::type>
126  YI_CONSTEXPR CYIOptional &operator=(U &&value);
127 
133  template<typename... Args>
134  T &Emplace(Args &&... args);
135 
141  constexpr bool HasValue() const;
142  constexpr explicit operator bool() const;
143 
144  T *operator->();
145  const T *operator->() const;
146 
160  T &GetValue() &;
161  const T &GetValue() const &;
162  T &&GetValue() &&;
163  T &operator*() &;
164  const T &operator*() const &;
165  T &&operator*() &&;
166 
170  template<typename U>
171  constexpr T GetValueOr(U &&alternative) const &;
172  template<typename U>
173  constexpr T GetValueOr(U &&alternative) &&;
174 
178  YI_CONSTEXPR void Reset();
179 
183  YI_CONSTEXPR void Swap(CYIOptional &other);
184 
185 private:
186  union {
189  };
190  bool m_hasValue;
191 #ifdef YI_DEBUG
192  mutable bool m_valueValidated;
193 #endif
194 };
195 
199 template<typename T, typename... Args>
200 constexpr CYIOptional<T> YiMakeOptional(Args &&... args);
201 
202 template<typename T1, typename T2>
203 constexpr bool operator==(const CYIOptional<T1> &first, const CYIOptional<T2> &second);
204 
205 template<typename T, typename U>
206 constexpr bool operator==(const CYIOptional<T> &first, const U &second);
207 
208 template<typename T, typename U>
209 constexpr bool operator==(const U &first, const CYIOptional<T> &second);
210 
211 template<typename T>
212 constexpr bool operator==(const CYIOptional<T> &first, const CYIEmptyOptionalType &);
213 
214 template<typename T>
215 constexpr bool operator==(const CYIEmptyOptionalType &, const CYIOptional<T> &second);
216 
217 template<typename T1, typename T2>
218 constexpr bool operator!=(const CYIOptional<T1> &first, const CYIOptional<T2> &second);
219 
220 template<typename T, typename U>
221 constexpr bool operator!=(const CYIOptional<T> &first, const U &second);
222 
223 template<typename T, typename U>
224 constexpr bool operator!=(const U &first, const CYIOptional<T> &second);
225 
226 template<typename T1, typename T2>
227 constexpr bool operator<(const CYIOptional<T1> &first, const CYIOptional<T2> &second);
228 
229 template<typename T, typename U>
230 constexpr bool operator<(const CYIOptional<T> &first, const U &second);
231 
232 template<typename T, typename U>
233 constexpr bool operator<(const U &first, const CYIOptional<T> &second);
234 
235 template<typename T>
236 constexpr bool operator<(const CYIOptional<T> &first, const CYIEmptyOptionalType &);
237 
238 template<typename T>
239 constexpr bool operator<(const CYIEmptyOptionalType &, const CYIOptional<T> &second);
240 
241 template<typename T1, typename T2>
242 constexpr bool operator>(const CYIOptional<T1> &first, const CYIOptional<T2> &second);
243 
244 template<typename T, typename U>
245 constexpr bool operator>(const CYIOptional<T> &first, const U &second);
246 
247 template<typename T, typename U>
248 constexpr bool operator>(const U &first, const CYIOptional<T> &second);
249 
250 template<typename T>
251 constexpr bool operator>(const CYIOptional<T> &first, const CYIEmptyOptionalType &);
252 
253 template<typename T>
254 constexpr bool operator>(const CYIEmptyOptionalType &, const CYIOptional<T> &second);
255 
256 template<typename T1, typename T2>
257 constexpr bool operator>=(const CYIOptional<T1> &first, const CYIOptional<T2> &second);
258 
259 template<typename T, typename U>
260 constexpr bool operator>=(const CYIOptional<T> &first, const U &second);
261 
262 template<typename T, typename U>
263 constexpr bool operator>=(const U &first, const CYIOptional<T> &second);
264 
265 template<typename T1, typename T2>
266 constexpr bool operator<=(const CYIOptional<T1> &first, const CYIOptional<T2> &second);
267 
268 template<typename T, typename U>
269 constexpr bool operator<=(const CYIOptional<T> &first, const U &second);
270 
271 template<typename T, typename U>
272 constexpr bool operator<=(const U &first, const CYIOptional<T> &second);
273 
274 namespace std
275 {
279 template<typename T>
280 void swap(CYIOptional<T> &first, CYIOptional<T> &second);
281 
285 template<typename T>
286 struct hash<CYIOptional<T>>;
287 } // namespace std
288 
291 #include "YiOptional.inl"
292 
293 #undef YI_CONSTEXPR
294 
295 #endif
char m_emptyValue
Definition: YiOptional.h:187
T * operator->()
STL namespace.
YI_CONSTEXPR CYIOptional & operator=(const CYIOptional &other)
constexpr bool operator>=(const CYIOptional< T1 > &first, const CYIOptional< T2 > &second)
constexpr T GetValueOr(U &&alternative) const &
T m_value
Definition: YiOptional.h:188
constexpr bool operator>(const CYIOptional< T1 > &first, const CYIOptional< T2 > &second)
constexpr CYIOptional< T > YiMakeOptional(Args &&... args)
YI_CONSTEXPR void Reset()
The type of the value used to assign &#39;empty&#39; to a CYIOptional object.
Definition: YiOptional.h:25
constexpr bool operator==(const CYIOptional< T1 > &first, const CYIOptional< T2 > &second)
A container class that either contains a value or &#39;nothing&#39;.
Definition: YiOptional.h:58
YI_CONSTEXPR void Swap(CYIOptional &other)
#define YI_CONSTEXPR
Definition: YiOptional.h:12
constexpr bool HasValue() const
constexpr bool operator!=(const CYIOptional< T1 > &first, const CYIOptional< T2 > &second)
T & GetValue() &
constexpr CYIOptional()
constexpr CYIEmptyOptionalType YiEmptyOptional
A value used to assign &#39;empty&#39; to a CYIOptional object. While {} can also be assigned, using this value may be more descriptive.
Definition: YiOptional.h:36
T & Emplace(Args &&... args)