any.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. // See http://www.boost.org/libs/any for Documentation.
  2. #ifndef BOOST_ANY_INCLUDED
  3. #define BOOST_ANY_INCLUDED
  4. #include <boost/config.hpp>
  5. #ifdef BOOST_HAS_PRAGMA_ONCE
  6. # pragma once
  7. #endif
  8. /// \file boost/any.hpp
  9. /// \brief \copybrief boost::any
  10. // what: variant type boost::any
  11. // who: contributed by Kevlin Henney,
  12. // with features contributed and bugs found by
  13. // Antony Polukhin, Ed Brey, Mark Rodgers,
  14. // Peter Dimov, and James Curran
  15. // when: July 2001, April 2013 - 2020
  16. #include <boost/any/bad_any_cast.hpp>
  17. #include <boost/any/fwd.hpp>
  18. #include <boost/any/detail/placeholder.hpp>
  19. #include <boost/throw_exception.hpp>
  20. #include <boost/type_index.hpp>
  21. #include <memory> // for std::addressof
  22. #include <type_traits>
  23. namespace boost
  24. {
  25. /// \brief A class whose instances can hold instances of any
  26. /// type that satisfies \forcedlink{ValueType} requirements.
  27. class any
  28. {
  29. public:
  30. /// \post this->empty() is true.
  31. constexpr any() noexcept
  32. : content(0)
  33. {
  34. }
  35. /// Makes a copy of `value`, so
  36. /// that the initial content of the new instance is equivalent
  37. /// in both type and value to `value`.
  38. ///
  39. /// \throws std::bad_alloc or any exceptions arising from the copy
  40. /// constructor of the contained type.
  41. template<typename ValueType>
  42. any(const ValueType & value)
  43. : content(new holder<
  44. typename std::remove_cv<typename std::decay<const ValueType>::type>::type
  45. >(value))
  46. {
  47. static_assert(
  48. !anys::detail::is_basic_any<ValueType>::value,
  49. "boost::any shall not be constructed from boost::anys::basic_any"
  50. );
  51. }
  52. /// Copy constructor that copies content of
  53. /// `other` into new instance, so that any content
  54. /// is equivalent in both type and value to the content of
  55. /// `other`, or empty if `other` is empty.
  56. ///
  57. /// \throws May fail with a `std::bad_alloc`
  58. /// exception or any exceptions arising from the copy
  59. /// constructor of the contained type.
  60. any(const any & other)
  61. : content(other.content ? other.content->clone() : 0)
  62. {
  63. }
  64. /// Move constructor that moves content of
  65. /// `other` into new instance and leaves `other` empty.
  66. ///
  67. /// \post other->empty() is true
  68. /// \throws Nothing.
  69. any(any&& other) noexcept
  70. : content(other.content)
  71. {
  72. other.content = 0;
  73. }
  74. /// Forwards `value`, so
  75. /// that the initial content of the new instance is equivalent
  76. /// in both type and value to `value` before the forward.
  77. ///
  78. /// \throws std::bad_alloc or any exceptions arising from the move or
  79. /// copy constructor of the contained type.
  80. template<typename ValueType>
  81. any(ValueType&& value
  82. , typename std::enable_if<!std::is_same<any&, ValueType>::value >::type* = 0 // disable if value has type `any&`
  83. , typename std::enable_if<!std::is_const<ValueType>::value >::type* = 0) // disable if value has type `const ValueType&&`
  84. : content(new holder< typename std::decay<ValueType>::type >(std::forward<ValueType>(value)))
  85. {
  86. static_assert(
  87. !anys::detail::is_basic_any<typename std::decay<ValueType>::type>::value,
  88. "boost::any shall not be constructed from boost::anys::basic_any"
  89. );
  90. }
  91. /// Releases any and all resources used in management of instance.
  92. ///
  93. /// \throws Nothing.
  94. ~any() noexcept
  95. {
  96. delete content;
  97. }
  98. public: // modifiers
  99. /// Exchange of the contents of `*this` and `rhs`.
  100. ///
  101. /// \returns `*this`
  102. /// \throws Nothing.
  103. any & swap(any & rhs) noexcept
  104. {
  105. placeholder* tmp = content;
  106. content = rhs.content;
  107. rhs.content = tmp;
  108. return *this;
  109. }
  110. /// Copies content of `rhs` into
  111. /// current instance, discarding previous content, so that the
  112. /// new content is equivalent in both type and value to the
  113. /// content of `rhs`, or empty if `rhs.empty()`.
  114. ///
  115. /// \throws std::bad_alloc
  116. /// or any exceptions arising from the copy constructor of the
  117. /// contained type. Assignment satisfies the strong guarantee
  118. /// of exception safety.
  119. any & operator=(const any& rhs)
  120. {
  121. any(rhs).swap(*this);
  122. return *this;
  123. }
  124. /// Moves content of `rhs` into
  125. /// current instance, discarding previous content, so that the
  126. /// new content is equivalent in both type and value to the
  127. /// content of `rhs` before move, or empty if
  128. /// `rhs.empty()`.
  129. ///
  130. /// \post `rhs->empty()` is true
  131. /// \throws Nothing.
  132. any & operator=(any&& rhs) noexcept
  133. {
  134. rhs.swap(*this);
  135. any().swap(rhs);
  136. return *this;
  137. }
  138. /// Forwards `rhs`,
  139. /// discarding previous content, so that the new content of is
  140. /// equivalent in both type and value to
  141. /// `rhs` before forward.
  142. ///
  143. /// \throws std::bad_alloc
  144. /// or any exceptions arising from the move or copy constructor of the
  145. /// contained type. Assignment satisfies the strong guarantee
  146. /// of exception safety.
  147. template <class ValueType>
  148. any & operator=(ValueType&& rhs)
  149. {
  150. static_assert(
  151. !anys::detail::is_basic_any<typename std::decay<ValueType>::type>::value,
  152. "boost::anys::basic_any shall not be assigned into boost::any"
  153. );
  154. any(std::forward<ValueType>(rhs)).swap(*this);
  155. return *this;
  156. }
  157. public: // queries
  158. /// \returns `true` if instance is empty, otherwise `false`.
  159. /// \throws Nothing.
  160. bool empty() const noexcept
  161. {
  162. return !content;
  163. }
  164. /// \post this->empty() is true
  165. void clear() noexcept
  166. {
  167. any().swap(*this);
  168. }
  169. /// \returns the `typeid` of the
  170. /// contained value if instance is non-empty, otherwise
  171. /// `typeid(void)`.
  172. ///
  173. /// Useful for querying against types known either at compile time or
  174. /// only at runtime.
  175. const boost::typeindex::type_info& type() const noexcept
  176. {
  177. return content ? content->type() : boost::typeindex::type_id<void>().type_info();
  178. }
  179. private: // types
  180. /// @cond
  181. class BOOST_SYMBOL_VISIBLE placeholder: public boost::anys::detail::placeholder
  182. {
  183. public:
  184. virtual placeholder * clone() const = 0;
  185. };
  186. template<typename ValueType>
  187. class holder final
  188. : public placeholder
  189. {
  190. public: // structors
  191. holder(const ValueType & value)
  192. : held(value)
  193. {
  194. }
  195. holder(ValueType&& value)
  196. : held(static_cast< ValueType&& >(value))
  197. {
  198. }
  199. public: // queries
  200. const boost::typeindex::type_info& type() const noexcept override
  201. {
  202. return boost::typeindex::type_id<ValueType>().type_info();
  203. }
  204. placeholder * clone() const BOOST_OVERRIDE
  205. {
  206. return new holder(held);
  207. }
  208. public: // representation
  209. ValueType held;
  210. private: // intentionally left unimplemented
  211. holder & operator=(const holder &);
  212. };
  213. private: // representation
  214. template<typename ValueType>
  215. friend ValueType * unsafe_any_cast(any *) noexcept;
  216. friend class boost::anys::unique_any;
  217. placeholder * content;
  218. /// @endcond
  219. };
  220. /// Exchange of the contents of `lhs` and `rhs`.
  221. /// \throws Nothing.
  222. inline void swap(any & lhs, any & rhs) noexcept
  223. {
  224. lhs.swap(rhs);
  225. }
  226. /// @cond
  227. // Note: The "unsafe" versions of any_cast are not part of the
  228. // public interface and may be removed at any time. They are
  229. // required where we know what type is stored in the any and can't
  230. // use typeid() comparison, e.g., when our types may travel across
  231. // different shared libraries.
  232. template<typename ValueType>
  233. inline ValueType * unsafe_any_cast(any * operand) noexcept
  234. {
  235. return std::addressof(
  236. static_cast<any::holder<ValueType> *>(operand->content)->held
  237. );
  238. }
  239. template<typename ValueType>
  240. inline const ValueType * unsafe_any_cast(const any * operand) noexcept
  241. {
  242. return boost::unsafe_any_cast<ValueType>(const_cast<any *>(operand));
  243. }
  244. /// @endcond
  245. /// \returns Pointer to a ValueType stored in `operand`, nullptr if
  246. /// `operand` does not contain specified `ValueType`.
  247. template<typename ValueType>
  248. ValueType * any_cast(any * operand) noexcept
  249. {
  250. return operand && operand->type() == boost::typeindex::type_id<ValueType>()
  251. ? boost::unsafe_any_cast<typename std::remove_cv<ValueType>::type>(operand)
  252. : 0;
  253. }
  254. /// \returns Const pointer to a ValueType stored in `operand`, nullptr if
  255. /// `operand` does not contain specified `ValueType`.
  256. template<typename ValueType>
  257. inline const ValueType * any_cast(const any * operand) noexcept
  258. {
  259. return boost::any_cast<ValueType>(const_cast<any *>(operand));
  260. }
  261. /// \returns ValueType stored in `operand`
  262. /// \throws boost::bad_any_cast if `operand` does not contain
  263. /// specified ValueType.
  264. template<typename ValueType>
  265. ValueType any_cast(any & operand)
  266. {
  267. typedef typename std::remove_reference<ValueType>::type nonref;
  268. nonref * result = boost::any_cast<nonref>(std::addressof(operand));
  269. if(!result)
  270. boost::throw_exception(bad_any_cast());
  271. // Attempt to avoid construction of a temporary object in cases when
  272. // `ValueType` is not a reference. Example:
  273. // `static_cast<std::string>(*result);`
  274. // which is equal to `std::string(*result);`
  275. typedef typename std::conditional<
  276. std::is_reference<ValueType>::value,
  277. ValueType,
  278. typename std::add_lvalue_reference<ValueType>::type
  279. >::type ref_type;
  280. #ifdef BOOST_MSVC
  281. # pragma warning(push)
  282. # pragma warning(disable: 4172) // "returning address of local variable or temporary" but *result is not local!
  283. #endif
  284. return static_cast<ref_type>(*result);
  285. #ifdef BOOST_MSVC
  286. # pragma warning(pop)
  287. #endif
  288. }
  289. /// \returns `ValueType` stored in `operand`
  290. /// \throws boost::bad_any_cast if `operand` does not contain
  291. /// specified `ValueType`.
  292. template<typename ValueType>
  293. inline ValueType any_cast(const any & operand)
  294. {
  295. typedef typename std::remove_reference<ValueType>::type nonref;
  296. return boost::any_cast<const nonref &>(const_cast<any &>(operand));
  297. }
  298. /// \returns `ValueType` stored in `operand`, leaving the `operand` empty.
  299. /// \throws boost::bad_any_cast if `operand` does not contain
  300. /// specified `ValueType`.
  301. template<typename ValueType>
  302. inline ValueType any_cast(any&& operand)
  303. {
  304. static_assert(
  305. std::is_rvalue_reference<ValueType&&>::value /*true if ValueType is rvalue or just a value*/
  306. || std::is_const< typename std::remove_reference<ValueType>::type >::value,
  307. "boost::any_cast shall not be used for getting nonconst references to temporary objects"
  308. );
  309. return boost::any_cast<ValueType>(operand);
  310. }
  311. }
  312. // Copyright Kevlin Henney, 2000, 2001, 2002. All rights reserved.
  313. // Copyright Antony Polukhin, 2013-2024.
  314. //
  315. // Distributed under the Boost Software License, Version 1.0. (See
  316. // accompanying file LICENSE_1_0.txt or copy at
  317. // http://www.boost.org/LICENSE_1_0.txt)
  318. #endif