ModErn Text Analysis
META Enumerates Textual Applications
optional.h
Go to the documentation of this file.
1 
10 #ifndef META_OPTIONAL_H_
11 #define META_OPTIONAL_H_
12 
13 #include <stdexcept>
14 #include <type_traits>
15 #include "util/comparable.h"
16 
17 namespace meta
18 {
19 namespace util
20 {
21 
25 constexpr struct trivial_init_t
26 {
27 } trivial_init{};
28 
32 struct nullopt_t
33 {
35  struct init
36  {
37  };
38  constexpr nullopt_t(init) {};
39 };
42 
47 {
48 };
49 
53 template <class T>
55 {
60 
64  T value_;
65 
70 
78  template <class... Args>
79  optional_storage(Args&&... args);
80 
85  {
86  /* nothing */
87  }
88 };
89 
100 template <class T>
101 class optional : public util::comparable<optional<T>>
102 {
103  public:
107  optional();
108 
113  optional(nullopt_t);
114 
119  optional(const T& value);
120 
126  optional(T&& value);
127 
131  optional(const optional&);
132 
136  optional(optional&&);
137 
142 
147  ~optional();
148 
153  void swap(optional& other);
154 
160  const T& operator*() const;
161 
167  T& operator*();
168 
176  const T* operator->() const;
177 
185  T* operator->();
186 
190  explicit operator bool() const;
191 
195  void clear();
196 
197  private:
204  const T* dataptr() const;
205 
212  T* dataptr();
213 
218 
223 };
224 
225 template <class T>
226 bool operator<(const optional<T>& lhs, const optional<T>& rhs);
227 
232 class bad_optional_access : public std::runtime_error
233 {
234  public:
235  using std::runtime_error::runtime_error;
236 };
237 }
238 }
239 
240 #include "util/optional.tcc"
241 #endif
void swap(optional &other)
Swaps the current optional instance with the parameter.
Definition: optional.tcc:80
const T * operator->() const
Member access operator to the value contained in the optional.
Definition: optional.tcc:121
A tag for trivial initialization of optional storage.
Definition: optional.h:25
A class for representing optional values.
Definition: vocabulary_map.h:21
optional()
Default constructor, creates a disengaged optional value.
Definition: optional.tcc:29
const T * dataptr() const
Helper function to obtain the address of the contained value.
Definition: optional.tcc:151
~optional()
Destructor.
Definition: optional.tcc:99
const T & operator*() const
Obtains the value contained in the optional.
Definition: optional.tcc:105
optional_storage< T > storage_
The storage for this optional.
Definition: optional.h:222
optional & operator=(optional)
Assignment operator.
Definition: optional.tcc:73
A dummy type for representing a disengaged option.
Definition: optional.h:32
Exception thrown when trying to obtain the value of a non-engaged optional.
Definition: optional.h:232
bool initialized_
Whether or not this optional is engaged.
Definition: optional.h:217
A dummy type for optional storage.
Definition: optional.h:46
The ModErn Text Analysis toolkit is a suite of natural language processing, classification, information retreival, data mining, and other applications of text processing.
Definition: analyzer.h:24
constexpr nullopt_t nullopt
A global nullopt_t constant.
Definition: optional.h:41
optional_storage(trivial_init_t)
Trivial constructor for the storage class.
Definition: optional.tcc:14
void clear()
Empties the optional.
Definition: optional.tcc:143
A storage class for the optional class.
Definition: optional.h:54
A CRTP base class that allows for inheritance of all comparator operators given that the derived clas...
Definition: comparable.h:26
An empty object.
Definition: optional.h:35
optional_dummy_t dummy_
A dummy value.
Definition: optional.h:59
~optional_storage()
no-op destructor.
Definition: optional.h:84
T value_
The contained value of an option that is engaged.
Definition: optional.h:64