]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/variant/include/boost/variant/get.hpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / variant / include / boost / variant / get.hpp
1 //-----------------------------------------------------------------------------
2 // boost variant/get.hpp header file
3 // See http://www.boost.org for updates, documentation, and revision history.
4 //-----------------------------------------------------------------------------
5 //
6 // Copyright (c) 2003 Eric Friedman, Itay Maman
7 // Copyright (c) 2014 Antony Polukhin
8 //
9 // Distributed under the Boost Software License, Version 1.0. (See
10 // accompanying file LICENSE_1_0.txt or copy at
11 // http://www.boost.org/LICENSE_1_0.txt)
12
13 #ifndef BOOST_VARIANT_GET_HPP
14 #define BOOST_VARIANT_GET_HPP
15
16 #include <exception>
17
18 #include <boost/config.hpp>
19 #include <boost/detail/workaround.hpp>
20 #include <boost/static_assert.hpp>
21 #include <boost/throw_exception.hpp>
22 #include <boost/utility/addressof.hpp>
23 #include <boost/variant/variant_fwd.hpp>
24 #include <boost/variant/detail/element_index.hpp>
25
26 #include <boost/type_traits/add_reference.hpp>
27 #include <boost/type_traits/add_pointer.hpp>
28
29 namespace boost {
30
31 #if defined(BOOST_CLANG)
32 # pragma clang diagnostic push
33 # pragma clang diagnostic ignored "-Wweak-vtables"
34 #endif
35 //////////////////////////////////////////////////////////////////////////
36 // class bad_get
37 //
38 // The exception thrown in the event of a failed get of a value.
39 //
40 class BOOST_SYMBOL_VISIBLE bad_get
41 : public std::exception
42 {
43 public: // std::exception implementation
44
45 virtual const char * what() const BOOST_NOEXCEPT_OR_NOTHROW
46 {
47 return "boost::bad_get: "
48 "failed value get using boost::get";
49 }
50
51 };
52 #if defined(BOOST_CLANG)
53 # pragma clang diagnostic pop
54 #endif
55
56
57 //////////////////////////////////////////////////////////////////////////
58 // function template get<T>
59 //
60 // Retrieves content of given variant object if content is of type T.
61 // Otherwise: pointer ver. returns 0; reference ver. throws bad_get.
62 //
63
64 namespace detail { namespace variant {
65
66 // (detail) class template get_visitor
67 //
68 // Generic static visitor that: if the value is of the specified type,
69 // returns a pointer to the value it visits; else a null pointer.
70 //
71 template <typename T>
72 struct get_visitor
73 {
74 private: // private typedefs
75
76 typedef typename add_pointer<T>::type pointer;
77 typedef typename add_reference<T>::type reference;
78
79 public: // visitor typedefs
80
81 typedef pointer result_type;
82
83 public: // visitor interfaces
84
85 pointer operator()(reference operand) const BOOST_NOEXCEPT
86 {
87 return boost::addressof(operand);
88 }
89
90 template <typename U>
91 pointer operator()(const U&) const BOOST_NOEXCEPT
92 {
93 return static_cast<pointer>(0);
94 }
95 };
96
97 }} // namespace detail::variant
98
99 #ifndef BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE
100 # if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x0551))
101 # define BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(t)
102 # else
103 # define BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(t) \
104 , t* = 0
105 # endif
106 #endif
107
108 /////////////////////////////////////////////////////////////////////////////////////////////////////////////
109 // relaxed_get<U>(variant) methods
110 //
111 template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
112 inline
113 typename add_pointer<U>::type
114 relaxed_get(
115 boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >* operand
116 BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
117 ) BOOST_NOEXCEPT
118 {
119 typedef typename add_pointer<U>::type U_ptr;
120 if (!operand) return static_cast<U_ptr>(0);
121
122 detail::variant::get_visitor<U> v;
123 return operand->apply_visitor(v);
124 }
125
126 template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
127 inline
128 typename add_pointer<const U>::type
129 relaxed_get(
130 const boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >* operand
131 BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
132 ) BOOST_NOEXCEPT
133 {
134 typedef typename add_pointer<const U>::type U_ptr;
135 if (!operand) return static_cast<U_ptr>(0);
136
137 detail::variant::get_visitor<const U> v;
138 return operand->apply_visitor(v);
139 }
140
141 template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
142 inline
143 typename add_reference<U>::type
144 relaxed_get(
145 boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >& operand
146 BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
147 )
148 {
149 typedef typename add_pointer<U>::type U_ptr;
150 U_ptr result = relaxed_get<U>(boost::addressof(operand));
151
152 if (!result)
153 boost::throw_exception(bad_get());
154 return *result;
155 }
156
157 template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
158 inline
159 typename add_reference<const U>::type
160 relaxed_get(
161 const boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >& operand
162 BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
163 )
164 {
165 typedef typename add_pointer<const U>::type U_ptr;
166 U_ptr result = relaxed_get<const U>(boost::addressof(operand));
167
168 if (!result)
169 boost::throw_exception(bad_get());
170 return *result;
171 }
172
173
174
175 /////////////////////////////////////////////////////////////////////////////////////////////////////////////
176 // strict_get<U>(variant) methods
177 //
178 template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
179 inline
180 typename add_pointer<U>::type
181 strict_get(
182 boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >* operand
183 BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
184 ) BOOST_NOEXCEPT
185 {
186 BOOST_STATIC_ASSERT_MSG(
187 (boost::detail::variant::holds_element<boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >, U >::value),
188 "boost::variant does not contain specified type U, "
189 "call to boost::get<U>(boost::variant<T...>*) will always return NULL"
190 );
191
192 return relaxed_get<U>(operand);
193 }
194
195 template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
196 inline
197 typename add_pointer<const U>::type
198 strict_get(
199 const boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >* operand
200 BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
201 ) BOOST_NOEXCEPT
202 {
203 BOOST_STATIC_ASSERT_MSG(
204 (boost::detail::variant::holds_element<boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >, const U >::value),
205 "boost::variant does not contain specified type U, "
206 "call to boost::get<U>(const boost::variant<T...>*) will always return NULL"
207 );
208
209 return relaxed_get<U>(operand);
210 }
211
212 template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
213 inline
214 typename add_reference<U>::type
215 strict_get(
216 boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >& operand
217 BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
218 )
219 {
220 BOOST_STATIC_ASSERT_MSG(
221 (boost::detail::variant::holds_element<boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >, U >::value),
222 "boost::variant does not contain specified type U, "
223 "call to boost::get<U>(boost::variant<T...>&) will always throw boost::bad_get exception"
224 );
225
226 return relaxed_get<U>(operand);
227 }
228
229 template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
230 inline
231 typename add_reference<const U>::type
232 strict_get(
233 const boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >& operand
234 BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
235 )
236 {
237 BOOST_STATIC_ASSERT_MSG(
238 (boost::detail::variant::holds_element<boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >, const U >::value),
239 "boost::variant does not contain specified type U, "
240 "call to boost::get<U>(const boost::variant<T...>&) will always throw boost::bad_get exception"
241 );
242
243 return relaxed_get<U>(operand);
244 }
245
246 /////////////////////////////////////////////////////////////////////////////////////////////////////////////
247 // get<U>(variant) methods
248 //
249
250 template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
251 inline
252 typename add_pointer<U>::type
253 get(
254 boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >* operand
255 BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
256 ) BOOST_NOEXCEPT
257 {
258 #ifdef BOOST_VARIANT_USE_RELAXED_GET_BY_DEFAULT
259 return relaxed_get<U>(operand);
260 #else
261 return strict_get<U>(operand);
262 #endif
263
264 }
265
266 template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
267 inline
268 typename add_pointer<const U>::type
269 get(
270 const boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >* operand
271 BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
272 ) BOOST_NOEXCEPT
273 {
274 #ifdef BOOST_VARIANT_USE_RELAXED_GET_BY_DEFAULT
275 return relaxed_get<U>(operand);
276 #else
277 return strict_get<U>(operand);
278 #endif
279 }
280
281 template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
282 inline
283 typename add_reference<U>::type
284 get(
285 boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >& operand
286 BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
287 )
288 {
289 #ifdef BOOST_VARIANT_USE_RELAXED_GET_BY_DEFAULT
290 return relaxed_get<U>(operand);
291 #else
292 return strict_get<U>(operand);
293 #endif
294 }
295
296 template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
297 inline
298 typename add_reference<const U>::type
299 get(
300 const boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >& operand
301 BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
302 )
303 {
304 #ifdef BOOST_VARIANT_USE_RELAXED_GET_BY_DEFAULT
305 return relaxed_get<U>(operand);
306 #else
307 return strict_get<U>(operand);
308 #endif
309 }
310
311 } // namespace boost
312
313 #endif // BOOST_VARIANT_GET_HPP