mn_optional.hpp
Go to the documentation of this file.
1 
19 #ifndef __MINLIB_BASIC_OPTIONAL_H__
20 #define __MINLIB_BASIC_OPTIONAL_H__
21 
22 #include "mn_config.hpp"
23 
24 #include "mn_algorithm.hpp"
25 #include "mn_functional.hpp"
26 
27 namespace mn {
28 
29  struct nullopt_t { };
30 
31  template< typename T>
33  public:
34  using value_type = T;
35  using const_value_type = const T;
36  using reference = T&;
37  using const_reference = const T&;
38  using move_type = T&&;
39  using const_move_type = const T&&;
40 
41  using pointer = T*;
42  using const_pointer = const T*;
43 
45 
46  constexpr basic_optional() noexcept
47  : m_bHasValue( false), m_tValue() { }
48 
49  basic_optional(const self_type& other)
50  : m_bHasValue( other.m_bHasValue), m_tValue(other.m_tValue) { }
51 
52  basic_optional(const self_type&& other) noexcept
53  : m_bHasValue( mn::move(other.m_bHasValue)), m_tValue( mn::move(other.m_tValue)) { }
54 
55 
56  template< typename... Args>
57  constexpr explicit basic_optional(Args&&... args)
58  : m_bHasValue( true), m_tValue( value_type( mn::forward<Args>(args)...)) {}
59 
60  template< typename U = value_type >
61  constexpr explicit basic_optional( U && value)
62  : m_bHasValue( true), m_tValue( mn::forward<U>( value)) { }
63 
65 
69  void reset() noexcept {
71  m_tValue->~T();
72 
73  m_bHasValue = false;
74  }
78  void clear() {
79  m_bHasValue = false;
80  }
81 
86  {
87  m_tValue = value;
88  m_bHasValue = true;
89  return *this;
90  }
91 
97  m_bHasValue = true;
98  return *this;
99  }
100 
104  self_type& assign(const self_type& other) {
105  self_type tmp(other);
106  swap(tmp);
107  return *this;
108  }
112  void swap( self_type& other) {
114  mn::swap(m_tValue, other.m_tValue);
115  }
116 
121  constexpr bool has_value() const noexcept {
122  return m_bHasValue;
123  }
128  assert( has_value());
129  return m_tValue;
130  }
135  assert( has_value());
136  return m_tValue;
137  }
142  constexpr value_type value_or(const value_type value) const {
143  return has_value() ? m_tValue : static_cast<value_type>( value);
144  }
149  template< typename U >
150  constexpr value_type value_or(const U& value) const {
151  return has_value() ? m_tValue : static_cast<value_type>( value);
152  }
153 
157  self_type& operator = (const self_type& other) noexcept {
158  return assign(value);
159  }
160 
164  self_type& operator = ( self_type&& other) noexcept {
165  return assign(value);
166  }
167 
171  constexpr const_pointer operator ->() const {
172  return m_tValue;
173  }
174 
179  return m_tValue;
180  }
181 
185  constexpr const_reference operator *() const {
186  return m_tValue;
187  }
188 
193  return m_tValue;
194  }
195 
200  explicit operator bool() const noexcept {
201  return m_bHasValue;
202  }
203  private:
212  };
213 
214  template <typename T>
216  a.swap(b);
217  }
218 
219 
220  template< typename T, typename U >
221  inline constexpr bool operator == (const basic_optional<T>& x, const basic_optional<U>& y) {
222  return bool(x) != bool(y) ? false : !bool( x) ? true : *x == *y;
223  }
224 
225  template< typename T, typename U >
226  inline constexpr bool operator != (const basic_optional<T>& x, const basic_optional<U>& y) {
227  return !(x.value() == y.value());
228  }
229 
230  template< typename T, typename U >
231  inline constexpr bool operator < (const basic_optional<T>& x, const basic_optional<U>& y) {
232  return (!y) ? false : (!x) ? true : *x < *y;
233  }
234 
235  template< typename T, typename U >
236  inline constexpr bool operator > (const basic_optional<T>& x, const basic_optional<U>& y) {
237  return (y.value() < x.value());
238  }
239 
240  template< typename T, typename U >
241  inline constexpr bool operator <= (const basic_optional<T>& x, const basic_optional<U>& y) {
242  return !(y.value() < x.value());
243  }
244 
245  template< typename T, typename U >
246  inline constexpr bool operator >= (const basic_optional<T>& x, const basic_optional<U>& y) {
247  return !(x.value() < y.value());
248  }
249 
250  // Comparison with nullopt
251 
252  template< typename T >
253  inline constexpr bool operator == (const basic_optional<T>& x, nullopt_t) noexcept {
254  return (!x);
255  }
256 
257  template< typename T >
258  inline constexpr bool operator == (nullopt_t, const basic_optional<T>& x) noexcept {
259  return (!x);
260  }
261 
262  template< typename T >
263  inline constexpr bool operator != (const basic_optional<T>& x, nullopt_t) noexcept {
264  return bool(x);
265  }
266 
267  template< typename T >
268  inline constexpr bool operator != (nullopt_t, const basic_optional<T>& x) noexcept {
269  return bool(x);
270  }
271 
272  template< typename T >
273  inline constexpr bool operator < (const basic_optional<T>&, nullopt_t) noexcept {
274  return false;
275  }
276 
277  template< typename T >
278  inline constexpr bool operator < (nullopt_t, const basic_optional<T>& x) noexcept {
279  return bool(x);
280  }
281 
282  template< typename T >
283  inline constexpr bool operator <= (const basic_optional<T>& x, nullopt_t) noexcept {
284  return (!x);
285  }
286 
287  template< typename T >
288  inline constexpr bool operator <= (nullopt_t, const basic_optional<T>&) noexcept {
289  return true;
290  }
291 
292  template< typename T >
293  inline constexpr bool operator > (const basic_optional<T>& x, nullopt_t) noexcept {
294  return bool(x);
295  }
296 
297  template< typename T >
298  inline constexpr bool operator > (nullopt_t, const basic_optional<T>&) noexcept
299  {
300  return false;
301  }
302 
303  template< typename T >
304  inline constexpr bool operator >= (const basic_optional<T>&, nullopt_t) noexcept {
305  return true;
306  }
307 
308  template< typename T >
309  inline constexpr bool operator >= (nullopt_t, const basic_optional<T>& x) noexcept {
310  return (!x);
311  }
312 
313  template< typename T, typename U >
314  inline constexpr bool operator == (const basic_optional<T>& x, const U& v) {
315  return bool(x) ? *x == v : false;
316  }
317 
318  template< typename T, typename U >
319  inline constexpr bool operator == (const U& v, const basic_optional<T>& x) {
320  return bool(x) ? v == *x : false;
321  }
322 
323  template< typename T, typename U >
324  inline constexpr bool operator != (const basic_optional<T>& x, const U& v) {
325  return bool(x) ? *x != v : true;
326  }
327 
328  template< typename T, typename U >
329  inline constexpr bool operator != (const U& v, const basic_optional<T>& x) {
330  return bool(x) ? v != *x : true;
331  }
332 
333  template< typename T, typename U >
334  inline constexpr bool operator < (const basic_optional<T>& x, const U& v) {
335  return bool(x) ? *x < v : true;
336  }
337 
338  template< typename T, typename U >
339  inline constexpr bool operator < (const U& v, const basic_optional<T>& x) {
340  return bool(x) ? v < *x : false;
341  }
342 
343  template< typename T, typename U >
344  inline constexpr bool operator <= (const basic_optional<T>& x, const U& v) {
345  return bool(x) ? *x <= v : true;
346  }
347 
348  template< typename T, typename U >
349  inline constexpr bool operator<=(const U& v, const basic_optional<T>& x) {
350  return bool(x) ? v <= *x : false;
351  }
352 
353  template< typename T, typename U>
354  inline constexpr bool operator>(const basic_optional<T>& x, const U& v) {
355  return bool(x) ? *x > v : false;
356  }
357 
358  template< typename T, typename U>
359  inline constexpr bool operator>(const U& v, const basic_optional<T>& x) {
360  return bool(x) ? v > *x : true;
361  }
362 
363  template< typename T, typename U>
364  inline constexpr bool operator>=(const basic_optional<T>& x, const U& v) {
365  return bool(x) ? *x >= v : false;
366  }
367 
368  template< typename T, typename U>
369  inline constexpr bool operator >= (const U& v, const basic_optional<T>& x) {
370  return bool(x) ? v >= *x : true;
371  }
372 
373  template< typename T >
375  return basic_optional<typename mn::decay<T>::type>( mn::forward<T>( value ) );
376  }
377 
378  template< typename T, typename...Args >
379  constexpr basic_optional<T> make_optional( Args&&... args ) {
380  return basic_optional<T>(mn::forward<Args>(args)...);
381  }
382 #if 0
383  template< class T >
384  struct hash<basic_optional<T>> {
385  mn::size_t operator()( const basic_optional<T>& opt ) const noexcept {
386  return bool(opt) ? hash<T>()(*opt) : 0;
387  }
388  };
389 #endif // 0
390 
391  template <typename T>
393 }
394 
395 #endif // __MINLIB_BASIC_OPTIONAL_H__
Definition: mn_optional.hpp:32
constexpr basic_optional(Args &&... args)
Definition: mn_optional.hpp:57
const_reference value() const
Returns the basic_optional's value.
Definition: mn_optional.hpp:134
bool m_bHasValue
Flag whether or not has this basic_optianel a value.
Definition: mn_optional.hpp:207
~basic_optional()
Definition: mn_optional.hpp:64
constexpr const_reference operator*() const
Definition: mn_optional.hpp:185
void clear()
Clears the basic_optional.
Definition: mn_optional.hpp:78
basic_optional(const self_type &other)
Definition: mn_optional.hpp:49
self_type & assign(const_value_type &value)
Assigns a value to the basic_otional.
Definition: mn_optional.hpp:85
T value_type
Definition: mn_optional.hpp:34
reference value()
Returns the basic_optional's value.
Definition: mn_optional.hpp:127
const T * const_pointer
Definition: mn_optional.hpp:42
const T && const_move_type
Definition: mn_optional.hpp:39
constexpr basic_optional() noexcept
Definition: mn_optional.hpp:46
const T const_value_type
Definition: mn_optional.hpp:35
self_type & assign(const self_type &other)
Assigns another basic_otional.
Definition: mn_optional.hpp:104
const T & const_reference
Definition: mn_optional.hpp:37
T && move_type
Definition: mn_optional.hpp:38
T & reference
Definition: mn_optional.hpp:36
void swap(self_type &other)
Swap this basic_optional with a other.
Definition: mn_optional.hpp:112
constexpr basic_optional(U &&value)
Definition: mn_optional.hpp:61
basic_optional(const self_type &&other) noexcept
Definition: mn_optional.hpp:52
constexpr const_pointer operator->() const
Get the given value as pointer.
Definition: mn_optional.hpp:171
constexpr bool has_value() const noexcept
Have this basic_optional a value?
Definition: mn_optional.hpp:121
constexpr value_type value_or(const value_type value) const
Returns the basic_optional's value, or the given default value when this basic_optional no value have...
Definition: mn_optional.hpp:142
self_type & assign(move_type value)
Moves a value into the basic_otional.
Definition: mn_optional.hpp:95
self_type & operator=(const self_type &other) noexcept
Assigns another basic_otional.
Definition: mn_optional.hpp:157
T * pointer
Definition: mn_optional.hpp:41
void reset() noexcept
Resets the basic_optional.
Definition: mn_optional.hpp:69
constexpr value_type value_or(const U &value) const
Returns the basic_optional's value, or the given default value when this basic_optional no value have...
Definition: mn_optional.hpp:150
value_type m_tValue
A saved / cached copy of the value.
Definition: mn_optional.hpp:211
Basic algorithmens This file is part of the Mini Thread Library (https://github.com/RoseLeBlood/MiniT...
struct mn::memory::detail::ptr_difference T
Definition: mn_atomic_singleton.hpp:38
Definition: mn_allocator_typetraits.hpp:25
constexpr bool operator==(const byte l, const byte r) noexcept
Definition: mn_def.hpp:112
constexpr bool operator!=(const byte l, const byte r) noexcept
Definition: mn_def.hpp:116
constexpr basic_optional< typename mn::decay< T >::type > make_optional(T &&value)
Definition: mn_optional.hpp:374
constexpr bool operator<=(const byte l, const byte r) noexcept
Definition: mn_def.hpp:124
void swap(TAssignable &a, TAssignable &b)
Definition: mn_algorithm.hpp:312
constexpr bool operator<(const byte l, const byte r) noexcept
Definition: mn_def.hpp:120
constexpr bool operator>=(const byte l, const byte r) noexcept
Definition: mn_def.hpp:132
void move(const T *src, const T *last, T *dest)
Definition: mn_algorithm.hpp:100
T && forward(remove_reference_t< T > &t)
Definition: mn_functional.hpp:74
MN_THREAD_CONFIG_SIZE_TYPE size_t
Definition: mn_def.hpp:48
constexpr bool operator>(const byte l, const byte r) noexcept
Definition: mn_def.hpp:128
Definition: mn_optional.hpp:29
const result_type operator()(T &t) const noexcept
Definition: mn_hash.hpp:73
Definition: mn_typetraits.hpp:159