// Copyright (C) 2015-2018 Andrzej Krzemienski. // // Use, modification, and distribution is subject to the Boost Software // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) // // See http://www.boost.org/libs/optional for documentation. // // You are welcome to contact the author at: // akrzemi1@gmail.com #ifndef BOOST_OPTIONAL_DETAIL_OPTIONAL_REFERENCE_SPEC_AJK_03OCT2015_HPP #define BOOST_OPTIONAL_DETAIL_OPTIONAL_REFERENCE_SPEC_AJK_03OCT2015_HPP #ifdef BOOST_OPTIONAL_CONFIG_NO_PROPER_ASSIGN_FROM_CONST_INT #include #include #endif # if 1 namespace boost { namespace detail { #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES template void prevent_binding_rvalue() { #ifndef BOOST_OPTIONAL_CONFIG_ALLOW_BINDING_TO_RVALUES BOOST_STATIC_ASSERT_MSG(boost::is_lvalue_reference::value, "binding rvalue references to optional lvalue references is disallowed"); #endif } template BOOST_DEDUCED_TYPENAME boost::remove_reference::type& forward_reference(T&& r) { BOOST_STATIC_ASSERT_MSG(boost::is_lvalue_reference::value, "binding rvalue references to optional lvalue references is disallowed"); return boost::forward(r); } #endif // BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES template struct is_const_integral { static const bool value = boost::is_const::value && boost::is_integral::value; }; template struct is_const_integral_bad_for_conversion { #if (!defined BOOST_OPTIONAL_CONFIG_ALLOW_BINDING_TO_RVALUES) && (defined BOOST_OPTIONAL_CONFIG_NO_PROPER_CONVERT_FROM_CONST_INT) static const bool value = boost::is_const::value && boost::is_integral::value; #else static const bool value = false; #endif }; template void prevent_assignment_from_false_const_integral() { #ifndef BOOST_OPTIONAL_CONFIG_ALLOW_BINDING_TO_RVALUES #ifdef BOOST_OPTIONAL_CONFIG_NO_PROPER_ASSIGN_FROM_CONST_INT // MSVC compiler without rvalue refernces: we need to disable the asignment from // const integral lvalue reference, as it may be an invalid temporary BOOST_STATIC_ASSERT_MSG(!is_const_integral::value, "binding const lvalue references to integral types is disabled in this compiler"); #endif #endif } template struct is_optional_ { static const bool value = false; }; template struct is_optional_< ::boost::optional > { static const bool value = true; }; template struct is_no_optional { static const bool value = !is_optional_::type>::value; }; template struct is_same_decayed { static const bool value = ::boost::is_same::type>::value || ::boost::is_same::type>::value; }; template struct no_unboxing_cond { static const bool value = is_no_optional::value && !is_same_decayed::value; }; } // namespace detail template class optional : public optional_detail::optional_tag { T* ptr_; public: typedef T& value_type; typedef T& reference_type; typedef T& reference_const_type; typedef T& rval_reference_type; typedef T* pointer_type; typedef T* pointer_const_type; optional() BOOST_NOEXCEPT : ptr_() {} optional(none_t) BOOST_NOEXCEPT : ptr_() {} template explicit optional(const optional& rhs) BOOST_NOEXCEPT : ptr_(rhs.get_ptr()) {} optional(const optional& rhs) BOOST_NOEXCEPT : ptr_(rhs.get_ptr()) {} // the following two implement a 'conditionally explicit' constructor: condition is a hack for buggy compilers with srewed conversion construction from const int template explicit optional(U& rhs, BOOST_DEDUCED_TYPENAME boost::enable_if_c::value && detail::is_const_integral_bad_for_conversion::value, bool>::type = true) BOOST_NOEXCEPT : ptr_(boost::addressof(rhs)) {} template optional(U& rhs, BOOST_DEDUCED_TYPENAME boost::enable_if_c::value && !detail::is_const_integral_bad_for_conversion::value, bool>::type = true) BOOST_NOEXCEPT : ptr_(boost::addressof(rhs)) {} optional& operator=(const optional& rhs) BOOST_NOEXCEPT { ptr_ = rhs.get_ptr(); return *this; } template optional& operator=(const optional& rhs) BOOST_NOEXCEPT { ptr_ = rhs.get_ptr(); return *this; } optional& operator=(none_t) BOOST_NOEXCEPT { ptr_ = 0; return *this; } void swap(optional& rhs) BOOST_NOEXCEPT { std::swap(ptr_, rhs.ptr_); } T& get() const { BOOST_ASSERT(ptr_); return *ptr_; } T* get_ptr() const BOOST_NOEXCEPT { return ptr_; } T* operator->() const { BOOST_ASSERT(ptr_); return ptr_; } T& operator*() const { BOOST_ASSERT(ptr_); return *ptr_; } T& value() const { return ptr_ ? *ptr_ : (throw_exception(bad_optional_access()), *ptr_); } bool operator!() const BOOST_NOEXCEPT { return ptr_ == 0; } BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT() void reset() BOOST_NOEXCEPT { ptr_ = 0; } bool is_initialized() const BOOST_NOEXCEPT { return ptr_ != 0; } bool has_value() const BOOST_NOEXCEPT { return ptr_ != 0; } template optional::type> map(F f) const { if (this->has_value()) return f(this->get()); else return none; } template optional::type>::type> flat_map(F f) const { if (this->has_value()) return f(get()); else return none; } #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES optional(T&& /* rhs */) BOOST_NOEXCEPT { detail::prevent_binding_rvalue(); } template optional(R&& r, BOOST_DEDUCED_TYPENAME boost::enable_if, bool>::type = true) BOOST_NOEXCEPT : ptr_(boost::addressof(r)) { detail::prevent_binding_rvalue(); } template optional(bool cond, R&& r, BOOST_DEDUCED_TYPENAME boost::enable_if, bool>::type = true) BOOST_NOEXCEPT : ptr_(cond ? boost::addressof(r) : 0) { detail::prevent_binding_rvalue(); } template BOOST_DEDUCED_TYPENAME boost::enable_if, optional&>::type operator=(R&& r) BOOST_NOEXCEPT { detail::prevent_binding_rvalue(); ptr_ = boost::addressof(r); return *this; } template void emplace(R&& r, BOOST_DEDUCED_TYPENAME boost::enable_if, bool>::type = true) BOOST_NOEXCEPT { detail::prevent_binding_rvalue(); ptr_ = boost::addressof(r); } template T& get_value_or(R&& r, BOOST_DEDUCED_TYPENAME boost::enable_if, bool>::type = true) const BOOST_NOEXCEPT { detail::prevent_binding_rvalue(); return ptr_ ? *ptr_ : r; } template T& value_or(R&& r, BOOST_DEDUCED_TYPENAME boost::enable_if, bool>::type = true) const BOOST_NOEXCEPT { detail::prevent_binding_rvalue(); return ptr_ ? *ptr_ : r; } template void reset(R&& r, BOOST_DEDUCED_TYPENAME boost::enable_if, bool>::type = true) BOOST_NOEXCEPT { detail::prevent_binding_rvalue(); ptr_ = boost::addressof(r); } template T& value_or_eval(F f) const { return ptr_ ? *ptr_ : detail::forward_reference(f()); } #else // BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES // the following two implement a 'conditionally explicit' constructor template explicit optional(U& v, BOOST_DEDUCED_TYPENAME boost::enable_if_c::value && detail::is_const_integral_bad_for_conversion::value, bool>::type = true) BOOST_NOEXCEPT : ptr_(boost::addressof(v)) { } template optional(U& v, BOOST_DEDUCED_TYPENAME boost::enable_if_c::value && !detail::is_const_integral_bad_for_conversion::value, bool>::type = true) BOOST_NOEXCEPT : ptr_(boost::addressof(v)) { } template optional(bool cond, U& v, BOOST_DEDUCED_TYPENAME boost::enable_if, bool>::type = true) BOOST_NOEXCEPT : ptr_(cond ? boost::addressof(v) : 0) {} template BOOST_DEDUCED_TYPENAME boost::enable_if, optional&>::type operator=(U& v) BOOST_NOEXCEPT { detail::prevent_assignment_from_false_const_integral(); ptr_ = boost::addressof(v); return *this; } template void emplace(U& v, BOOST_DEDUCED_TYPENAME boost::enable_if, bool>::type = true) BOOST_NOEXCEPT { ptr_ = boost::addressof(v); } template T& get_value_or(U& v, BOOST_DEDUCED_TYPENAME boost::enable_if, bool>::type = true) const BOOST_NOEXCEPT { return ptr_ ? *ptr_ : v; } template T& value_or(U& v, BOOST_DEDUCED_TYPENAME boost::enable_if, bool>::type = true) const BOOST_NOEXCEPT { return ptr_ ? *ptr_ : v; } template void reset(U& v, BOOST_DEDUCED_TYPENAME boost::enable_if, bool>::type = true) BOOST_NOEXCEPT { ptr_ = boost::addressof(v); } template T& value_or_eval(F f) const { return ptr_ ? *ptr_ : f(); } #endif // BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES }; template void swap ( optional& x, optional& y) BOOST_NOEXCEPT { x.swap(y); } } // namespace boost #endif // 1/0 #endif // header guard