any.hpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. // See http://www.boost.org/libs/any for Documentation.
  2. #ifndef BOOST_ANY_INCLUDED
  3. #define BOOST_ANY_INCLUDED
  4. #if defined(_MSC_VER)
  5. # pragma once
  6. #endif
  7. // what: variant type boost::any
  8. // who: contributed by Kevlin Henney,
  9. // with features contributed and bugs found by
  10. // Antony Polukhin, Ed Brey, Mark Rodgers,
  11. // Peter Dimov, and James Curran
  12. // when: July 2001, April 2013 - 2020
  13. #include <boost/config.hpp>
  14. #include <boost/type_index.hpp>
  15. #include <boost/type_traits/remove_reference.hpp>
  16. #include <boost/type_traits/decay.hpp>
  17. #include <boost/type_traits/remove_cv.hpp>
  18. #include <boost/type_traits/add_reference.hpp>
  19. #include <boost/type_traits/is_reference.hpp>
  20. #include <boost/type_traits/is_const.hpp>
  21. #include <boost/throw_exception.hpp>
  22. #include <boost/static_assert.hpp>
  23. #include <boost/utility/enable_if.hpp>
  24. #include <boost/core/addressof.hpp>
  25. #include <boost/type_traits/is_same.hpp>
  26. #include <boost/type_traits/is_const.hpp>
  27. #include <boost/type_traits/conditional.hpp>
  28. namespace boost
  29. {
  30. class any
  31. {
  32. public: // structors
  33. BOOST_CONSTEXPR any() BOOST_NOEXCEPT
  34. : content(0)
  35. {
  36. }
  37. template<typename ValueType>
  38. any(const ValueType & value)
  39. : content(new holder<
  40. BOOST_DEDUCED_TYPENAME remove_cv<BOOST_DEDUCED_TYPENAME decay<const ValueType>::type>::type
  41. >(value))
  42. {
  43. }
  44. any(const any & other)
  45. : content(other.content ? other.content->clone() : 0)
  46. {
  47. }
  48. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  49. // Move constructor
  50. any(any&& other) BOOST_NOEXCEPT
  51. : content(other.content)
  52. {
  53. other.content = 0;
  54. }
  55. // Perfect forwarding of ValueType
  56. template<typename ValueType>
  57. any(ValueType&& value
  58. , typename boost::disable_if<boost::is_same<any&, ValueType> >::type* = 0 // disable if value has type `any&`
  59. , typename boost::disable_if<boost::is_const<ValueType> >::type* = 0) // disable if value has type `const ValueType&&`
  60. : content(new holder< typename decay<ValueType>::type >(static_cast<ValueType&&>(value)))
  61. {
  62. }
  63. #endif
  64. ~any() BOOST_NOEXCEPT
  65. {
  66. delete content;
  67. }
  68. public: // modifiers
  69. any & swap(any & rhs) BOOST_NOEXCEPT
  70. {
  71. placeholder* tmp = content;
  72. content = rhs.content;
  73. rhs.content = tmp;
  74. return *this;
  75. }
  76. #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES
  77. template<typename ValueType>
  78. any & operator=(const ValueType & rhs)
  79. {
  80. any(rhs).swap(*this);
  81. return *this;
  82. }
  83. any & operator=(any rhs)
  84. {
  85. rhs.swap(*this);
  86. return *this;
  87. }
  88. #else
  89. any & operator=(const any& rhs)
  90. {
  91. any(rhs).swap(*this);
  92. return *this;
  93. }
  94. // move assignment
  95. any & operator=(any&& rhs) BOOST_NOEXCEPT
  96. {
  97. rhs.swap(*this);
  98. any().swap(rhs);
  99. return *this;
  100. }
  101. // Perfect forwarding of ValueType
  102. template <class ValueType>
  103. any & operator=(ValueType&& rhs)
  104. {
  105. any(static_cast<ValueType&&>(rhs)).swap(*this);
  106. return *this;
  107. }
  108. #endif
  109. public: // queries
  110. bool empty() const BOOST_NOEXCEPT
  111. {
  112. return !content;
  113. }
  114. void clear() BOOST_NOEXCEPT
  115. {
  116. any().swap(*this);
  117. }
  118. const boost::typeindex::type_info& type() const BOOST_NOEXCEPT
  119. {
  120. return content ? content->type() : boost::typeindex::type_id<void>().type_info();
  121. }
  122. #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
  123. private: // types
  124. #else
  125. public: // types (public so any_cast can be non-friend)
  126. #endif
  127. class BOOST_SYMBOL_VISIBLE placeholder
  128. {
  129. public: // structors
  130. virtual ~placeholder()
  131. {
  132. }
  133. public: // queries
  134. virtual const boost::typeindex::type_info& type() const BOOST_NOEXCEPT = 0;
  135. virtual placeholder * clone() const = 0;
  136. };
  137. template<typename ValueType>
  138. class holder
  139. #ifndef BOOST_NO_CXX11_FINAL
  140. final
  141. #endif
  142. : public placeholder
  143. {
  144. public: // structors
  145. holder(const ValueType & value)
  146. : held(value)
  147. {
  148. }
  149. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  150. holder(ValueType&& value)
  151. : held(static_cast< ValueType&& >(value))
  152. {
  153. }
  154. #endif
  155. public: // queries
  156. const boost::typeindex::type_info& type() const BOOST_NOEXCEPT BOOST_OVERRIDE
  157. {
  158. return boost::typeindex::type_id<ValueType>().type_info();
  159. }
  160. placeholder * clone() const BOOST_OVERRIDE
  161. {
  162. return new holder(held);
  163. }
  164. public: // representation
  165. ValueType held;
  166. private: // intentionally left unimplemented
  167. holder & operator=(const holder &);
  168. };
  169. #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
  170. private: // representation
  171. template<typename ValueType>
  172. friend ValueType * any_cast(any *) BOOST_NOEXCEPT;
  173. template<typename ValueType>
  174. friend ValueType * unsafe_any_cast(any *) BOOST_NOEXCEPT;
  175. #else
  176. public: // representation (public so any_cast can be non-friend)
  177. #endif
  178. placeholder * content;
  179. };
  180. inline void swap(any & lhs, any & rhs) BOOST_NOEXCEPT
  181. {
  182. lhs.swap(rhs);
  183. }
  184. class BOOST_SYMBOL_VISIBLE bad_any_cast :
  185. #ifndef BOOST_NO_RTTI
  186. public std::bad_cast
  187. #else
  188. public std::exception
  189. #endif
  190. {
  191. public:
  192. const char * what() const BOOST_NOEXCEPT_OR_NOTHROW BOOST_OVERRIDE
  193. {
  194. return "boost::bad_any_cast: "
  195. "failed conversion using boost::any_cast";
  196. }
  197. };
  198. template<typename ValueType>
  199. ValueType * any_cast(any * operand) BOOST_NOEXCEPT
  200. {
  201. return operand && operand->type() == boost::typeindex::type_id<ValueType>()
  202. ? boost::addressof(
  203. static_cast<any::holder<BOOST_DEDUCED_TYPENAME remove_cv<ValueType>::type> *>(operand->content)->held
  204. )
  205. : 0;
  206. }
  207. template<typename ValueType>
  208. inline const ValueType * any_cast(const any * operand) BOOST_NOEXCEPT
  209. {
  210. return any_cast<ValueType>(const_cast<any *>(operand));
  211. }
  212. template<typename ValueType>
  213. ValueType any_cast(any & operand)
  214. {
  215. typedef BOOST_DEDUCED_TYPENAME remove_reference<ValueType>::type nonref;
  216. nonref * result = any_cast<nonref>(boost::addressof(operand));
  217. if(!result)
  218. boost::throw_exception(bad_any_cast());
  219. // Attempt to avoid construction of a temporary object in cases when
  220. // `ValueType` is not a reference. Example:
  221. // `static_cast<std::string>(*result);`
  222. // which is equal to `std::string(*result);`
  223. typedef BOOST_DEDUCED_TYPENAME boost::conditional<
  224. boost::is_reference<ValueType>::value,
  225. ValueType,
  226. BOOST_DEDUCED_TYPENAME boost::add_reference<ValueType>::type
  227. >::type ref_type;
  228. #ifdef BOOST_MSVC
  229. # pragma warning(push)
  230. # pragma warning(disable: 4172) // "returning address of local variable or temporary" but *result is not local!
  231. #endif
  232. return static_cast<ref_type>(*result);
  233. #ifdef BOOST_MSVC
  234. # pragma warning(pop)
  235. #endif
  236. }
  237. template<typename ValueType>
  238. inline ValueType any_cast(const any & operand)
  239. {
  240. typedef BOOST_DEDUCED_TYPENAME remove_reference<ValueType>::type nonref;
  241. return any_cast<const nonref &>(const_cast<any &>(operand));
  242. }
  243. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  244. template<typename ValueType>
  245. inline ValueType any_cast(any&& operand)
  246. {
  247. BOOST_STATIC_ASSERT_MSG(
  248. boost::is_rvalue_reference<ValueType&&>::value /*true if ValueType is rvalue or just a value*/
  249. || boost::is_const< typename boost::remove_reference<ValueType>::type >::value,
  250. "boost::any_cast shall not be used for getting nonconst references to temporary objects"
  251. );
  252. return any_cast<ValueType>(operand);
  253. }
  254. #endif
  255. // Note: The "unsafe" versions of any_cast are not part of the
  256. // public interface and may be removed at any time. They are
  257. // required where we know what type is stored in the any and can't
  258. // use typeid() comparison, e.g., when our types may travel across
  259. // different shared libraries.
  260. template<typename ValueType>
  261. inline ValueType * unsafe_any_cast(any * operand) BOOST_NOEXCEPT
  262. {
  263. return boost::addressof(
  264. static_cast<any::holder<ValueType> *>(operand->content)->held
  265. );
  266. }
  267. template<typename ValueType>
  268. inline const ValueType * unsafe_any_cast(const any * operand) BOOST_NOEXCEPT
  269. {
  270. return unsafe_any_cast<ValueType>(const_cast<any *>(operand));
  271. }
  272. }
  273. // Copyright Kevlin Henney, 2000, 2001, 2002. All rights reserved.
  274. // Copyright Antony Polukhin, 2013-2021.
  275. //
  276. // Distributed under the Boost Software License, Version 1.0. (See
  277. // accompanying file LICENSE_1_0.txt or copy at
  278. // http://www.boost.org/LICENSE_1_0.txt)
  279. #endif