1 // Copyright (C) 2015-2016 Andrzej Krzemienski.
2 //
3 // Use, modification, and distribution is subject to the Boost Software
4 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // See http://www.boost.org/libs/optional for documentation.
8 //
9 // You are welcome to contact the author at:
10 //  akrzemi1@gmail.com
11 
12 #ifndef BOOST_OPTIONAL_DETAIL_OPTIONAL_REFERENCE_SPEC_AJK_03OCT2015_HPP
13 #define BOOST_OPTIONAL_DETAIL_OPTIONAL_REFERENCE_SPEC_AJK_03OCT2015_HPP
14 
15 #ifdef BOOST_OPTIONAL_CONFIG_NO_PROPER_ASSIGN_FROM_CONST_INT
16 #include <boost/type_traits/is_integral.hpp>
17 #include <boost/type_traits/is_const.hpp>
18 #endif
19 
20 # if 1
21 
22 namespace boost {
23 
24 namespace detail {
25 
26 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
27 
28 template <class From>
prevent_binding_rvalue()29 void prevent_binding_rvalue()
30 {
31 #ifndef BOOST_OPTIONAL_CONFIG_ALLOW_BINDING_TO_RVALUES
32     BOOST_STATIC_ASSERT_MSG(boost::is_lvalue_reference<From>::value,
33                             "binding rvalue references to optional lvalue references is disallowed");
34 #endif
35 }
36 
37 template <class T>
forward_reference(T && r)38 BOOST_DEDUCED_TYPENAME boost::remove_reference<T>::type& forward_reference(T&& r)
39 {
40     BOOST_STATIC_ASSERT_MSG(boost::is_lvalue_reference<T>::value,
41                             "binding rvalue references to optional lvalue references is disallowed");
42     return boost::forward<T>(r);
43 }
44 
45 #endif // BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
46 
47 
48 template <class T>
49 struct is_const_integral
50 {
51   static const bool value = boost::is_const<T>::value && boost::is_integral<T>::value;
52 };
53 
54 template <class T>
55 struct is_const_integral_bad_for_conversion
56 {
57 #if (!defined BOOST_OPTIONAL_CONFIG_ALLOW_BINDING_TO_RVALUES) && (defined BOOST_OPTIONAL_CONFIG_NO_PROPER_CONVERT_FROM_CONST_INT)
58   static const bool value = boost::is_const<T>::value && boost::is_integral<T>::value;
59 #else
60   static const bool value = false;
61 #endif
62 };
63 
64 template <class From>
prevent_assignment_from_false_const_integral()65 void prevent_assignment_from_false_const_integral()
66 {
67 #ifndef BOOST_OPTIONAL_CONFIG_ALLOW_BINDING_TO_RVALUES
68 #ifdef BOOST_OPTIONAL_CONFIG_NO_PROPER_ASSIGN_FROM_CONST_INT
69     // MSVC compiler without rvalue refernces: we need to disable the asignment from
70     // const integral lvalue reference, as it may be an invalid temporary
71     BOOST_STATIC_ASSERT_MSG(!is_const_integral<From>::value,
72                             "binding const lvalue references to integral types is disabled in this compiler");
73 #endif
74 #endif
75 }
76 
77 
78 template <class T>
79 struct is_optional_
80 {
81   static const bool value = false;
82 };
83 
84 template <class U>
85 struct is_optional_< ::boost::optional<U> >
86 {
87   static const bool value = true;
88 };
89 
90 template <class T>
91 struct is_no_optional
92 {
93   static const bool value = !is_optional_<BOOST_DEDUCED_TYPENAME boost::decay<T>::type>::value;
94 };
95 
96 
97 template <class T, class U>
98   struct is_same_decayed
99   {
100     static const bool value = ::boost::is_same<T, BOOST_DEDUCED_TYPENAME ::boost::remove_reference<U>::type>::value
101                            || ::boost::is_same<T, const BOOST_DEDUCED_TYPENAME ::boost::remove_reference<U>::type>::value;
102   };
103 
104 template <class T, class U>
105 struct no_unboxing_cond
106 {
107   static const bool value = is_no_optional<U>::value && !is_same_decayed<T, U>::value;
108 };
109 
110 
111 } // namespace detail
112 
113 template <class T>
114 class optional<T&> : public optional_detail::optional_tag
115 {
116     T* ptr_;
117 
118 public:
119     typedef T& value_type;
120     typedef T& reference_type;
121     typedef T& reference_const_type;
122     typedef T& rval_reference_type;
123     typedef T* pointer_type;
124     typedef T* pointer_const_type;
125 
optional()126     optional() BOOST_NOEXCEPT : ptr_() {}
optional(none_t)127     optional(none_t) BOOST_NOEXCEPT : ptr_() {}
128 
129     template <class U>
optional(const optional<U &> & rhs)130         explicit optional(const optional<U&>& rhs) BOOST_NOEXCEPT : ptr_(rhs.get_ptr()) {}
optional(const optional & rhs)131     optional(const optional& rhs) BOOST_NOEXCEPT : ptr_(rhs.get_ptr()) {}
132 
133     // the following two implement a 'conditionally explicit' constructor: condition is a hack for buggy compilers with srewed conversion construction from const int
134     template <class U>
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>::type * =0)135       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>::type* = 0) BOOST_NOEXCEPT
136       : ptr_(boost::addressof(rhs)) {}
137 
138     template <class U>
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>::type * =0)139       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>::type* = 0) BOOST_NOEXCEPT
140       : ptr_(boost::addressof(rhs)) {}
141 
operator =(const optional & rhs)142     optional& operator=(const optional& rhs) BOOST_NOEXCEPT { ptr_ = rhs.get_ptr(); return *this; }
143     template <class U>
operator =(const optional<U &> & rhs)144         optional& operator=(const optional<U&>& rhs) BOOST_NOEXCEPT { ptr_ = rhs.get_ptr(); return *this; }
operator =(none_t)145     optional& operator=(none_t) BOOST_NOEXCEPT { ptr_ = 0; return *this; }
146 
147 
swap(optional & rhs)148     void swap(optional& rhs) BOOST_NOEXCEPT { std::swap(ptr_, rhs.ptr_); }
get() const149     T& get() const { BOOST_ASSERT(ptr_); return   *ptr_; }
150 
get_ptr() const151     T* get_ptr() const BOOST_NOEXCEPT { return ptr_; }
operator ->() const152     T* operator->() const { BOOST_ASSERT(ptr_); return ptr_; }
operator *() const153     T& operator*() const { BOOST_ASSERT(ptr_); return *ptr_; }
value() const154     T& value() const { return ptr_ ? *ptr_ : (throw_exception(bad_optional_access()), *ptr_); }
155 
operator !() const156     bool operator!() const BOOST_NOEXCEPT { return ptr_ == 0; }
BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT()157     BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT()
158 
159     void reset() BOOST_NOEXCEPT { ptr_ = 0; }
160 
is_initialized() const161     bool is_initialized() const BOOST_NOEXCEPT { return ptr_ != 0; }
162 
163 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
164 
optional(T &&)165     optional(T&& /* rhs */) BOOST_NOEXCEPT { detail::prevent_binding_rvalue<T&&>(); }
166 
167     template <class R>
optional(R && r,BOOST_DEDUCED_TYPENAME boost::enable_if<detail::no_unboxing_cond<T,R>>::type * =0)168         optional(R&& r, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::no_unboxing_cond<T, R> >::type* = 0) BOOST_NOEXCEPT
169         : ptr_(boost::addressof(r)) { detail::prevent_binding_rvalue<R>(); }
170 
171     template <class R>
optional(bool cond,R && r,BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<R>>::type * =0)172         optional(bool cond, R&& r, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<R> >::type* = 0) BOOST_NOEXCEPT
173         : ptr_(cond ? boost::addressof(r) : 0) { detail::prevent_binding_rvalue<R>(); }
174 
175     template <class R>
176         BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<R>, optional<T&>&>::type
operator =(R && r)177         operator=(R&& r) BOOST_NOEXCEPT { detail::prevent_binding_rvalue<R>(); ptr_ = boost::addressof(r); return *this; }
178 
179     template <class R>
emplace(R && r,BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<R>>::type * =0)180         void emplace(R&& r, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<R> >::type* = 0) BOOST_NOEXCEPT
181         { detail::prevent_binding_rvalue<R>(); ptr_ = boost::addressof(r); }
182 
183     template <class R>
get_value_or(R && r,BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<R>>::type * =0) const184       T& get_value_or(R&& r, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<R> >::type* = 0) const BOOST_NOEXCEPT
185       { detail::prevent_binding_rvalue<R>(); return ptr_ ? *ptr_ : r; }
186 
187     template <class R>
value_or(R && r,BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<R>>::type * =0) const188         T& value_or(R&& r, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<R> >::type* = 0) const BOOST_NOEXCEPT
189         { detail::prevent_binding_rvalue<R>(); return ptr_ ? *ptr_ : r; }
190 
191     template <class R>
reset(R && r,BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<R>>::type * =0)192       void reset(R&& r, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<R> >::type* = 0) BOOST_NOEXCEPT
193       { detail::prevent_binding_rvalue<R>(); ptr_ = boost::addressof(r); }
194 
195     template <class F>
value_or_eval(F f) const196         T& value_or_eval(F f) const { return ptr_ ? *ptr_ : detail::forward_reference(f()); }
197 
198 #else  // BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
199 
200 
201     // the following two implement a 'conditionally explicit' constructor
202     template <class U>
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>::type * =0)203       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 >::type* = 0) BOOST_NOEXCEPT
204       : ptr_(boost::addressof(v)) { }
205 
206     template <class U>
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>::type * =0)207       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 >::type* = 0) BOOST_NOEXCEPT
208       : ptr_(boost::addressof(v)) { }
209 
210     template <class U>
optional(bool cond,U & v,BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<U>>::type * =0)211       optional(bool cond, U& v, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<U> >::type* = 0) BOOST_NOEXCEPT : ptr_(cond ? boost::addressof(v) : 0) {}
212 
213     template <class U>
214       BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<U>, optional<T&>&>::type
operator =(U & v)215       operator=(U& v) BOOST_NOEXCEPT
216       {
217         detail::prevent_assignment_from_false_const_integral<U>();
218         ptr_ = boost::addressof(v); return *this;
219       }
220 
221     template <class U>
emplace(U & v,BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<U>>::type * =0)222         void emplace(U& v, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<U> >::type* = 0) BOOST_NOEXCEPT
223         { ptr_ = boost::addressof(v); }
224 
225     template <class U>
get_value_or(U & v,BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<U>>::type * =0) const226       T& get_value_or(U& v, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<U> >::type* = 0) const BOOST_NOEXCEPT
227       { return ptr_ ? *ptr_ : v; }
228 
229     template <class U>
value_or(U & v,BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<U>>::type * =0) const230         T& value_or(U& v, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<U> >::type* = 0) const BOOST_NOEXCEPT
231         { return ptr_ ? *ptr_ : v; }
232 
233     template <class U>
reset(U & v,BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<U>>::type * =0)234       void reset(U& v, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<U> >::type* = 0) BOOST_NOEXCEPT
235       { ptr_ = boost::addressof(v); }
236 
237     template <class F>
value_or_eval(F f) const238       T& value_or_eval(F f) const { return ptr_ ? *ptr_ : f(); }
239 
240 #endif // BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
241 };
242 
243 template <class T>
swap(optional<T &> & x,optional<T &> & y)244   void swap ( optional<T&>& x, optional<T&>& y) BOOST_NOEXCEPT
245 {
246   x.swap(y);
247 }
248 
249 } // namespace boost
250 
251 #endif // 1/0
252 
253 #endif // header guard
254