6 #ifndef DMLC_OPTIONAL_H_ 7 #define DMLC_OPTIONAL_H_ 16 #include "./logging.h" 23 #if defined(_MSC_VER) && _MSC_VER < 1900 54 is_none = other.is_none;
56 new (&val) T(other.
value());
62 reinterpret_cast<T*
>(&val)->~T();
67 std::swap(val, other.val);
68 std::swap(is_none, other.is_none);
95 return *
reinterpret_cast<T*
>(&val);
99 return *
reinterpret_cast<const T*
>(&val);
103 return this->is_none == other.is_none &&
104 (this->is_none ==
true || this->value() == other.
value());
111 throw std::logic_error(
"bad optional access");
113 return *
reinterpret_cast<const T*
>(&val);
116 explicit operator bool()
const {
return !is_none; }
124 typename std::aligned_storage<sizeof(T), alignof(T)>::type val;
141 std::ostream &operator<<(std::ostream &os, const optional<T> &t) {
170 std::streampos origin = is.tellg();
172 if (is.fail() || buf[0] !=
'N' || buf[1] !=
'o' ||
173 buf[2] !=
'n' || buf[3] !=
'e') {
179 if (std::is_integral<T>::value && !is.eof() && is.peek() ==
'L') is.get();
212 while (isalnum(is.peek()))
213 s.push_back(is.get());
216 std::transform(s.begin(), s.end(), s.begin(), ::tolower);
217 if (s ==
"1" || s ==
"true")
219 else if (s ==
"0" || s ==
"false")
221 else if (s ==
"none")
224 is.setstate(std::ios::failbit);
244 struct hash<
dmlc::optional<T> > {
251 std::hash<bool> hash_bool;
261 #endif // DMLC_OPTIONAL_H_ c++17 compatible optional class.
Definition: optional.h:43
~optional()
deconstructor
Definition: optional.h:60
size_t operator()(const dmlc::optional< T > &val) const
returns hash of the optional value.
Definition: optional.h:250
optional(const T &value)
construct an optional object with value
Definition: optional.h:48
Definition: optional.h:241
std::istream & operator>>(std::istream &is, optional< T > &t)
parse a string object into optional<T>
Definition: optional.h:168
optional(const optional< T > &other)
construct an optional object with another optional object
Definition: optional.h:53
optional< T > & operator=(const T &value)
set this object to hold value
Definition: optional.h:74
size_t HashCombine(size_t key, const T &value)
hash an object and combines the key with previous keys
Definition: common.h:37
bool isspace(char c)
Inline implementation of isspace(). Tests whether the given character is a whitespace letter...
Definition: strtonum.h:26
optional< T > & operator=(nullopt_t)
clear the value this object is holding. optional<T> x = nullopt;
Definition: optional.h:89
namespace for dmlc
Definition: array_view.h:12
const T & value() const
return the holded value. throws std::logic_error if holding no value
Definition: optional.h:109
void swap(optional< T > &other)
swap two optional
Definition: optional.h:66
constexpr const nullopt_t nullopt
Definition: optional.h:33
bool operator==(const optional< T > &other) const
equal comparison
Definition: optional.h:102
optional< T > & operator=(const optional< T > &other)
set this object to hold the same value with other
Definition: optional.h:82
T & operator*()
non-const dereference operator
Definition: optional.h:94
constexpr nullopt_t(int a)
dummy constructor
Definition: optional.h:28
bool has_value() const
whether this object is holding a value (alternate form).
Definition: optional.h:118
defines some common utility function.
dummy type for assign null to optional
Definition: optional.h:22
DMLC_DECLARE_TYPE_NAME(optional< int >,"int or None")
description for optional int
const T & operator*() const
const dereference operator
Definition: optional.h:98
optional()
construct an optional object that contains no value
Definition: optional.h:46
type traits information header