]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/optional/detail/optional_reference_spec.hpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / boost / optional / detail / optional_reference_spec.hpp
1 // Copyright (C) 2015-2018 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>
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>
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>
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
126 optional() BOOST_NOEXCEPT : ptr_() {}
127 optional(none_t) BOOST_NOEXCEPT : ptr_() {}
128
129 template <class U>
130 explicit optional(const optional<U&>& rhs) BOOST_NOEXCEPT : ptr_(rhs.get_ptr()) {}
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>
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, bool>::type = true) BOOST_NOEXCEPT
136 : ptr_(boost::addressof(rhs)) {}
137
138 template <class U>
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, bool>::type = true) BOOST_NOEXCEPT
140 : ptr_(boost::addressof(rhs)) {}
141
142 optional& operator=(const optional& rhs) BOOST_NOEXCEPT { ptr_ = rhs.get_ptr(); return *this; }
143 template <class U>
144 optional& operator=(const optional<U&>& rhs) BOOST_NOEXCEPT { ptr_ = rhs.get_ptr(); return *this; }
145 optional& operator=(none_t) BOOST_NOEXCEPT { ptr_ = 0; return *this; }
146
147
148 void swap(optional& rhs) BOOST_NOEXCEPT { std::swap(ptr_, rhs.ptr_); }
149 T& get() const { BOOST_ASSERT(ptr_); return *ptr_; }
150
151 T* get_ptr() const BOOST_NOEXCEPT { return ptr_; }
152 T* operator->() const { BOOST_ASSERT(ptr_); return ptr_; }
153 T& operator*() const { BOOST_ASSERT(ptr_); return *ptr_; }
154
155 T& value() const
156 {
157 if (this->is_initialized())
158 return this->get();
159 else
160 throw_exception(bad_optional_access());
161 }
162
163 bool operator!() const BOOST_NOEXCEPT { return ptr_ == 0; }
164 BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT()
165
166 void reset() BOOST_NOEXCEPT { ptr_ = 0; }
167
168 bool is_initialized() const BOOST_NOEXCEPT { return ptr_ != 0; }
169 bool has_value() const BOOST_NOEXCEPT { return ptr_ != 0; }
170
171 template <typename F>
172 optional<typename boost::result_of<F(T&)>::type> map(F f) const
173 {
174 if (this->has_value())
175 return f(this->get());
176 else
177 return none;
178 }
179
180 template <typename F>
181 optional<typename optional_detail::optional_value_type<typename boost::result_of<F(T&)>::type>::type> flat_map(F f) const
182 {
183 if (this->has_value())
184 return f(get());
185 else
186 return none;
187 }
188
189 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
190
191 optional(T&& /* rhs */) BOOST_NOEXCEPT { detail::prevent_binding_rvalue<T&&>(); }
192
193 template <class R>
194 optional(R&& r, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::no_unboxing_cond<T, R>, bool>::type = true) BOOST_NOEXCEPT
195 : ptr_(boost::addressof(r)) { detail::prevent_binding_rvalue<R>(); }
196
197 template <class R>
198 optional(bool cond, R&& r, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<R>, bool>::type = true) BOOST_NOEXCEPT
199 : ptr_(cond ? boost::addressof(r) : 0) { detail::prevent_binding_rvalue<R>(); }
200
201 template <class R>
202 BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<R>, optional<T&>&>::type
203 operator=(R&& r) BOOST_NOEXCEPT { detail::prevent_binding_rvalue<R>(); ptr_ = boost::addressof(r); return *this; }
204
205 template <class R>
206 void emplace(R&& r, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<R>, bool>::type = true) BOOST_NOEXCEPT
207 { detail::prevent_binding_rvalue<R>(); ptr_ = boost::addressof(r); }
208
209 template <class R>
210 T& get_value_or(R&& r, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<R>, bool>::type = true) const BOOST_NOEXCEPT
211 { detail::prevent_binding_rvalue<R>(); return ptr_ ? *ptr_ : r; }
212
213 template <class R>
214 T& value_or(R&& r, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<R>, bool>::type = true) const BOOST_NOEXCEPT
215 { detail::prevent_binding_rvalue<R>(); return ptr_ ? *ptr_ : r; }
216
217 template <class R>
218 void reset(R&& r, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<R>, bool>::type = true) BOOST_NOEXCEPT
219 { detail::prevent_binding_rvalue<R>(); ptr_ = boost::addressof(r); }
220
221 template <class F>
222 T& value_or_eval(F f) const { return ptr_ ? *ptr_ : detail::forward_reference(f()); }
223
224 #else // BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
225
226
227 // the following two implement a 'conditionally explicit' constructor
228 template <class U>
229 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
230 : ptr_(boost::addressof(v)) { }
231
232 template <class U>
233 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
234 : ptr_(boost::addressof(v)) { }
235
236 template <class U>
237 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) {}
238
239 template <class U>
240 BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<U>, optional<T&>&>::type
241 operator=(U& v) BOOST_NOEXCEPT
242 {
243 detail::prevent_assignment_from_false_const_integral<U>();
244 ptr_ = boost::addressof(v); return *this;
245 }
246
247 template <class U>
248 void emplace(U& v, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<U>, bool>::type = true) BOOST_NOEXCEPT
249 { ptr_ = boost::addressof(v); }
250
251 template <class U>
252 T& get_value_or(U& v, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<U>, bool>::type = true) const BOOST_NOEXCEPT
253 { return ptr_ ? *ptr_ : v; }
254
255 template <class U>
256 T& value_or(U& v, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<U>, bool>::type = true) const BOOST_NOEXCEPT
257 { return ptr_ ? *ptr_ : v; }
258
259 template <class U>
260 void reset(U& v, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<U>, bool>::type = true) BOOST_NOEXCEPT
261 { ptr_ = boost::addressof(v); }
262
263 template <class F>
264 T& value_or_eval(F f) const { return ptr_ ? *ptr_ : f(); }
265
266 #endif // BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
267 };
268
269 template <class T>
270 void swap ( optional<T&>& x, optional<T&>& y) BOOST_NOEXCEPT
271 {
272 x.swap(y);
273 }
274
275 } // namespace boost
276
277 #endif // 1/0
278
279 #endif // header guard