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