get.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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-2019 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/detail/workaround.hpp>
  17. #include <boost/static_assert.hpp>
  18. #include <boost/throw_exception.hpp>
  19. #include <boost/utility/addressof.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. virtual const char * what() const BOOST_NOEXCEPT_OR_NOTHROW
  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(__BORLANDC__, BOOST_TESTED_AT(0x0551))
  83. # define BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(t)
  84. # else
  85. # define BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(t) \
  86. , t* = 0
  87. # endif
  88. #endif
  89. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  90. // relaxed_get<U>(variant) methods
  91. //
  92. template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
  93. inline
  94. typename add_pointer<U>::type
  95. relaxed_get(
  96. boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >* operand
  97. BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
  98. ) BOOST_NOEXCEPT
  99. {
  100. typedef typename add_pointer<U>::type U_ptr;
  101. if (!operand) return static_cast<U_ptr>(0);
  102. detail::variant::get_visitor<U> v;
  103. return operand->apply_visitor(v);
  104. }
  105. template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
  106. inline
  107. typename add_pointer<const U>::type
  108. relaxed_get(
  109. const boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >* operand
  110. BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
  111. ) BOOST_NOEXCEPT
  112. {
  113. typedef typename add_pointer<const U>::type U_ptr;
  114. if (!operand) return static_cast<U_ptr>(0);
  115. detail::variant::get_visitor<const U> v;
  116. return operand->apply_visitor(v);
  117. }
  118. template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
  119. inline
  120. typename add_reference<U>::type
  121. relaxed_get(
  122. boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >& operand
  123. BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
  124. )
  125. {
  126. typedef typename add_pointer<U>::type U_ptr;
  127. U_ptr result = relaxed_get<U>(boost::addressof(operand));
  128. if (!result)
  129. boost::throw_exception(bad_get());
  130. return *result;
  131. }
  132. template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
  133. inline
  134. typename add_reference<const U>::type
  135. relaxed_get(
  136. const boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >& operand
  137. BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
  138. )
  139. {
  140. typedef typename add_pointer<const U>::type U_ptr;
  141. U_ptr result = relaxed_get<const U>(boost::addressof(operand));
  142. if (!result)
  143. boost::throw_exception(bad_get());
  144. return *result;
  145. }
  146. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  147. #if defined(BOOST_MSVC) && (_MSC_VER < 1900) // MSVC-2014 has fixed the incorrect diagnostics.
  148. # pragma warning(push)
  149. # pragma warning(disable: 4172) // returning address of local variable or temporary
  150. #endif
  151. template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
  152. inline
  153. U&&
  154. relaxed_get(
  155. boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >&& operand
  156. BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
  157. )
  158. {
  159. typedef typename add_pointer<U>::type U_ptr;
  160. U_ptr result = relaxed_get<U>(boost::addressof(operand));
  161. if (!result)
  162. boost::throw_exception(bad_get());
  163. return static_cast<U&&>(*result);
  164. }
  165. #if defined(BOOST_MSVC) && (_MSC_VER < 1900)
  166. # pragma warning(pop)
  167. #endif
  168. #endif
  169. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  170. // strict_get<U>(variant) methods
  171. //
  172. template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
  173. inline
  174. typename add_pointer<U>::type
  175. strict_get(
  176. boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >* operand
  177. BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
  178. ) BOOST_NOEXCEPT
  179. {
  180. BOOST_STATIC_ASSERT_MSG(
  181. (boost::detail::variant::holds_element<boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >, U >::value),
  182. "boost::variant does not contain specified type U, "
  183. "call to boost::get<U>(boost::variant<T...>*) will always return NULL"
  184. );
  185. return relaxed_get<U>(operand);
  186. }
  187. template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
  188. inline
  189. typename add_pointer<const U>::type
  190. strict_get(
  191. const boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >* operand
  192. BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
  193. ) BOOST_NOEXCEPT
  194. {
  195. BOOST_STATIC_ASSERT_MSG(
  196. (boost::detail::variant::holds_element<boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >, const U >::value),
  197. "boost::variant does not contain specified type U, "
  198. "call to boost::get<U>(const boost::variant<T...>*) will always return NULL"
  199. );
  200. return relaxed_get<U>(operand);
  201. }
  202. template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
  203. inline
  204. typename add_reference<U>::type
  205. strict_get(
  206. boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >& operand
  207. BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
  208. )
  209. {
  210. BOOST_STATIC_ASSERT_MSG(
  211. (boost::detail::variant::holds_element<boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >, U >::value),
  212. "boost::variant does not contain specified type U, "
  213. "call to boost::get<U>(boost::variant<T...>&) will always throw boost::bad_get exception"
  214. );
  215. return relaxed_get<U>(operand);
  216. }
  217. template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
  218. inline
  219. typename add_reference<const U>::type
  220. strict_get(
  221. const boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >& operand
  222. BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
  223. )
  224. {
  225. BOOST_STATIC_ASSERT_MSG(
  226. (boost::detail::variant::holds_element<boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >, const U >::value),
  227. "boost::variant does not contain specified type U, "
  228. "call to boost::get<U>(const boost::variant<T...>&) will always throw boost::bad_get exception"
  229. );
  230. return relaxed_get<U>(operand);
  231. }
  232. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  233. template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
  234. inline
  235. U&&
  236. strict_get(
  237. boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >&& operand
  238. BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
  239. )
  240. {
  241. BOOST_STATIC_ASSERT_MSG(
  242. (!boost::is_lvalue_reference<U>::value),
  243. "remove ampersand '&' from template type U in boost::get<U>(boost::variant<T...>&&) "
  244. );
  245. BOOST_STATIC_ASSERT_MSG(
  246. (boost::detail::variant::holds_element<boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >, U >::value),
  247. "boost::variant does not contain specified type U, "
  248. "call to boost::get<U>(const boost::variant<T...>&) will always throw boost::bad_get exception"
  249. );
  250. return relaxed_get<U>(detail::variant::move(operand));
  251. }
  252. #endif
  253. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  254. // get<U>(variant) methods
  255. //
  256. template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
  257. inline
  258. typename add_pointer<U>::type
  259. get(
  260. boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >* operand
  261. BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
  262. ) BOOST_NOEXCEPT
  263. {
  264. #ifdef BOOST_VARIANT_USE_RELAXED_GET_BY_DEFAULT
  265. return relaxed_get<U>(operand);
  266. #else
  267. return strict_get<U>(operand);
  268. #endif
  269. }
  270. template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
  271. inline
  272. typename add_pointer<const U>::type
  273. get(
  274. const boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >* operand
  275. BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
  276. ) BOOST_NOEXCEPT
  277. {
  278. #ifdef BOOST_VARIANT_USE_RELAXED_GET_BY_DEFAULT
  279. return relaxed_get<U>(operand);
  280. #else
  281. return strict_get<U>(operand);
  282. #endif
  283. }
  284. template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
  285. inline
  286. typename add_reference<U>::type
  287. get(
  288. boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >& operand
  289. BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
  290. )
  291. {
  292. #ifdef BOOST_VARIANT_USE_RELAXED_GET_BY_DEFAULT
  293. return relaxed_get<U>(operand);
  294. #else
  295. return strict_get<U>(operand);
  296. #endif
  297. }
  298. template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
  299. inline
  300. typename add_reference<const U>::type
  301. get(
  302. const boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >& operand
  303. BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
  304. )
  305. {
  306. #ifdef BOOST_VARIANT_USE_RELAXED_GET_BY_DEFAULT
  307. return relaxed_get<U>(operand);
  308. #else
  309. return strict_get<U>(operand);
  310. #endif
  311. }
  312. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  313. template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
  314. inline
  315. U&&
  316. get(
  317. boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >&& operand
  318. BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
  319. )
  320. {
  321. #ifdef BOOST_VARIANT_USE_RELAXED_GET_BY_DEFAULT
  322. return relaxed_get<U>(detail::variant::move(operand));
  323. #else
  324. return strict_get<U>(detail::variant::move(operand));
  325. #endif
  326. }
  327. #endif
  328. } // namespace boost
  329. #endif // BOOST_VARIANT_GET_HPP