polymorphic_get.hpp 11 KB

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