Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-17 08:40:05

0001 // Copyright (C) 2015-2024 Andrzej Krzemienski.
0002 //
0003 // Use, modification, and distribution is subject to the Boost Software
0004 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
0005 // http://www.boost.org/LICENSE_1_0.txt)
0006 //
0007 // See http://www.boost.org/libs/optional for documentation.
0008 //
0009 // You are welcome to contact the author at:
0010 //  akrzemi1@gmail.com
0011 
0012 #ifndef BOOST_OPTIONAL_DETAIL_OPTIONAL_REFERENCE_SPEC_AJK_03OCT2015_HPP
0013 #define BOOST_OPTIONAL_DETAIL_OPTIONAL_REFERENCE_SPEC_AJK_03OCT2015_HPP
0014 
0015 #ifdef BOOST_OPTIONAL_CONFIG_NO_PROPER_ASSIGN_FROM_CONST_INT
0016 #include <boost/type_traits/is_integral.hpp>
0017 #include <boost/type_traits/is_const.hpp>
0018 #endif
0019 
0020 # if 1
0021 
0022 namespace boost {
0023 
0024 namespace detail {
0025 
0026 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
0027 
0028 template <class From>
0029 void prevent_binding_rvalue()
0030 {
0031 #ifndef BOOST_OPTIONAL_CONFIG_ALLOW_BINDING_TO_RVALUES
0032     static_assert(boost::is_lvalue_reference<From>::value,
0033                   "binding rvalue references to optional lvalue references is disallowed");
0034 #endif
0035 }
0036 
0037 template <class T>
0038 BOOST_DEDUCED_TYPENAME boost::remove_reference<T>::type& forward_reference(T&& r)
0039 {
0040     static_assert(boost::is_lvalue_reference<T>::value,
0041                   "binding rvalue references to optional lvalue references is disallowed");
0042     return optional_detail::forward<T>(r);
0043 }
0044 
0045 #endif // BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
0046 
0047 
0048 template <class T>
0049 struct is_const_integral
0050 {
0051   static const bool value = boost::is_const<T>::value && boost::is_integral<T>::value;
0052 };
0053 
0054 template <class T>
0055 struct is_const_integral_bad_for_conversion
0056 {
0057 #if (!defined BOOST_OPTIONAL_CONFIG_ALLOW_BINDING_TO_RVALUES) && (defined BOOST_OPTIONAL_CONFIG_NO_PROPER_CONVERT_FROM_CONST_INT)
0058   static const bool value = boost::is_const<T>::value && boost::is_integral<T>::value;
0059 #else
0060   static const bool value = false;
0061 #endif
0062 };
0063 
0064 template <class From>
0065 void prevent_assignment_from_false_const_integral()
0066 {
0067 #ifndef BOOST_OPTIONAL_CONFIG_ALLOW_BINDING_TO_RVALUES
0068 #ifdef BOOST_OPTIONAL_CONFIG_NO_PROPER_ASSIGN_FROM_CONST_INT
0069     // MSVC compiler without rvalue references: we need to disable the assignment from
0070     // const integral lvalue reference, as it may be an invalid temporary
0071     static_assert(!is_const_integral<From>::value,
0072                   "binding const lvalue references to integral types is disabled in this compiler");
0073 #endif
0074 #endif
0075 }
0076 
0077 
0078 template <class T>
0079 struct is_optional_
0080 {
0081   static const bool value = false;
0082 };
0083 
0084 template <class U>
0085 struct is_optional_< ::boost::optional<U> >
0086 {
0087   static const bool value = true;
0088 };
0089 
0090 template <class T>
0091 struct is_no_optional
0092 {
0093   static const bool value = !is_optional_<BOOST_DEDUCED_TYPENAME boost::decay<T>::type>::value;
0094 };
0095 
0096 
0097 template <class T, class U>
0098   struct is_same_decayed
0099   {
0100     static const bool value = ::boost::is_same<T, BOOST_DEDUCED_TYPENAME ::boost::remove_reference<U>::type>::value
0101                            || ::boost::is_same<T, const BOOST_DEDUCED_TYPENAME ::boost::remove_reference<U>::type>::value;
0102   };
0103 
0104 template <class T, class U>
0105 struct no_unboxing_cond
0106 {
0107   static const bool value = is_no_optional<U>::value && !is_same_decayed<T, U>::value;
0108 };
0109 
0110 
0111 } // namespace detail
0112 
0113 template <class T>
0114 class optional<T&> : public optional_detail::optional_tag
0115 {
0116     T* ptr_;
0117 
0118 public:
0119     typedef T& value_type;
0120     typedef T& reference_type;
0121     typedef T& reference_const_type;
0122     typedef T& rval_reference_type;
0123     typedef T* pointer_type;
0124     typedef T* pointer_const_type;
0125 
0126     optional() BOOST_NOEXCEPT : ptr_() {}
0127     optional(none_t) BOOST_NOEXCEPT : ptr_() {}
0128 
0129     template <class U>
0130         explicit optional(const optional<U&>& rhs) BOOST_NOEXCEPT : ptr_(rhs.get_ptr()) {}
0131     optional(const optional& rhs) BOOST_NOEXCEPT : ptr_(rhs.get_ptr()) {}
0132 
0133     // the following two implement a 'conditionally explicit' constructor: condition is a hack for buggy compilers with screwed conversion construction from const int
0134     template <class U>
0135       explicit optional(U& rhs, BOOST_DEDUCED_TYPENAME boost::enable_if_c<detail::is_same_decayed<T, U>::value && detail::is_const_integral_bad_for_conversion<U>::value, bool>::type = true) BOOST_NOEXCEPT
0136       : ptr_(boost::addressof(rhs)) {}
0137 
0138     template <class U>
0139       optional(U& rhs, BOOST_DEDUCED_TYPENAME boost::enable_if_c<detail::is_same_decayed<T, U>::value && !detail::is_const_integral_bad_for_conversion<U>::value, bool>::type = true) BOOST_NOEXCEPT
0140       : ptr_(boost::addressof(rhs)) {}
0141 
0142     optional& operator=(const optional& rhs) BOOST_NOEXCEPT { ptr_ = rhs.get_ptr(); return *this; }
0143     template <class U>
0144         optional& operator=(const optional<U&>& rhs) BOOST_NOEXCEPT { ptr_ = rhs.get_ptr(); return *this; }
0145     optional& operator=(none_t) BOOST_NOEXCEPT { ptr_ = 0; return *this; }
0146 
0147 
0148     void swap(optional& rhs) BOOST_NOEXCEPT { std::swap(ptr_, rhs.ptr_); }
0149     T& get() const { BOOST_ASSERT(ptr_); return   *ptr_; }
0150 
0151     T* get_ptr() const BOOST_NOEXCEPT { return ptr_; }
0152     T* operator->() const { BOOST_ASSERT(ptr_); return ptr_; }
0153     T& operator*() const { BOOST_ASSERT(ptr_); return *ptr_; }
0154 
0155     T& value() const
0156     {
0157       if (this->is_initialized())
0158         return this->get();
0159       else
0160         throw_exception(bad_optional_access());
0161     }
0162 
0163     explicit operator bool() const BOOST_NOEXCEPT { return ptr_ != 0; }
0164 
0165     void reset() BOOST_NOEXCEPT { ptr_ = 0; }
0166 
0167     bool is_initialized() const BOOST_NOEXCEPT { return ptr_ != 0; }
0168     bool has_value() const BOOST_NOEXCEPT { return ptr_ != 0; }
0169 
0170     template <typename F>
0171     optional<typename optional_detail::result_of<F, reference_const_type>::type>
0172     map(F f) const
0173     {
0174       if (this->has_value())
0175         return f(this->get());
0176       else
0177         return none;
0178     }
0179 
0180     template <typename F>
0181     optional<typename optional_detail::result_value_type<F, reference_const_type>::type>
0182     flat_map(F f) const
0183       {
0184         if (this->has_value())
0185           return f(get());
0186         else
0187           return none;
0188       }
0189 
0190 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
0191 
0192     optional(T&& /* rhs */) BOOST_NOEXCEPT { detail::prevent_binding_rvalue<T&&>(); }
0193 
0194     template <class R>
0195         optional(R&& r, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::no_unboxing_cond<T, R>, bool>::type = true) BOOST_NOEXCEPT
0196         : ptr_(boost::addressof(r)) { detail::prevent_binding_rvalue<R>(); }
0197 
0198     template <class R>
0199         optional(bool cond, R&& r, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<R>, bool>::type = true) BOOST_NOEXCEPT
0200         : ptr_(cond ? boost::addressof(r) : 0) { detail::prevent_binding_rvalue<R>(); }
0201 
0202     template <class R>
0203         BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<R>, optional<T&>&>::type
0204         operator=(R&& r) BOOST_NOEXCEPT { detail::prevent_binding_rvalue<R>(); ptr_ = boost::addressof(r); return *this; }
0205 
0206     template <class R>
0207         void emplace(R&& r, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<R>, bool>::type = true) BOOST_NOEXCEPT
0208         { detail::prevent_binding_rvalue<R>(); ptr_ = boost::addressof(r); }
0209 
0210     template <class R>
0211       T& get_value_or(R&& r, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<R>, bool>::type = true) const BOOST_NOEXCEPT
0212       { detail::prevent_binding_rvalue<R>(); return ptr_ ? *ptr_ : r; }
0213 
0214     template <class R>
0215         T& value_or(R&& r, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<R>, bool>::type = true) const BOOST_NOEXCEPT
0216         { detail::prevent_binding_rvalue<R>(); return ptr_ ? *ptr_ : r; }
0217 
0218     template <class R>
0219       void reset(R&& r, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<R>, bool>::type = true) BOOST_NOEXCEPT
0220       { detail::prevent_binding_rvalue<R>(); ptr_ = boost::addressof(r); }
0221 
0222     template <class F>
0223         T& value_or_eval(F f) const { return ptr_ ? *ptr_ : detail::forward_reference(f()); }
0224 
0225 #else  // BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
0226 
0227 
0228     // the following two implement a 'conditionally explicit' constructor
0229     template <class U>
0230       explicit optional(U& v, BOOST_DEDUCED_TYPENAME boost::enable_if_c<detail::no_unboxing_cond<T, U>::value && detail::is_const_integral_bad_for_conversion<U>::value, bool>::type = true) BOOST_NOEXCEPT
0231       : ptr_(boost::addressof(v)) { }
0232 
0233     template <class U>
0234       optional(U& v, BOOST_DEDUCED_TYPENAME boost::enable_if_c<detail::no_unboxing_cond<T, U>::value && !detail::is_const_integral_bad_for_conversion<U>::value, bool>::type = true) BOOST_NOEXCEPT
0235       : ptr_(boost::addressof(v)) { }
0236 
0237     template <class U>
0238       optional(bool cond, U& v, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<U>, bool>::type = true) BOOST_NOEXCEPT : ptr_(cond ? boost::addressof(v) : 0) {}
0239 
0240     template <class U>
0241       BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<U>, optional<T&>&>::type
0242       operator=(U& v) BOOST_NOEXCEPT
0243       {
0244         detail::prevent_assignment_from_false_const_integral<U>();
0245         ptr_ = boost::addressof(v); return *this;
0246       }
0247 
0248     template <class U>
0249         void emplace(U& v, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<U>, bool>::type = true) BOOST_NOEXCEPT
0250         { ptr_ = boost::addressof(v); }
0251 
0252     template <class U>
0253       T& get_value_or(U& v, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<U>, bool>::type = true) const BOOST_NOEXCEPT
0254       { return ptr_ ? *ptr_ : v; }
0255 
0256     template <class U>
0257         T& value_or(U& v, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<U>, bool>::type = true) const BOOST_NOEXCEPT
0258         { return ptr_ ? *ptr_ : v; }
0259 
0260     template <class U>
0261       void reset(U& v, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<U>, bool>::type = true) BOOST_NOEXCEPT
0262       { ptr_ = boost::addressof(v); }
0263 
0264     template <class F>
0265       T& value_or_eval(F f) const { return ptr_ ? *ptr_ : f(); }
0266 
0267 #endif // BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
0268 };
0269 
0270 template <class T>
0271   void swap ( optional<T&>& x, optional<T&>& y) BOOST_NOEXCEPT
0272 {
0273   x.swap(y);
0274 }
0275 
0276 } // namespace boost
0277 
0278 #endif // 1/0
0279 
0280 #endif // header guard