PRCYCoin  2.0.0.7rc1
P2P Digital Currency
optional.hpp
Go to the documentation of this file.
1 /* A lightweight version of C++1y optional */
2 
3 #ifndef __ZXCVBN__OPTIONAL_HPP
4 #define __ZXCVBN__OPTIONAL_HPP
5 
6 #include <functional>
7 #include <new>
8 #include <stdexcept>
9 #include <type_traits>
10 
11 #include <cstdint>
12 #include <cassert>
13 
14 namespace zxcvbn {
15 
16 namespace optional {
17 
18 class nullopt_t {
19  public:
20  constexpr nullopt_t() {}
21 };
22 
23 constexpr nullopt_t nullopt;
24 
25 class bad_optional_access : public std::logic_error {
26  public:
27  bad_optional_access(const char *a) : std::logic_error(a) {}
28 };
29 
30 template <class T>
31 class optional {
32  static_assert(!std::is_reference<T>::value, "cannot use optional with a reference type!");
33 
34  union {
36  T _val;
37  };
38  bool _engaged;
39 
40  template<class Optional>
41  void _init(Optional && o) {
42  if (o) {
43  new (this) optional(*std::forward<Optional>(o));
44  }
45  else {
46  new (this) optional();
47  }
48  }
49 
50  template<class T2>
51  optional & _assign(T2 && val) {
52  this->~optional();
53  new (this) optional(std::forward<T2>(val));
54  return *this;
55  }
56 
57  public:
58  constexpr optional() : _null_state('\0'), _engaged(false) {}
59 
60  constexpr optional(nullopt_t) : optional() {}
61 
62  constexpr optional(const T & val) : _val(val), _engaged(true) {}
63  constexpr optional(T && val) : _val(std::move(val)), _engaged(true) {}
64 
65  optional(const optional & val) {
66  _init(val);
67  }
68 
69  optional(optional && val) {
70  _init(std::move(val));
71  }
72 
73  template<class U,
74  std::enable_if_t<std::is_same<std::decay_t<U>, T>::value> * = nullptr>
75  optional & operator=(U && val) {
76  return _assign(std::forward<U>(val));
77  }
78 
79  optional & operator=(const optional & val) {
80  return _assign(val);
81  }
82 
84  return _assign(std::move(val));
85  }
86 
88  if (_engaged) {
89  _val.~T();
90  }
91  }
92 
93  constexpr const T *operator->() const {
94  return &_val;
95  }
96 
97  constexpr T *operator->() {
98  return &_val;
99  }
100 
101  constexpr const T & operator*() const & {
102  return _val;
103  }
104 
105  constexpr T & operator*() & {
106  return _val;
107  }
108 
109  constexpr T && operator*() const && {
110  return std::move(_val);
111  }
112 
113  constexpr T && operator*() && {
114  return std::move(_val);
115  }
116 
117  constexpr explicit operator bool() const { return _engaged; }
118 };
119 
120 template <class T>
121 constexpr bool operator==(optional<T> f, nullopt_t) {
122  return !f;
123 }
124 
125 template <class T>
126 constexpr bool operator!=(optional<T> f, nullopt_t n) {
127  return !(f == n);
128 }
129 
130 template <class T>
131 constexpr bool operator==(nullopt_t, optional<T> f) {
132  return !f;
133 }
134 
135 template <class T>
136 constexpr bool operator!=(nullopt_t n, optional<T> f) {
137  return !(n == f);
138 }
139 
140 template <class T>
141 constexpr bool operator==(optional<T> a, optional<T> b) {
142  return a && b ? *a == *b : !a && !b;
143 }
144 
145 template <class T>
146 constexpr bool operator!=(optional<T> a, optional<T> b) {
147  return !(a == b);
148 }
149 
150 template <class T>
152  return optional<typename std::decay<T>::type>(std::forward<T>(value));
153 }
154 
155 }
156 
157 }
158 
159 namespace std {
160 
161 template<class T>
162 struct hash<zxcvbn::optional::optional<T>> {
163  std::size_t operator()(const zxcvbn::optional::optional<T> & v) const {
164  if (!v) return 0;
165  return std::hash<T>{}(*v);
166  }
167 };
168 
169 }
170 
171 #endif
zxcvbn::optional::make_optional
constexpr optional< typename std::decay< T >::type > make_optional(T &&value)
Definition: optional.hpp:151
zxcvbn::optional::optional::operator*
constexpr const T & operator*() const &
Definition: optional.hpp:101
zxcvbn::optional::nullopt_t::nullopt_t
constexpr nullopt_t()
Definition: optional.hpp:20
zxcvbn::optional::optional::operator=
optional & operator=(U &&val)
Definition: optional.hpp:75
zxcvbn::optional::nullopt_t
Definition: optional.hpp:18
b
void const uint64_t * b
Definition: field_5x52_asm_impl.h:10
zxcvbn::optional::optional::optional
constexpr optional(T &&val)
Definition: optional.hpp:63
zxcvbn::optional::operator==
constexpr bool operator==(optional< T > f, nullopt_t)
Definition: optional.hpp:121
zxcvbn::optional::optional::_val
T _val
Definition: optional.hpp:36
zxcvbn::optional::optional::optional
constexpr optional(const T &val)
Definition: optional.hpp:62
Optional
boost::optional< T > Optional
Substitute for C++17 std::optional.
Definition: optional.h:12
zxcvbn
Definition: _frequency_lists.cpp:7
zxcvbn::optional::optional::operator*
constexpr T && operator*() &&
Definition: optional.hpp:113
zxcvbn::optional::optional::_null_state
char _null_state
Definition: optional.hpp:35
zxcvbn::optional::optional::operator=
optional & operator=(const optional &val)
Definition: optional.hpp:79
zxcvbn::optional::optional::operator->
constexpr const T * operator->() const
Definition: optional.hpp:93
zxcvbn::optional::optional::_assign
optional & _assign(T2 &&val)
Definition: optional.hpp:51
zxcvbn::optional::optional::_engaged
bool _engaged
Definition: optional.hpp:38
zxcvbn::optional::optional::operator->
constexpr T * operator->()
Definition: optional.hpp:97
zxcvbn::optional::nullopt
constexpr nullopt_t nullopt
Definition: optional.hpp:23
zxcvbn::optional::optional::optional
constexpr optional()
Definition: optional.hpp:58
zxcvbn::optional::optional::operator*
constexpr T & operator*() &
Definition: optional.hpp:105
zxcvbn::optional::optional::operator*
constexpr T && operator*() const &&
Definition: optional.hpp:109
zxcvbn::optional::optional::operator=
optional & operator=(optional &&val)
Definition: optional.hpp:83
zxcvbn::optional::optional::optional
constexpr optional(nullopt_t)
Definition: optional.hpp:60
zxcvbn::optional::optional::~optional
~optional()
Definition: optional.hpp:87
std
Definition: adjacency_graphs.hpp:25
zxcvbn::optional::optional::optional
optional(const optional &val)
Definition: optional.hpp:65
zxcvbn::optional::operator!=
constexpr bool operator!=(optional< T > f, nullopt_t n)
Definition: optional.hpp:126
std::hash< zxcvbn::optional::optional< T > >::operator()
std::size_t operator()(const zxcvbn::optional::optional< T > &v) const
Definition: optional.hpp:163
zxcvbn::optional::bad_optional_access
Definition: optional.hpp:25
zxcvbn::optional::optional
Definition: optional.hpp:31
zxcvbn::optional::optional::_init
void _init(Optional &&o)
Definition: optional.hpp:41
zxcvbn::optional::bad_optional_access::bad_optional_access
bad_optional_access(const char *a)
Definition: optional.hpp:27
zxcvbn::optional::optional::optional
optional(optional &&val)
Definition: optional.hpp:69