ModErn Text Analysis
META Enumerates Textual Applications
optional.h
Go to the documentation of this file.
1 
10 #ifndef META_UTIL_OPTIONAL_H_
11 #define META_UTIL_OPTIONAL_H_
12 
13 #include "meta/config.h"
14 
15 #if META_HAS_EXPERIMENTAL_OPTIONAL
16 #include <experimental/optional>
17 namespace meta
18 {
19 namespace util
20 {
21 template <class T>
22 using optional = std::experimental::optional<T>;
23 
25 }
26 }
27 #else
28 #include "meta/util/comparable.h"
29 #include <stdexcept>
30 #include <type_traits>
31 
32 namespace meta
33 {
34 namespace util
35 {
36 
40 constexpr struct trivial_init_t
41 {
42 } trivial_init{};
43 
47 struct nullopt_t
48 {
50  struct init
51  {
52  };
53  constexpr nullopt_t(init){};
54 };
57 
62 {
63 };
64 
68 template <class T>
74 
78  T value_;
79 
84 
92  template <class... Args>
93  optional_storage(Args&&... args);
94 
99  {
100  /* nothing */
101  }
102 };
103 
114 template <class T>
115 class optional : public util::comparable<optional<T>>
116 {
117  public:
121  optional();
122 
128 
133  optional(const T& value);
134 
140  optional(T&& value);
141 
145  optional(const optional&);
146 
150  optional(optional&&);
151 
155  optional& operator=(optional);
156 
161  ~optional();
162 
167  void swap(optional& other);
168 
174  const T& operator*() const;
175 
181  T& operator*();
182 
190  const T* operator->() const;
191 
199  T* operator->();
200 
204  explicit operator bool() const;
205 
209  void clear();
210 
216  template <class U>
217  T value_or(U&& default_value) const&;
218 
224  template <class U>
225  T value_or(U&& default_value) &&;
226 
227  private:
234  const T* dataptr() const;
235 
242  T* dataptr();
243 
248 
253 };
254 
255 template <class T>
256 bool operator<(const optional<T>& lhs, const optional<T>& rhs);
257 
262 class bad_optional_access : public std::runtime_error
263 {
264  public:
265  using std::runtime_error::runtime_error;
266 };
267 }
268 }
269 
270 #include "meta/util/optional.tcc"
271 #endif // !META_HAS_EXPERIMENTAL_OPTIONAL
272 #endif // META_UTIL_OPTIONAL_H_
A tag for trivial initialization of optional storage.
Definition: optional.h:40
A class for representing optional values.
Definition: optional.h:115
optional_storage< T > storage_
The storage for this optional.
Definition: optional.h:252
A dummy type for representing a disengaged option<T>.
Definition: optional.h:47
Exception thrown when trying to obtain the value of a non-engaged optional.
Definition: optional.h:262
bool initialized_
Whether or not this optional is engaged.
Definition: optional.h:247
A dummy type for optional storage.
Definition: optional.h:61
The ModErn Text Analysis toolkit is a suite of natural language processing, classification, information retrieval, data mining, and other applications of text processing.
Definition: analyzer.h:25
constexpr nullopt_t nullopt
A global nullopt_t constant.
Definition: optional.h:56
A storage class for the optional<T> class.
Definition: optional.h:69
A CRTP base class that allows for inheritance of all comparator operators given that the derived clas...
Definition: comparable.h:28
An empty object.
Definition: optional.h:50
optional_dummy_t dummy_
A dummy value.
Definition: optional.h:73
~optional_storage()
no-op destructor.
Definition: optional.h:98
T value_
The contained value of an option<T> that is engaged.
Definition: optional.h:78