/*! * Copyright (c) 2016 by Contributors * \file optional.h * \brief Container to hold optional data. */ #ifndef DMLC_OPTIONAL_H_ #define DMLC_OPTIONAL_H_ #include #include #include #include #include "./base.h" #include "./common.h" #include "./logging.h" #include "./type_traits.h" namespace dmlc { /*! \brief dummy type for assign null to optional */ struct nullopt_t { #if defined(_MSC_VER) && _MSC_VER < 1900 /*! \brief dummy constructor */ explicit nullopt_t(int a) {} #else /*! \brief dummy constructor */ constexpr explicit nullopt_t(int) {} #endif }; /*! Assign null to optional: optional x = nullopt; */ constexpr const nullopt_t nullopt = nullopt_t(0); /*! \brief C++14 aliases */ template using decay_t = typename std::decay::type; template using enable_if_t = typename std::enable_if::type; /*! \brief disambiguation tags that can be passed to the constructors of * dmlc::optional. A tag type to tell constructor to construct its value in-place */ struct in_place_t { explicit in_place_t() = default; // NOLINT(*) }; /*! \brief A tag to tell constructor to construct its value in-place */ static constexpr in_place_t in_place{}; /*! \brief Is not constructible or convertible from any expression of type (possibly const) dmlc::optional, i.e., the following 8 type traits are all false: Link: https://en.cppreference.com/w/cpp/utility/optional/optional */ template class optional; template using enable_constructor_from_other = enable_if_t::value && !std::is_constructible &>::value && !std::is_constructible &&>::value && !std::is_convertible &, T>::value && !std::is_convertible &&, T>::value && !std::is_constructible &>::value && !std::is_constructible &&>::value && !std::is_convertible &, T>::value && !std::is_convertible &&, T>::value>; template using enable_constructor_from_value = enable_if_t::value && !std::is_same, in_place_t>::value && !std::is_same, decay_t>::value>; /*! * \brief c++17 compatible optional class. * * At any time an optional instance either * hold no value (string representation "None") * or hold a value of type T. */ template class optional { public: using value_type = T; /*! \brief constructs an object that does not contain a value. */ optional() : is_none(true) {} /*! \brief constructs an object that does contain a nullopt value. */ optional(dmlc::nullopt_t) noexcept : is_none(true) {} // NOLINT(*) /*! \brief construct an optional object with another optional object */ optional(const optional &other) { #if __GNUC__ >= 6 #endif is_none = other.is_none; if (!is_none) { new (&val) T(other.value()); } } /*! \brief move constructor: If other contains a value, then stored value is * direct-intialized with it. */ optional(optional &&other) noexcept( std::is_nothrow_move_constructible::value && std::is_nothrow_move_assignable::value) { if (!other.has_value()) { reset(); } else if (has_value()) { **this = std::move(*other); } else { new (&val) T(std::move(*other)); is_none = false; } } /*! \brief constructs an optional object that contains a value, initialized as * if direct-initializing */ template explicit optional( enable_if_t::value, in_place_t>, Args &&...args) { new (&val) T(std::forward(args)...); is_none = false; } /*! \brief Constructs an optional object that contains a value, initialized as * if direct-initializing */ template explicit optional( enable_if_t &, Args &&...>::value, in_place_t>, std::initializer_list ilist, Args &&...args) { construct(ilist, std::forward(args)...); } /*! \brief constructs the stored value with value with `other` parameter.*/ template ::value> * = nullptr, enable_constructor_from_value * = nullptr> optional(U &&other) noexcept { // NOLINT(*) new (&val) T(std::forward(other)); is_none = false; } /*! \brief explicit constructor: constructs the stored value with `other` * parameter. */ template ::value> * = nullptr, enable_constructor_from_value * = nullptr> explicit optional(U &&other) noexcept { new (&val) T(std::forward(other)); is_none = false; } /*! \brief converting copy constructor */ template * = nullptr, enable_if_t::value> * = nullptr> optional(const optional &other) { if (other.has_value()) { construct(*other); } } /*! \brief explicit converting copy constructor */ template * = nullptr, enable_if_t::value> * = nullptr> explicit optional(const optional &other) { if (other.has_value()) { construct(*other); } } /*! \brief converting move constructor */ template * = nullptr, enable_if_t::value> * = nullptr> optional(optional &&other) { if (other.has_value()) { construct(std::move(*other)); } } /*! \brief explicit converting move constructor */ template * = nullptr, enable_if_t::value> * = nullptr> explicit optional(optional &&other) { if (other.has_value()) { construct(std::move(*other)); } } /*! \brief reset */ void reset() noexcept { if (!has_value()) { return; } (**this).T::~T(); is_none = true; } /*! \brief deconstructor */ ~optional() { if (!is_none) { reinterpret_cast(&val)->~T(); } } /*! \brief swap two optional */ void swap(optional &other) { std::swap(val, other.val); std::swap(is_none, other.is_none); } /*! \brief set this object to hold value * \param value the value to hold * \return return self to support chain assignment */ optional &operator=(const T &value) { (optional(value)).swap(*this); return *this; } /*! \brief set this object to hold the same value with other * \param other the other object * \return return self to support chain assignment */ optional &operator=(const optional &other) { #if __GNUC__ >= 6 #endif (optional(other)).swap(*this); return *this; } /*! \brief clear the value this object is holding. * optional x = nullopt; */ optional &operator=(nullopt_t) { (optional()).swap(*this); return *this; } /*! \brief non-const dereference operator */ T &operator*() { // NOLINT(*) return *reinterpret_cast(&val); } /*! \brief const dereference operator */ const T &operator*() const { return *reinterpret_cast(&val); } /*! \brief equal comparison */ bool operator==(const optional &other) const { return this->is_none == other.is_none && (this->is_none == true || this->value() == other.value()); } /*! \brief return the holded value. * throws std::logic_error if holding no value */ T &value() & { if (is_none) { throw std::logic_error("bad optional access"); } return *reinterpret_cast(&val); } const T &value() const & { if (is_none) { throw std::logic_error("bad optional access"); } return *reinterpret_cast(&val); } T &&value() && { if (is_none) { throw std::logic_error("bad optional access"); } return std::move(value()); } const T &&value() const && { if (is_none) { throw std::logic_error("bad optional access"); } return std::move(value()); } /*! \brief whether this object is holding a value */ explicit operator bool() const { return !is_none; } /*! \brief whether this object is holding a value (alternate form). */ bool has_value() const { return operator bool(); } private: // whether this is none bool is_none = true; // on stack storage of value typename std::aligned_storage::type val; template void construct(Args &&...args) noexcept { new (std::addressof(val)) T(std::forward(args)...); is_none = false; } }; /*! \brief serialize an optional object to string. * * \code * dmlc::optional x; * std::cout << x; // None * x = 0; * std::cout << x; // 0 * \endcode * * \param os output stream * \param t source optional object * \return output stream */ template std::ostream &operator<<(std::ostream &os, const optional &t) { if (t) { os << *t; } else { os << "None"; } return os; } /*! \brief parse a string object into optional * * \code * dmlc::optional x; * std::string s1 = "1"; * std::istringstream is1(s1); * s1 >> x; // x == optional(1) * * std::string s2 = "None"; * std::istringstream is2(s2); * s2 >> x; // x == optional() * \endcode * * \param is input stream * \param t target optional object * \return input stream */ template std::istream &operator>>(std::istream &is, optional &t) { char buf[4]; std::streampos origin = is.tellg(); is.read(buf, 4); if (is.fail() || buf[0] != 'N' || buf[1] != 'o' || buf[2] != 'n' || buf[3] != 'e') { is.clear(); is.seekg(origin); T x; is >> x; t = x; if (std::is_integral::value && !is.eof() && is.peek() == 'L') { is.get(); } } else { t = nullopt; } return is; } /*! \brief specialization of '>>' istream parsing for optional * * Permits use of generic parameter FieldEntry class to create * FieldEntry> without explicit specialization. * * \code * dmlc::optional x; * std::string s1 = "true"; * std::istringstream is1(s1); * s1 >> x; // x == optional(true) * * std::string s2 = "None"; * std::istringstream is2(s2); * s2 >> x; // x == optional() * \endcode * * \param is input stream * \param t target optional object * \return input stream */ inline std::istream &operator>>(std::istream &is, optional &t) { // Discard initial whitespace while (isspace(is.peek())) { is.get(); } // Extract chars that might be valid into a separate string, stopping // on whitespace or other non-alphanumerics such as ",)]". std::string s; while (isalnum(is.peek())) { s.push_back(is.get()); } if (!is.fail()) { std::transform(s.begin(), s.end(), s.begin(), ::tolower); if (s == "1" || s == "true") { t = true; } else if (s == "0" || s == "false") { t = false; } else if (s == "none") { t = nullopt; } else { is.setstate(std::ios::failbit); } } return is; } /*! \brief description for optional int */ DMLC_DECLARE_TYPE_NAME(optional, "int or None"); /*! \brief description for optional bool */ DMLC_DECLARE_TYPE_NAME(optional, "boolean or None"); /*! \brief description for optional float */ DMLC_DECLARE_TYPE_NAME(optional, "float or None"); /*! \brief description for optional double */ DMLC_DECLARE_TYPE_NAME(optional, "double or None"); } // namespace dmlc namespace std { /*! \brief std hash function for optional */ template struct hash> { /*! * \brief returns hash of the optional value. * \param val value. * \return hash code. */ size_t operator()(const dmlc::optional &val) const { std::hash hash_bool; size_t res = hash_bool(val.has_value()); if (val.has_value()) { res = dmlc::HashCombine(res, val.value()); } return res; } }; } // namespace std #endif // DMLC_OPTIONAL_H_