| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#ifndef DMLC_OPTIONAL_H_ |
| 7 |
#define DMLC_OPTIONAL_H_ |
| 8 |
|
| 9 |
#include <algorithm> |
| 10 |
#include <iostream> |
| 11 |
#include <string> |
| 12 |
#include <utility> |
| 13 |
|
| 14 |
#include "./base.h" |
| 15 |
#include "./common.h" |
| 16 |
#include "./logging.h" |
| 17 |
#include "./type_traits.h" |
| 18 |
|
| 19 |
namespace dmlc { |
| 20 |
|
| 21 |
|
| 22 |
struct nullopt_t { |
| 23 |
#if defined(_MSC_VER) && _MSC_VER < 1900 |
| 24 |
|
| 25 |
explicit nullopt_t(int a) {} |
| 26 |
#else |
| 27 |
|
| 28 |
constexpr explicit nullopt_t(int) {} |
| 29 |
#endif |
| 30 |
}; |
| 31 |
|
| 32 |
|
| 33 |
constexpr const nullopt_t nullopt = nullopt_t(0); |
| 34 |
|
| 35 |
template <typename T> |
| 36 |
using decay_t = typename std::decay<T>::type; |
| 37 |
template <bool B, typename T = void> |
| 38 |
using enable_if_t = typename std::enable_if<B, T>::type; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
struct in_place_t { |
| 44 |
explicit in_place_t() = default; |
| 45 |
}; |
| 46 |
|
| 47 |
static constexpr in_place_t in_place{}; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
template <typename T> |
| 53 |
class optional; |
| 54 |
template <typename T, typename U, typename Other> |
| 55 |
using enable_constructor_from_other |
| 56 |
= enable_if_t<std::is_constructible<T, Other>::value |
| 57 |
&& !std::is_constructible<T, optional<U> &>::value |
| 58 |
&& !std::is_constructible<T, optional<U> &&>::value |
| 59 |
&& !std::is_convertible<optional<U> &, T>::value |
| 60 |
&& !std::is_convertible<optional<U> &&, T>::value |
| 61 |
&& !std::is_constructible<T, const optional<U> &>::value |
| 62 |
&& !std::is_constructible<T, const optional<U> &&>::value |
| 63 |
&& !std::is_convertible<const optional<U> &, T>::value |
| 64 |
&& !std::is_convertible<const optional<U> &&, T>::value>; |
| 65 |
template <typename T, typename U> |
| 66 |
using enable_constructor_from_value = enable_if_t<std::is_constructible<T, U &&>::value |
| 67 |
&& !std::is_same<decay_t<U>, in_place_t>::value |
| 68 |
&& !std::is_same<optional<T>, decay_t<U>>::value>; |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
template <typename T> |
| 77 |
class optional { |
| 78 |
public: |
| 79 |
using value_type = T; |
| 80 |
|
| 81 |
optional() : is_none(true) {} |
| 82 |
|
| 83 |
optional(dmlc::nullopt_t) noexcept : is_none(true) {} |
| 84 |
|
| 85 |
optional(const optional<T> &other) { |
| 86 |
|
| 87 |
#if __GNUC__ >= 6 |
| 88 |
|
| 89 |
#endif |
| 90 |
is_none = other.is_none; |
| 91 |
if (!is_none) { |
| 92 |
new (&val) T(other.value()); |
| 93 |
} |
| 94 |
|
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
optional(optional &&other) noexcept( |
| 99 |
std::is_nothrow_move_constructible<T>::value && std::is_nothrow_move_assignable<T>::value) { |
| 100 |
if (!other.has_value()) { |
| 101 |
reset(); |
| 102 |
} else if (has_value()) { |
| 103 |
**this = std::move(*other); |
| 104 |
} else { |
| 105 |
new (&val) T(std::move(*other)); |
| 106 |
is_none = false; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
template <typename... Args> |
| 112 |
explicit optional( |
| 113 |
enable_if_t<std::is_constructible<T, Args...>::value, in_place_t>, Args &&...args) { |
| 114 |
new (&val) T(std::forward<Args>(args)...); |
| 115 |
is_none = false; |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
template <typename U, typename... Args> |
| 120 |
explicit optional( |
| 121 |
enable_if_t<std::is_constructible<T, std::initializer_list<U> &, Args &&...>::value, |
| 122 |
in_place_t>, |
| 123 |
std::initializer_list<U> ilist, Args &&...args) { |
| 124 |
construct(ilist, std::forward<Args>(args)...); |
| 125 |
} |
| 126 |
|
| 127 |
template <typename U = value_type, enable_if_t<std::is_convertible<U &&, T>::value> * = nullptr, |
| 128 |
enable_constructor_from_value<T, U> * = nullptr> |
| 129 |
optional(U &&other) noexcept { |
| 130 |
new (&val) T(std::forward<U>(other)); |
| 131 |
is_none = false; |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
template <typename U = value_type, enable_if_t<!std::is_convertible<U &&, T>::value> * = nullptr, |
| 136 |
enable_constructor_from_value<T, U> * = nullptr> |
| 137 |
explicit optional(U &&other) noexcept { |
| 138 |
new (&val) T(std::forward<U>(other)); |
| 139 |
is_none = false; |
| 140 |
} |
| 141 |
|
| 142 |
template <typename U, enable_constructor_from_other<T, U, const U &> * = nullptr, |
| 143 |
enable_if_t<std::is_convertible<const U &, T>::value> * = nullptr> |
| 144 |
optional(const optional<U> &other) { |
| 145 |
if (other.has_value()) { |
| 146 |
construct(*other); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
template <typename U, enable_constructor_from_other<T, U, const U &> * = nullptr, |
| 151 |
enable_if_t<!std::is_convertible<const U &, T>::value> * = nullptr> |
| 152 |
explicit optional(const optional<U> &other) { |
| 153 |
if (other.has_value()) { |
| 154 |
construct(*other); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
template <typename U, enable_constructor_from_other<T, U, U &&> * = nullptr, |
| 159 |
enable_if_t<std::is_convertible<U &&, T>::value> * = nullptr> |
| 160 |
optional(optional<U> &&other) { |
| 161 |
if (other.has_value()) { |
| 162 |
construct(std::move(*other)); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
template <typename U, enable_constructor_from_other<T, U, U &&> * = nullptr, |
| 167 |
enable_if_t<!std::is_convertible<U &&, T>::value> * = nullptr> |
| 168 |
explicit optional(optional<U> &&other) { |
| 169 |
if (other.has_value()) { |
| 170 |
construct(std::move(*other)); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
void reset() noexcept { |
| 175 |
if (!has_value()) { |
| 176 |
return; |
| 177 |
} |
| 178 |
(**this).T::~T(); |
| 179 |
is_none = true; |
| 180 |
} |
| 181 |
|
| 182 |
~optional() { |
| 183 |
if (!is_none) { |
| 184 |
reinterpret_cast<T *>(&val)->~T(); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
void swap(optional<T> &other) { |
| 189 |
std::swap(val, other.val); |
| 190 |
std::swap(is_none, other.is_none); |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
optional<T> &operator=(const T &value) { |
| 197 |
(optional<T>(value)).swap(*this); |
| 198 |
return *this; |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
optional<T> &operator=(const optional<T> &other) { |
| 205 |
|
| 206 |
#if __GNUC__ >= 6 |
| 207 |
|
| 208 |
#endif |
| 209 |
(optional<T>(other)).swap(*this); |
| 210 |
return *this; |
| 211 |
|
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
optional<T> &operator=(nullopt_t) { |
| 217 |
(optional<T>()).swap(*this); |
| 218 |
return *this; |
| 219 |
} |
| 220 |
|
| 221 |
T &operator*() { |
| 222 |
return *reinterpret_cast<T *>(&val); |
| 223 |
} |
| 224 |
|
| 225 |
const T &operator*() const { |
| 226 |
return *reinterpret_cast<const T *>(&val); |
| 227 |
} |
| 228 |
|
| 229 |
bool operator==(const optional<T> &other) const { |
| 230 |
return this->is_none == other.is_none |
| 231 |
&& (this->is_none == true || this->value() == other.value()); |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
T &value() & { |
| 237 |
if (is_none) { |
| 238 |
throw std::logic_error("bad optional access"); |
| 239 |
} |
| 240 |
return *reinterpret_cast<T *>(&val); |
| 241 |
} |
| 242 |
const T &value() const & { |
| 243 |
if (is_none) { |
| 244 |
throw std::logic_error("bad optional access"); |
| 245 |
} |
| 246 |
return *reinterpret_cast<const T *>(&val); |
| 247 |
} |
| 248 |
T &&value() && { |
| 249 |
if (is_none) { |
| 250 |
throw std::logic_error("bad optional access"); |
| 251 |
} |
| 252 |
return std::move(value()); |
| 253 |
} |
| 254 |
const T &&value() const && { |
| 255 |
if (is_none) { |
| 256 |
throw std::logic_error("bad optional access"); |
| 257 |
} |
| 258 |
return std::move(value()); |
| 259 |
} |
| 260 |
|
| 261 |
explicit operator bool() const { |
| 262 |
return !is_none; |
| 263 |
} |
| 264 |
|
| 265 |
bool has_value() const { |
| 266 |
return operator bool(); |
| 267 |
} |
| 268 |
|
| 269 |
private: |
| 270 |
|
| 271 |
bool is_none = true; |
| 272 |
|
| 273 |
typename std::aligned_storage<sizeof(T), alignof(T)>::type val; |
| 274 |
|
| 275 |
template <typename... Args> |
| 276 |
void construct(Args &&...args) noexcept { |
| 277 |
new (std::addressof(val)) T(std::forward<Args>(args)...); |
| 278 |
is_none = false; |
| 279 |
} |
| 280 |
}; |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
template <typename T> |
| 296 |
std::ostream &operator<<(std::ostream &os, const optional<T> &t) { |
| 297 |
if (t) { |
| 298 |
os << *t; |
| 299 |
} else { |
| 300 |
os << "None"; |
| 301 |
} |
| 302 |
return os; |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
template <typename T> |
| 323 |
std::istream &operator>>(std::istream &is, optional<T> &t) { |
| 324 |
char buf[4]; |
| 325 |
std::streampos origin = is.tellg(); |
| 326 |
is.read(buf, 4); |
| 327 |
if (is.fail() || buf[0] != 'N' || buf[1] != 'o' || buf[2] != 'n' || buf[3] != 'e') { |
| 328 |
is.clear(); |
| 329 |
is.seekg(origin); |
| 330 |
T x; |
| 331 |
is >> x; |
| 332 |
t = x; |
| 333 |
if (std::is_integral<T>::value && !is.eof() && is.peek() == 'L') { |
| 334 |
is.get(); |
| 335 |
} |
| 336 |
} else { |
| 337 |
t = nullopt; |
| 338 |
} |
| 339 |
return is; |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
inline std::istream &operator>>(std::istream &is, optional<bool> &t) { |
| 362 |
|
| 363 |
while (isspace(is.peek())) { |
| 364 |
is.get(); |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
std::string s; |
| 369 |
while (isalnum(is.peek())) { |
| 370 |
s.push_back(is.get()); |
| 371 |
} |
| 372 |
|
| 373 |
if (!is.fail()) { |
| 374 |
std::transform(s.begin(), s.end(), s.begin(), ::tolower); |
| 375 |
if (s == "1" || s == "true") { |
| 376 |
t = true; |
| 377 |
} else if (s == "0" || s == "false") { |
| 378 |
t = false; |
| 379 |
} else if (s == "none") { |
| 380 |
t = nullopt; |
| 381 |
} else { |
| 382 |
is.setstate(std::ios::failbit); |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
return is; |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
DMLC_DECLARE_TYPE_NAME(optional<int>, "int or None"); |
| 391 |
|
| 392 |
DMLC_DECLARE_TYPE_NAME(optional<bool>, "boolean or None"); |
| 393 |
|
| 394 |
DMLC_DECLARE_TYPE_NAME(optional<float>, "float or None"); |
| 395 |
|
| 396 |
DMLC_DECLARE_TYPE_NAME(optional<double>, "double or None"); |
| 397 |
|
| 398 |
} |
| 399 |
|
| 400 |
namespace std { |
| 401 |
|
| 402 |
template <typename T> |
| 403 |
struct hash<dmlc::optional<T>> { |
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
size_t operator()(const dmlc::optional<T> &val) const { |
| 410 |
std::hash<bool> hash_bool; |
| 411 |
size_t res = hash_bool(val.has_value()); |
| 412 |
if (val.has_value()) { |
| 413 |
res = dmlc::HashCombine(res, val.value()); |
| 414 |
} |
| 415 |
return res; |
| 416 |
} |
| 417 |
}; |
| 418 |
} |
| 419 |
|
| 420 |
#endif |