Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:43:20

0001 // Copyright (C) 2015-2018 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     BOOST_STATIC_ASSERT_MSG(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     BOOST_STATIC_ASSERT_MSG(boost::is_lvalue_reference<T>::value, 
0041                             "binding rvalue references to optional lvalue references is disallowed");
0042     return boost::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     BOOST_STATIC_ASSERT_MSG(!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     bool operator!() const BOOST_NOEXCEPT { return ptr_ == 0; }  
0164     BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT()
0165       
0166     void reset() BOOST_NOEXCEPT { ptr_ = 0; }
0167 
0168     bool is_initialized() const BOOST_NOEXCEPT { return ptr_ != 0; }
0169     bool has_value() const BOOST_NOEXCEPT { return ptr_ != 0; }
0170     
0171     template <typename F>
0172     optional<typename boost::result_of<F(T&)>::type> 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::optional_value_type<typename boost::result_of<F(T&)>::type>::type> flat_map(F f) const
0182       {
0183         if (this->has_value())
0184           return f(get());
0185         else
0186           return none;
0187       }
0188     
0189 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES   
0190  
0191     optional(T&& /* rhs */) BOOST_NOEXCEPT { detail::prevent_binding_rvalue<T&&>(); }
0192     
0193     template <class R>
0194         optional(R&& r, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::no_unboxing_cond<T, R>, bool>::type = true) BOOST_NOEXCEPT
0195         : ptr_(boost::addressof(r)) { detail::prevent_binding_rvalue<R>(); }
0196         
0197     template <class R>
0198         optional(bool cond, R&& r, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<R>, bool>::type = true) BOOST_NOEXCEPT
0199         : ptr_(cond ? boost::addressof(r) : 0) { detail::prevent_binding_rvalue<R>(); }
0200         
0201     template <class R>
0202         BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<R>, optional<T&>&>::type
0203         operator=(R&& r) BOOST_NOEXCEPT { detail::prevent_binding_rvalue<R>(); ptr_ = boost::addressof(r); return *this; }
0204         
0205     template <class R>
0206         void emplace(R&& r, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<R>, bool>::type = true) BOOST_NOEXCEPT
0207         { detail::prevent_binding_rvalue<R>(); ptr_ = boost::addressof(r); }
0208         
0209     template <class R>
0210       T& get_value_or(R&& r, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<R>, bool>::type = true) const BOOST_NOEXCEPT
0211       { detail::prevent_binding_rvalue<R>(); return ptr_ ? *ptr_ : r; }
0212       
0213     template <class R>
0214         T& value_or(R&& r, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<R>, bool>::type = true) const BOOST_NOEXCEPT
0215         { detail::prevent_binding_rvalue<R>(); return ptr_ ? *ptr_ : r; }
0216         
0217     template <class R>
0218       void reset(R&& r, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<R>, bool>::type = true) BOOST_NOEXCEPT
0219       { detail::prevent_binding_rvalue<R>(); ptr_ = boost::addressof(r); }
0220       
0221     template <class F>
0222         T& value_or_eval(F f) const { return ptr_ ? *ptr_ : detail::forward_reference(f()); }
0223       
0224 #else  // BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
0225 
0226     
0227     // the following two implement a 'conditionally explicit' constructor
0228     template <class U>
0229       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
0230       : ptr_(boost::addressof(v)) { }
0231       
0232     template <class U>
0233       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
0234       : ptr_(boost::addressof(v)) { }
0235         
0236     template <class U>
0237       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) {}
0238 
0239     template <class U>
0240       BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<U>, optional<T&>&>::type
0241       operator=(U& v) BOOST_NOEXCEPT
0242       {
0243         detail::prevent_assignment_from_false_const_integral<U>();
0244         ptr_ = boost::addressof(v); return *this;
0245       }
0246 
0247     template <class U>
0248         void emplace(U& v, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<U>, bool>::type = true) BOOST_NOEXCEPT
0249         { ptr_ = boost::addressof(v); }
0250         
0251     template <class U>
0252       T& get_value_or(U& v, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<U>, bool>::type = true) const BOOST_NOEXCEPT
0253       { return ptr_ ? *ptr_ : v; }
0254       
0255     template <class U>
0256         T& value_or(U& v, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<U>, bool>::type = true) const BOOST_NOEXCEPT
0257         { return ptr_ ? *ptr_ : v; }
0258         
0259     template <class U>
0260       void reset(U& v, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<U>, bool>::type = true) BOOST_NOEXCEPT
0261       { ptr_ = boost::addressof(v); }
0262       
0263     template <class F>
0264       T& value_or_eval(F f) const { return ptr_ ? *ptr_ : f(); }
0265       
0266 #endif // BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
0267 };
0268 
0269 template <class T> 
0270   void swap ( optional<T&>& x, optional<T&>& y) BOOST_NOEXCEPT
0271 {
0272   x.swap(y);
0273 }
0274 
0275 } // namespace boost
0276 
0277 #endif // 1/0
0278 
0279 #endif // header guard