get.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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-2024 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. #ifndef BOOST_VARIANT_GET_HPP
  13. #define BOOST_VARIANT_GET_HPP
  14. #include <exception>
  15. #include <boost/config.hpp>
  16. #include <boost/core/addressof.hpp>
  17. #include <boost/detail/workaround.hpp>
  18. #include <boost/static_assert.hpp>
  19. #include <boost/throw_exception.hpp>
  20. #include <boost/variant/variant_fwd.hpp>
  21. #include <boost/variant/detail/element_index.hpp>
  22. #include <boost/variant/detail/move.hpp>
  23. #include <boost/type_traits/add_reference.hpp>
  24. #include <boost/type_traits/add_pointer.hpp>
  25. #include <boost/type_traits/is_lvalue_reference.hpp>
  26. namespace boost {
  27. #if defined(BOOST_CLANG)
  28. # pragma clang diagnostic push
  29. # pragma clang diagnostic ignored "-Wweak-vtables"
  30. #endif
  31. //////////////////////////////////////////////////////////////////////////
  32. // class bad_get
  33. //
  34. // The exception thrown in the event of a failed get of a value.
  35. //
  36. class BOOST_SYMBOL_VISIBLE bad_get
  37. : public std::exception
  38. {
  39. public: // std::exception implementation
  40. const char * what() const BOOST_NOEXCEPT_OR_NOTHROW BOOST_OVERRIDE
  41. {
  42. return "boost::bad_get: "
  43. "failed value get using boost::get";
  44. }
  45. };
  46. #if defined(BOOST_CLANG)
  47. # pragma clang diagnostic pop
  48. #endif
  49. //////////////////////////////////////////////////////////////////////////
  50. // function template get<T>
  51. //
  52. // Retrieves content of given variant object if content is of type T.
  53. // Otherwise: pointer ver. returns 0; reference ver. throws bad_get.
  54. //
  55. namespace detail { namespace variant {
  56. // (detail) class template get_visitor
  57. //
  58. // Generic static visitor that: if the value is of the specified type,
  59. // returns a pointer to the value it visits; else a null pointer.
  60. //
  61. template <typename T>
  62. struct get_visitor
  63. {
  64. private: // private typedefs
  65. typedef typename add_pointer<T>::type pointer;
  66. typedef typename add_reference<T>::type reference;
  67. public: // visitor typedefs
  68. typedef pointer result_type;
  69. public: // visitor interfaces
  70. pointer operator()(reference operand) const BOOST_NOEXCEPT
  71. {
  72. return boost::addressof(operand);
  73. }
  74. template <typename U>
  75. pointer operator()(const U&) const BOOST_NOEXCEPT
  76. {
  77. return static_cast<pointer>(0);
  78. }
  79. };
  80. }} // namespace detail::variant
  81. #ifndef BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE
  82. # if !BOOST_WORKAROUND(BOOST_BORLANDC, BOOST_TESTED_AT(0x0551))
  83. # define BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(t)
  84. # else
  85. # if defined(BOOST_NO_CXX11_NULLPTR)
  86. # define BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(t) \
  87. , t* = 0
  88. # else
  89. # define BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(t) \
  90. , t* = nullptr
  91. # endif
  92. # endif
  93. #endif
  94. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  95. // relaxed_get<U>(variant) methods
  96. //
  97. template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
  98. inline
  99. typename add_pointer<U>::type
  100. relaxed_get(
  101. boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >* operand
  102. BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
  103. ) BOOST_NOEXCEPT
  104. {
  105. typedef typename add_pointer<U>::type U_ptr;
  106. if (!operand) return static_cast<U_ptr>(0);
  107. detail::variant::get_visitor<U> v;
  108. return operand->apply_visitor(v);
  109. }
  110. template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
  111. inline
  112. typename add_pointer<const U>::type
  113. relaxed_get(
  114. const boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >* operand
  115. BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
  116. ) BOOST_NOEXCEPT
  117. {
  118. typedef typename add_pointer<const U>::type U_ptr;
  119. if (!operand) return static_cast<U_ptr>(0);
  120. detail::variant::get_visitor<const U> v;
  121. return operand->apply_visitor(v);
  122. }
  123. template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
  124. inline
  125. typename add_reference<U>::type
  126. relaxed_get(
  127. boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >& operand
  128. BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
  129. )
  130. {
  131. typedef typename add_pointer<U>::type U_ptr;
  132. U_ptr result = relaxed_get<U>(boost::addressof(operand));
  133. if (!result)
  134. boost::throw_exception(bad_get());
  135. return *result;
  136. }
  137. template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
  138. inline
  139. typename add_reference<const U>::type
  140. relaxed_get(
  141. const boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >& operand
  142. BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
  143. )
  144. {
  145. typedef typename add_pointer<const U>::type U_ptr;
  146. U_ptr result = relaxed_get<const U>(boost::addressof(operand));
  147. if (!result)
  148. boost::throw_exception(bad_get());
  149. return *result;
  150. }
  151. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  152. #if defined(BOOST_MSVC) && (_MSC_VER < 1900) // MSVC-2014 has fixed the incorrect diagnostics.
  153. # pragma warning(push)
  154. # pragma warning(disable: 4172) // returning address of local variable or temporary
  155. #endif
  156. template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
  157. inline
  158. U&&
  159. relaxed_get(
  160. boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >&& operand
  161. BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
  162. )
  163. {
  164. typedef typename add_pointer<U>::type U_ptr;
  165. U_ptr result = relaxed_get<U>(boost::addressof(operand));
  166. if (!result)
  167. boost::throw_exception(bad_get());
  168. return static_cast<U&&>(*result);
  169. }
  170. #if defined(BOOST_MSVC) && (_MSC_VER < 1900)
  171. # pragma warning(pop)
  172. #endif
  173. #endif
  174. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  175. // strict_get<U>(variant) methods
  176. //
  177. template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
  178. inline
  179. typename add_pointer<U>::type
  180. strict_get(
  181. boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >* operand
  182. BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
  183. ) BOOST_NOEXCEPT
  184. {
  185. BOOST_STATIC_ASSERT_MSG(
  186. (boost::detail::variant::holds_element<boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >, U >::value),
  187. "boost::variant does not contain specified type U, "
  188. "call to boost::get<U>(boost::variant<T...>*) will always return NULL"
  189. );
  190. return relaxed_get<U>(operand);
  191. }
  192. template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
  193. inline
  194. typename add_pointer<const U>::type
  195. strict_get(
  196. const boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >* operand
  197. BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
  198. ) BOOST_NOEXCEPT
  199. {
  200. BOOST_STATIC_ASSERT_MSG(
  201. (boost::detail::variant::holds_element<boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >, const U >::value),
  202. "boost::variant does not contain specified type U, "
  203. "call to boost::get<U>(const boost::variant<T...>*) will always return NULL"
  204. );
  205. return relaxed_get<U>(operand);
  206. }
  207. template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
  208. inline
  209. typename add_reference<U>::type
  210. strict_get(
  211. boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >& operand
  212. BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
  213. )
  214. {
  215. BOOST_STATIC_ASSERT_MSG(
  216. (boost::detail::variant::holds_element<boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >, U >::value),
  217. "boost::variant does not contain specified type U, "
  218. "call to boost::get<U>(boost::variant<T...>&) will always throw boost::bad_get exception"
  219. );
  220. return relaxed_get<U>(operand);
  221. }
  222. template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
  223. inline
  224. typename add_reference<const U>::type
  225. strict_get(
  226. const boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >& operand
  227. BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
  228. )
  229. {
  230. BOOST_STATIC_ASSERT_MSG(
  231. (boost::detail::variant::holds_element<boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >, const U >::value),
  232. "boost::variant does not contain specified type U, "
  233. "call to boost::get<U>(const boost::variant<T...>&) will always throw boost::bad_get exception"
  234. );
  235. return relaxed_get<U>(operand);
  236. }
  237. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  238. template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
  239. inline
  240. U&&
  241. strict_get(
  242. boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >&& operand
  243. BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
  244. )
  245. {
  246. BOOST_STATIC_ASSERT_MSG(
  247. (!boost::is_lvalue_reference<U>::value),
  248. "remove ampersand '&' from template type U in boost::get<U>(boost::variant<T...>&&) "
  249. );
  250. BOOST_STATIC_ASSERT_MSG(
  251. (boost::detail::variant::holds_element<boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >, U >::value),
  252. "boost::variant does not contain specified type U, "
  253. "call to boost::get<U>(const boost::variant<T...>&) will always throw boost::bad_get exception"
  254. );
  255. return relaxed_get<U>(detail::variant::move(operand));
  256. }
  257. #endif
  258. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  259. // get<U>(variant) methods
  260. //
  261. template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
  262. inline
  263. typename add_pointer<U>::type
  264. get(
  265. boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >* operand
  266. BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
  267. ) BOOST_NOEXCEPT
  268. {
  269. #ifdef BOOST_VARIANT_USE_RELAXED_GET_BY_DEFAULT
  270. return relaxed_get<U>(operand);
  271. #else
  272. return strict_get<U>(operand);
  273. #endif
  274. }
  275. template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
  276. inline
  277. typename add_pointer<const U>::type
  278. get(
  279. const boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >* operand
  280. BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
  281. ) BOOST_NOEXCEPT
  282. {
  283. #ifdef BOOST_VARIANT_USE_RELAXED_GET_BY_DEFAULT
  284. return relaxed_get<U>(operand);
  285. #else
  286. return strict_get<U>(operand);
  287. #endif
  288. }
  289. template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
  290. inline
  291. typename add_reference<U>::type
  292. get(
  293. boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >& operand
  294. BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
  295. )
  296. {
  297. #ifdef BOOST_VARIANT_USE_RELAXED_GET_BY_DEFAULT
  298. return relaxed_get<U>(operand);
  299. #else
  300. return strict_get<U>(operand);
  301. #endif
  302. }
  303. template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
  304. inline
  305. typename add_reference<const U>::type
  306. get(
  307. const boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >& operand
  308. BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
  309. )
  310. {
  311. #ifdef BOOST_VARIANT_USE_RELAXED_GET_BY_DEFAULT
  312. return relaxed_get<U>(operand);
  313. #else
  314. return strict_get<U>(operand);
  315. #endif
  316. }
  317. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  318. template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
  319. inline
  320. U&&
  321. get(
  322. boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >&& operand
  323. BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
  324. )
  325. {
  326. #ifdef BOOST_VARIANT_USE_RELAXED_GET_BY_DEFAULT
  327. return relaxed_get<U>(detail::variant::move(operand));
  328. #else
  329. return strict_get<U>(detail::variant::move(operand));
  330. #endif
  331. }
  332. #endif
  333. } // namespace boost
  334. #endif // BOOST_VARIANT_GET_HPP