Go to the documentation of this file.
6 #ifndef DMLC_OPTIONAL_H_
7 #define DMLC_OPTIONAL_H_
16 #include "./logging.h"
23 #if defined(_MSC_VER) && _MSC_VER < 1900
49 #pragma GCC diagnostic push
51 #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
55 #pragma GCC diagnostic pop
59 #pragma GCC diagnostic push
61 #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
63 is_none = other.is_none;
65 new (&val) T(other.
value());
67 #pragma GCC diagnostic pop
72 reinterpret_cast<T*
>(&val)->~T();
77 std::swap(val, other.val);
78 std::swap(is_none, other.is_none);
105 return *
reinterpret_cast<T*
>(&val);
109 return *
reinterpret_cast<const T*
>(&val);
113 return this->is_none == other.is_none &&
114 (this->is_none ==
true || this->
value() == other.
value());
121 throw std::logic_error(
"bad optional access");
123 return *
reinterpret_cast<const T*
>(&val);
126 explicit operator bool()
const {
return !is_none; }
134 typename std::aligned_storage<
sizeof(T),
alignof(T)>::type val;
180 std::streampos origin = is.tellg();
182 if (is.fail() || buf[0] !=
'N' || buf[1] !=
'o' ||
183 buf[2] !=
'n' || buf[3] !=
'e') {
189 if (std::is_integral<T>::value && !is.eof() && is.peek() ==
'L') is.get();
222 while (isalnum(is.peek()))
223 s.push_back(is.get());
226 std::transform(s.begin(), s.end(), s.begin(), ::tolower);
227 if (s ==
"1" || s ==
"true")
229 else if (s ==
"0" || s ==
"false")
231 else if (s ==
"none")
234 is.setstate(std::ios::failbit);
254 struct hash<
dmlc::optional<T> > {
261 std::hash<bool> hash_bool;
271 #endif // DMLC_OPTIONAL_H_
std::istream & operator>>(std::istream &is, optional< T > &t)
parse a string object into optional<T>
Definition: optional.h:178
optional< T > & operator=(const optional< T > &other)
set this object to hold the same value with other
Definition: optional.h:92
const T & operator*() const
const dereference operator
Definition: optional.h:108
bool isspace(char c)
Inline implementation of isspace(). Tests whether the given character is a whitespace letter.
Definition: strtonum.h:26
bool has_value() const
whether this object is holding a value (alternate form).
Definition: optional.h:128
namespace for dmlc
Definition: array_view.h:12
T & operator*()
non-const dereference operator
Definition: optional.h:104
defines configuration macros
size_t operator()(const dmlc::optional< T > &val) const
returns hash of the optional value.
Definition: optional.h:260
DMLC_DECLARE_TYPE_NAME(optional< int >, "int or None")
description for optional int
size_t HashCombine(size_t key, const T &value)
hash an object and combines the key with previous keys
Definition: common.h:37
optional()
construct an optional object that contains no value
Definition: optional.h:46
~optional()
deconstructor
Definition: optional.h:70
std::ostream & operator<<(std::ostream &os, const optional< T > &t)
serialize an optional object to string.
Definition: optional.h:151
constexpr const nullopt_t nullopt
Definition: optional.h:33
constexpr nullopt_t(int a)
dummy constructor
Definition: optional.h:28
optional(const optional< T > &other)
construct an optional object with another optional object
Definition: optional.h:58
optional< T > & operator=(const T &value)
set this object to hold value
Definition: optional.h:84
defines some common utility function.
optional(const T &value)
construct an optional object with value
Definition: optional.h:48
Definition: optional.h:251
type traits information header
optional< T > & operator=(nullopt_t)
clear the value this object is holding. optional<T> x = nullopt;
Definition: optional.h:99
dummy type for assign null to optional
Definition: optional.h:22
c++17 compatible optional class.
Definition: optional.h:43
const T & value() const
return the holded value. throws std::logic_error if holding no value
Definition: optional.h:119
bool operator==(const optional< T > &other) const
equal comparison
Definition: optional.h:112
void swap(optional< T > &other)
swap two optional
Definition: optional.h:76