basic_any.hpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. // Copyright Ruslan Arutyunyan, 2019-2021.
  2. // Copyright Antony Polukhin, 2021-2023.
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See
  5. // accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. // Contributed by Ruslan Arutyunyan
  8. #ifndef BOOST_ANYS_BASIC_ANY_HPP_INCLUDED
  9. #define BOOST_ANYS_BASIC_ANY_HPP_INCLUDED
  10. #include <boost/config.hpp>
  11. #ifdef BOOST_HAS_PRAGMA_ONCE
  12. # pragma once
  13. #endif
  14. #include <boost/any/bad_any_cast.hpp>
  15. #include <boost/any/fwd.hpp>
  16. #include <boost/assert.hpp>
  17. #include <boost/aligned_storage.hpp>
  18. #include <boost/type_index.hpp>
  19. #include <boost/type_traits/remove_reference.hpp>
  20. #include <boost/type_traits/decay.hpp>
  21. #include <boost/type_traits/remove_cv.hpp>
  22. #include <boost/type_traits/add_reference.hpp>
  23. #include <boost/type_traits/is_reference.hpp>
  24. #include <boost/type_traits/is_const.hpp>
  25. #include <boost/type_traits/is_nothrow_move_constructible.hpp>
  26. #include <boost/throw_exception.hpp>
  27. #include <boost/static_assert.hpp>
  28. #include <boost/core/enable_if.hpp>
  29. #include <boost/core/addressof.hpp>
  30. #include <boost/type_traits/is_same.hpp>
  31. #include <boost/type_traits/conditional.hpp>
  32. namespace boost {
  33. namespace anys {
  34. template <std::size_t OptimizeForSize, std::size_t OptimizeForAlignment>
  35. class basic_any
  36. {
  37. BOOST_STATIC_ASSERT_MSG(OptimizeForSize > 0 && OptimizeForAlignment > 0, "Size and Align shall be positive values");
  38. BOOST_STATIC_ASSERT_MSG(OptimizeForSize >= OptimizeForAlignment, "Size shall non less than Align");
  39. BOOST_STATIC_ASSERT_MSG((OptimizeForAlignment & (OptimizeForAlignment - 1)) == 0, "Align shall be a power of 2");
  40. BOOST_STATIC_ASSERT_MSG(OptimizeForSize % OptimizeForAlignment == 0, "Size shall be multiple of alignment");
  41. private:
  42. enum operation
  43. {
  44. Destroy,
  45. Move,
  46. Copy,
  47. AnyCast,
  48. UnsafeCast,
  49. Typeinfo
  50. };
  51. template <typename ValueType>
  52. static void* small_manager(operation op, basic_any& left, const basic_any* right, const boost::typeindex::type_info* info)
  53. {
  54. switch (op)
  55. {
  56. case Destroy:
  57. BOOST_ASSERT(!left.empty());
  58. reinterpret_cast<ValueType*>(&left.content.small_value)->~ValueType();
  59. break;
  60. case Move: {
  61. BOOST_ASSERT(left.empty());
  62. BOOST_ASSERT(right);
  63. BOOST_ASSERT(!right->empty());
  64. BOOST_ASSERT(right->type() == boost::typeindex::type_id<ValueType>());
  65. ValueType* value = reinterpret_cast<ValueType*>(&const_cast<basic_any*>(right)->content.small_value);
  66. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  67. new (&left.content.small_value) ValueType(std::move(*value));
  68. #else
  69. new (&left.content.small_value) ValueType(*value);
  70. #endif
  71. left.man = right->man;
  72. reinterpret_cast<ValueType const*>(&right->content.small_value)->~ValueType();
  73. const_cast<basic_any*>(right)->man = 0;
  74. };
  75. break;
  76. case Copy:
  77. BOOST_ASSERT(left.empty());
  78. BOOST_ASSERT(right);
  79. BOOST_ASSERT(!right->empty());
  80. BOOST_ASSERT(right->type() == boost::typeindex::type_id<ValueType>());
  81. new (&left.content.small_value) ValueType(*reinterpret_cast<const ValueType*>(&right->content.small_value));
  82. left.man = right->man;
  83. break;
  84. case AnyCast:
  85. BOOST_ASSERT(info);
  86. BOOST_ASSERT(!left.empty());
  87. return boost::typeindex::type_id<ValueType>() == *info ?
  88. reinterpret_cast<typename remove_cv<ValueType>::type *>(&left.content.small_value) : 0;
  89. case UnsafeCast:
  90. BOOST_ASSERT(!left.empty());
  91. return reinterpret_cast<typename remove_cv<ValueType>::type *>(&left.content.small_value);
  92. case Typeinfo:
  93. return const_cast<void*>(static_cast<const void*>(&boost::typeindex::type_id<ValueType>().type_info()));
  94. }
  95. return 0;
  96. }
  97. template <typename ValueType>
  98. static void* large_manager(operation op, basic_any& left, const basic_any* right, const boost::typeindex::type_info* info)
  99. {
  100. switch (op)
  101. {
  102. case Destroy:
  103. BOOST_ASSERT(!left.empty());
  104. delete static_cast<ValueType*>(left.content.large_value);
  105. break;
  106. case Move:
  107. BOOST_ASSERT(left.empty());
  108. BOOST_ASSERT(right);
  109. BOOST_ASSERT(!right->empty());
  110. BOOST_ASSERT(right->type() == boost::typeindex::type_id<ValueType>());
  111. left.content.large_value = right->content.large_value;
  112. left.man = right->man;
  113. const_cast<basic_any*>(right)->content.large_value = 0;
  114. const_cast<basic_any*>(right)->man = 0;
  115. break;
  116. case Copy:
  117. BOOST_ASSERT(left.empty());
  118. BOOST_ASSERT(right);
  119. BOOST_ASSERT(!right->empty());
  120. BOOST_ASSERT(right->type() == boost::typeindex::type_id<ValueType>());
  121. left.content.large_value = new ValueType(*static_cast<const ValueType*>(right->content.large_value));
  122. left.man = right->man;
  123. break;
  124. case AnyCast:
  125. BOOST_ASSERT(info);
  126. BOOST_ASSERT(!left.empty());
  127. return boost::typeindex::type_id<ValueType>() == *info ?
  128. static_cast<typename remove_cv<ValueType>::type *>(left.content.large_value) : 0;
  129. case UnsafeCast:
  130. BOOST_ASSERT(!left.empty());
  131. return reinterpret_cast<typename remove_cv<ValueType>::type *>(left.content.large_value);
  132. case Typeinfo:
  133. return const_cast<void*>(static_cast<const void*>(&boost::typeindex::type_id<ValueType>().type_info()));
  134. }
  135. return 0;
  136. }
  137. template <typename ValueType>
  138. struct is_small_object : boost::integral_constant<bool, sizeof(ValueType) <= OptimizeForSize &&
  139. boost::alignment_of<ValueType>::value <= OptimizeForAlignment &&
  140. boost::is_nothrow_move_constructible<ValueType>::value>
  141. {};
  142. template <typename ValueType>
  143. static void create(basic_any& any, const ValueType& value, boost::true_type)
  144. {
  145. typedef typename boost::decay<const ValueType>::type DecayedType;
  146. any.man = &small_manager<DecayedType>;
  147. new (&any.content.small_value) ValueType(value);
  148. }
  149. template <typename ValueType>
  150. static void create(basic_any& any, const ValueType& value, boost::false_type)
  151. {
  152. typedef typename boost::decay<const ValueType>::type DecayedType;
  153. any.man = &large_manager<DecayedType>;
  154. any.content.large_value = new DecayedType(value);
  155. }
  156. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  157. template <typename ValueType>
  158. static void create(basic_any& any, ValueType&& value, boost::true_type)
  159. {
  160. typedef typename boost::decay<const ValueType>::type DecayedType;
  161. any.man = &small_manager<DecayedType>;
  162. new (&any.content.small_value) DecayedType(static_cast<ValueType&&>(value));
  163. }
  164. template <typename ValueType>
  165. static void create(basic_any& any, ValueType&& value, boost::false_type)
  166. {
  167. typedef typename boost::decay<const ValueType>::type DecayedType;
  168. any.man = &large_manager<DecayedType>;
  169. any.content.large_value = new DecayedType(static_cast<ValueType&&>(value));
  170. }
  171. #endif
  172. public: // non-type template parameters accessors
  173. static BOOST_CONSTEXPR_OR_CONST std::size_t buffer_size = OptimizeForSize;
  174. static BOOST_CONSTEXPR_OR_CONST std::size_t buffer_align = OptimizeForAlignment;
  175. public: // structors
  176. BOOST_CONSTEXPR basic_any() BOOST_NOEXCEPT
  177. : man(0), content()
  178. {
  179. }
  180. template<typename ValueType>
  181. basic_any(const ValueType & value)
  182. : man(0), content()
  183. {
  184. BOOST_STATIC_ASSERT_MSG(
  185. !(boost::is_same<ValueType, boost::any>::value),
  186. "boost::anys::basic_any shall not be constructed from boost::any"
  187. );
  188. BOOST_STATIC_ASSERT_MSG(
  189. !anys::detail::is_basic_any<ValueType>::value,
  190. "boost::anys::basic_any<A, B> shall not be constructed from boost::anys::basic_any<C, D>"
  191. );
  192. create(*this, value, is_small_object<ValueType>());
  193. }
  194. basic_any(const basic_any & other)
  195. : man(0), content()
  196. {
  197. if (other.man)
  198. {
  199. other.man(Copy, *this, &other, 0);
  200. }
  201. }
  202. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  203. // Move constructor
  204. basic_any(basic_any&& other) BOOST_NOEXCEPT
  205. : man(0), content()
  206. {
  207. if (other.man)
  208. {
  209. other.man(Move, *this, &other, 0);
  210. }
  211. }
  212. // Perfect forwarding of ValueType
  213. template<typename ValueType>
  214. basic_any(ValueType&& value
  215. , typename boost::disable_if<boost::is_same<basic_any&, ValueType> >::type* = 0 // disable if value has type `basic_any&`
  216. , typename boost::disable_if<boost::is_const<ValueType> >::type* = 0) // disable if value has type `const ValueType&&`
  217. : man(0), content()
  218. {
  219. typedef typename boost::decay<ValueType>::type DecayedType;
  220. BOOST_STATIC_ASSERT_MSG(
  221. !(boost::is_same<DecayedType, boost::any>::value),
  222. "boost::anys::basic_any shall not be constructed from boost::any"
  223. );
  224. BOOST_STATIC_ASSERT_MSG(
  225. !anys::detail::is_basic_any<DecayedType>::value,
  226. "boost::anys::basic_any<A, B> shall not be constructed from boost::anys::basic_any<C, D>"
  227. );
  228. create(*this, static_cast<ValueType&&>(value), is_small_object<DecayedType>());
  229. }
  230. #endif
  231. ~basic_any() BOOST_NOEXCEPT
  232. {
  233. if (man)
  234. {
  235. man(Destroy, *this, 0, 0);
  236. }
  237. }
  238. public: // modifiers
  239. basic_any & swap(basic_any & rhs) BOOST_NOEXCEPT
  240. {
  241. if (this == &rhs)
  242. {
  243. return *this;
  244. }
  245. if (man && rhs.man)
  246. {
  247. basic_any tmp;
  248. rhs.man(Move, tmp, &rhs, 0);
  249. man(Move, rhs, this, 0);
  250. tmp.man(Move, *this, &tmp, 0);
  251. }
  252. else if (man)
  253. {
  254. man(Move, rhs, this, 0);
  255. }
  256. else if (rhs.man)
  257. {
  258. rhs.man(Move, *this, &rhs, 0);
  259. }
  260. return *this;
  261. }
  262. #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES
  263. template<typename ValueType>
  264. basic_any & operator=(const ValueType & rhs)
  265. {
  266. BOOST_STATIC_ASSERT_MSG(
  267. !(boost::is_same<ValueType, boost::any>::value),
  268. "boost::any shall not be assigned into boost::anys::basic_any"
  269. );
  270. BOOST_STATIC_ASSERT_MSG(
  271. !anys::detail::is_basic_any<ValueType>::value,
  272. "boost::anys::basic_any<A, B> shall not be assigned into boost::anys::basic_any<C, D>"
  273. );
  274. basic_any(rhs).swap(*this);
  275. return *this;
  276. }
  277. basic_any & operator=(basic_any rhs)
  278. {
  279. rhs.swap(*this);
  280. return *this;
  281. }
  282. #else
  283. basic_any & operator=(const basic_any& rhs)
  284. {
  285. basic_any(rhs).swap(*this);
  286. return *this;
  287. }
  288. // move assignment
  289. basic_any & operator=(basic_any&& rhs) BOOST_NOEXCEPT
  290. {
  291. rhs.swap(*this);
  292. basic_any().swap(rhs);
  293. return *this;
  294. }
  295. // Perfect forwarding of ValueType
  296. template <class ValueType>
  297. basic_any & operator=(ValueType&& rhs)
  298. {
  299. typedef typename boost::decay<ValueType>::type DecayedType;
  300. BOOST_STATIC_ASSERT_MSG(
  301. !(boost::is_same<DecayedType, boost::any>::value),
  302. "boost::any shall not be assigned into boost::anys::basic_any"
  303. );
  304. BOOST_STATIC_ASSERT_MSG(
  305. (!anys::detail::is_basic_any<DecayedType>::value || boost::is_same<DecayedType, basic_any>::value),
  306. "boost::anys::basic_any<A, B> shall not be assigned into boost::anys::basic_any<C, D>"
  307. );
  308. basic_any(static_cast<ValueType&&>(rhs)).swap(*this);
  309. return *this;
  310. }
  311. #endif
  312. public: // queries
  313. bool empty() const BOOST_NOEXCEPT
  314. {
  315. return !man;
  316. }
  317. void clear() BOOST_NOEXCEPT
  318. {
  319. basic_any().swap(*this);
  320. }
  321. const boost::typeindex::type_info& type() const BOOST_NOEXCEPT
  322. {
  323. return man
  324. ? *static_cast<const boost::typeindex::type_info*>(man(Typeinfo, const_cast<basic_any&>(*this), 0, 0))
  325. : boost::typeindex::type_id<void>().type_info();
  326. }
  327. private: // representation
  328. template<typename ValueType, std::size_t Size, std::size_t Alignment>
  329. friend ValueType * any_cast(basic_any<Size, Alignment> *) BOOST_NOEXCEPT;
  330. template<typename ValueType, std::size_t Size, std::size_t Alignment>
  331. friend ValueType * unsafe_any_cast(basic_any<Size, Alignment> *) BOOST_NOEXCEPT;
  332. typedef void*(*manager)(operation op, basic_any& left, const basic_any* right, const boost::typeindex::type_info* info);
  333. manager man;
  334. union content {
  335. void * large_value;
  336. typename boost::aligned_storage<OptimizeForSize, OptimizeForAlignment>::type small_value;
  337. } content;
  338. };
  339. template<std::size_t OptimizeForSize, std::size_t OptimizeForAlignment>
  340. void swap(basic_any<OptimizeForSize, OptimizeForAlignment>& lhs, basic_any<OptimizeForSize, OptimizeForAlignment>& rhs) BOOST_NOEXCEPT
  341. {
  342. lhs.swap(rhs);
  343. }
  344. template<typename ValueType, std::size_t Size, std::size_t Alignment>
  345. ValueType * any_cast(basic_any<Size, Alignment> * operand) BOOST_NOEXCEPT
  346. {
  347. return operand->man ?
  348. static_cast<typename remove_cv<ValueType>::type *>(operand->man(basic_any<Size, Alignment>::AnyCast, *operand, 0, &boost::typeindex::type_id<ValueType>().type_info()))
  349. : 0;
  350. }
  351. template<typename ValueType, std::size_t OptimizeForSize, std::size_t OptimizeForAlignment>
  352. inline const ValueType * any_cast(const basic_any<OptimizeForSize, OptimizeForAlignment> * operand) BOOST_NOEXCEPT
  353. {
  354. return any_cast<ValueType>(const_cast<basic_any<OptimizeForSize, OptimizeForAlignment> *>(operand));
  355. }
  356. template<typename ValueType, std::size_t OptimizeForSize, std::size_t OptimizeForAlignment>
  357. ValueType any_cast(basic_any<OptimizeForSize, OptimizeForAlignment> & operand)
  358. {
  359. typedef typename remove_reference<ValueType>::type nonref;
  360. nonref * result = any_cast<nonref>(boost::addressof(operand));
  361. if(!result)
  362. boost::throw_exception(bad_any_cast());
  363. // Attempt to avoid construction of a temporary object in cases when
  364. // `ValueType` is not a reference. Example:
  365. // `static_cast<std::string>(*result);`
  366. // which is equal to `std::string(*result);`
  367. typedef typename boost::conditional<
  368. boost::is_reference<ValueType>::value,
  369. ValueType,
  370. typename boost::add_reference<ValueType>::type
  371. >::type ref_type;
  372. #ifdef BOOST_MSVC
  373. # pragma warning(push)
  374. # pragma warning(disable: 4172) // "returning address of local variable or temporary" but *result is not local!
  375. #endif
  376. return static_cast<ref_type>(*result);
  377. #ifdef BOOST_MSVC
  378. # pragma warning(pop)
  379. #endif
  380. }
  381. template<typename ValueType, std::size_t OptimizeForSize, std::size_t OptimizeForAlignment>
  382. inline ValueType any_cast(const basic_any<OptimizeForSize, OptimizeForAlignment> & operand)
  383. {
  384. typedef typename remove_reference<ValueType>::type nonref;
  385. return any_cast<const nonref &>(const_cast<basic_any<OptimizeForSize, OptimizeForAlignment> &>(operand));
  386. }
  387. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  388. template<typename ValueType, std::size_t OptimizeForSize, std::size_t OptimizeForAlignment>
  389. inline ValueType any_cast(basic_any<OptimizeForSize, OptimizeForAlignment>&& operand)
  390. {
  391. BOOST_STATIC_ASSERT_MSG(
  392. boost::is_rvalue_reference<ValueType&&>::value /*true if ValueType is rvalue or just a value*/
  393. || boost::is_const< typename boost::remove_reference<ValueType>::type >::value,
  394. "boost::any_cast shall not be used for getting nonconst references to temporary objects"
  395. );
  396. return any_cast<ValueType>(operand);
  397. }
  398. #endif
  399. // Note: The "unsafe" versions of any_cast are not part of the
  400. // public interface and may be removed at any time. They are
  401. // required where we know what type is stored in the any and can't
  402. // use typeid() comparison, e.g., when our types may travel across
  403. // different shared libraries.
  404. template<typename ValueType, std::size_t OptimizedForSize, std::size_t OptimizeForAlignment>
  405. inline ValueType * unsafe_any_cast(basic_any<OptimizedForSize, OptimizeForAlignment> * operand) BOOST_NOEXCEPT
  406. {
  407. return static_cast<ValueType*>(operand->man(basic_any<OptimizedForSize, OptimizeForAlignment>::UnsafeCast, *operand, 0, 0));
  408. }
  409. template<typename ValueType, std::size_t OptimizeForSize, std::size_t OptimizeForAlignment>
  410. inline const ValueType * unsafe_any_cast(const basic_any<OptimizeForSize, OptimizeForAlignment> * operand) BOOST_NOEXCEPT
  411. {
  412. return unsafe_any_cast<ValueType>(const_cast<basic_any<OptimizeForSize, OptimizeForAlignment> *>(operand));
  413. }
  414. } // namespace anys
  415. using boost::anys::any_cast;
  416. using boost::anys::unsafe_any_cast;
  417. } // namespace boost
  418. #endif // #ifndef BOOST_ANYS_BASIC_ANY_HPP_INCLUDED