You.i Engine
YiOptional.h
Go to the documentation of this file.
1 // © You i Labs Inc. All rights reserved.
2 #ifndef _YI_OPTIONAL_H_
3 #define _YI_OPTIONAL_H_
4 
5 #include "framework/YiPredef.h"
7 
8 #include <algorithm>
9 #include <utility>
10 
11 #ifdef YI_STD_HASH_REQUIRES_FUNCTIONAL_INCLUDE
12 # include <functional>
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  template<typename U = T, typename = typename std::enable_if<std::is_convertible<U, T>::value, char>::type>
92  constexpr CYIOptional(U &&value);
93 
94  ~CYIOptional();
95 
100 
104  template<typename U, typename = typename std::enable_if<std::is_convertible<U, T>::value, char>::type>
105  YI_CONSTEXPR CYIOptional &operator=(const U &value);
106 
113 
117  template<typename U, typename = typename std::enable_if<std::is_convertible<U, T>::value, char>::type>
118  YI_CONSTEXPR CYIOptional &operator=(U &&value);
119 
125  template<typename... Args>
126  T &Emplace(Args &&... args);
127 
133  YI_CONSTEXPR bool HasValue() const;
134  YI_CONSTEXPR explicit operator bool() const;
135 
136  T *operator->();
137  const T *operator->() const;
138 
152  T &GetValue() &;
153  const T &GetValue() const &;
154  T &&GetValue() &&;
155  T &operator*() &;
156  const T &operator*() const &;
157  T &&operator*() &&;
158 
162  template<typename U>
163  YI_CONSTEXPR T GetValueOr(U &&alternative) const &;
164  template<typename U>
165  YI_CONSTEXPR T GetValueOr(U &&alternative) &&;
166 
170  YI_CONSTEXPR void Reset();
171 
175  YI_CONSTEXPR void Swap(CYIOptional &other);
176 
177 private:
178  union {
181  };
182  bool m_hasValue;
183 #ifdef YI_DEBUG
184  mutable bool m_valueValidated;
185 #endif
186 };
187 
191 template<typename T, typename... Args>
193 
194 template<typename T1, typename T2>
195 constexpr bool operator==(const CYIOptional<T1> &first, const CYIOptional<T2> &second);
196 
197 template<typename T, typename U>
198 constexpr bool operator==(const CYIOptional<T> &first, const U &second);
199 
200 template<typename T, typename U>
201 constexpr bool operator==(const U &first, const CYIOptional<T> &second);
202 
203 template<typename T>
204 constexpr bool operator==(const CYIOptional<T> &first, const CYIEmptyOptionalType &);
205 
206 template<typename T>
207 constexpr bool operator==(const CYIEmptyOptionalType &, const CYIOptional<T> &second);
208 
209 template<typename T1, typename T2>
210 constexpr bool operator!=(const CYIOptional<T1> &first, const CYIOptional<T2> &second);
211 
212 template<typename T, typename U>
213 constexpr bool operator!=(const CYIOptional<T> &first, const U &second);
214 
215 template<typename T, typename U>
216 constexpr bool operator!=(const U &first, const CYIOptional<T> &second);
217 
218 template<typename T1, typename T2>
219 constexpr bool operator<(const CYIOptional<T1> &first, const CYIOptional<T2> &second);
220 
221 template<typename T, typename U>
222 constexpr bool operator<(const CYIOptional<T> &first, const U &second);
223 
224 template<typename T, typename U>
225 constexpr bool operator<(const U &first, const CYIOptional<T> &second);
226 
227 template<typename T>
228 constexpr bool operator<(const CYIOptional<T> &first, const CYIEmptyOptionalType &);
229 
230 template<typename T>
231 constexpr bool operator<(const CYIEmptyOptionalType &, const CYIOptional<T> &second);
232 
233 template<typename T1, typename T2>
234 constexpr bool operator>(const CYIOptional<T1> &first, const CYIOptional<T2> &second);
235 
236 template<typename T, typename U>
237 constexpr bool operator>(const CYIOptional<T> &first, const U &second);
238 
239 template<typename T, typename U>
240 constexpr bool operator>(const U &first, const CYIOptional<T> &second);
241 
242 template<typename T>
243 constexpr bool operator>(const CYIOptional<T> &first, const CYIEmptyOptionalType &);
244 
245 template<typename T>
246 constexpr bool operator>(const CYIEmptyOptionalType &, const CYIOptional<T> &second);
247 
248 template<typename T1, typename T2>
249 constexpr bool operator>=(const CYIOptional<T1> &first, const CYIOptional<T2> &second);
250 
251 template<typename T, typename U>
252 constexpr bool operator>=(const CYIOptional<T> &first, const U &second);
253 
254 template<typename T, typename U>
255 constexpr bool operator>=(const U &first, const CYIOptional<T> &second);
256 
257 template<typename T1, typename T2>
258 constexpr bool operator<=(const CYIOptional<T1> &first, const CYIOptional<T2> &second);
259 
260 template<typename T, typename U>
261 constexpr bool operator<=(const CYIOptional<T> &first, const U &second);
262 
263 template<typename T, typename U>
264 constexpr bool operator<=(const U &first, const CYIOptional<T> &second);
265 
266 namespace std
267 {
271 template<typename T>
272 void swap(CYIOptional<T> &first, CYIOptional<T> &second);
273 
277 template<typename T>
278 struct hash<CYIOptional<T>>;
279 } // namespace std
280 
283 #include "utility/internal/YiOptional.inl"
284 
285 #endif
YI_CONSTEXPR bool HasValue() const
char m_emptyValue
Definition: YiOptional.h:179
T * operator->()
STL namespace.
YI_CONSTEXPR CYIOptional & operator=(const CYIOptional &other)
YI_CONSTEXPR T GetValueOr(U &&alternative) const &
constexpr bool operator>=(const CYIOptional< T1 > &first, const CYIOptional< T2 > &second)
T m_value
Definition: YiOptional.h:180
#define YI_CONSTEXPR
Definition: YiPredef.h:38
constexpr bool operator>(const CYIOptional< T1 > &first, const CYIOptional< T2 > &second)
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)
YI_CONSTEXPR CYIOptional< T > YiMakeOptional(Args &&... args)
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)