any 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. // <experimental/any> -*- C++ -*-
  2. // Copyright (C) 2014-2015 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file experimental/any
  21. * This is a TS C++ Library header.
  22. */
  23. #ifndef _GLIBCXX_EXPERIMENTAL_ANY
  24. #define _GLIBCXX_EXPERIMENTAL_ANY 1
  25. #pragma GCC system_header
  26. #if __cplusplus <= 201103L
  27. # include <bits/c++14_warning.h>
  28. #else
  29. #include <typeinfo>
  30. #include <new>
  31. #include <utility>
  32. #include <type_traits>
  33. namespace std _GLIBCXX_VISIBILITY(default)
  34. {
  35. namespace experimental
  36. {
  37. inline namespace fundamentals_v1
  38. {
  39. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  40. /**
  41. * @defgroup any Type-safe container of any type
  42. * @ingroup experimental
  43. *
  44. * A type-safe container for single values of value types, as
  45. * described in n3804 "Any Library Proposal (Revision 3)".
  46. *
  47. * @{
  48. */
  49. #define __cpp_lib_experimental_any 201411
  50. /**
  51. * @brief Exception class thrown by a failed @c any_cast
  52. * @ingroup exceptions
  53. */
  54. class bad_any_cast : public bad_cast
  55. {
  56. public:
  57. virtual const char* what() const noexcept { return "bad any_cast"; }
  58. };
  59. [[gnu::noreturn]] inline void __throw_bad_any_cast()
  60. {
  61. #if __cpp_exceptions
  62. throw bad_any_cast{};
  63. #else
  64. __builtin_abort();
  65. #endif
  66. }
  67. /**
  68. * @brief A type-safe container of any type.
  69. *
  70. * An @c any object's state is either empty or it stores a contained object
  71. * of CopyConstructible type.
  72. */
  73. class any
  74. {
  75. // Holds either pointer to a heap object or the contained object itself.
  76. union _Storage
  77. {
  78. void* _M_ptr;
  79. std::aligned_storage<sizeof(_M_ptr), sizeof(_M_ptr)>::type _M_buffer;
  80. };
  81. template<typename _Tp, typename _Safe = is_trivially_copyable<_Tp>,
  82. bool _Fits = (sizeof(_Tp) <= sizeof(_Storage))>
  83. using _Internal = std::integral_constant<bool, _Safe::value && _Fits>;
  84. template<typename _Tp>
  85. struct _Manager_internal; // uses small-object optimization
  86. template<typename _Tp>
  87. struct _Manager_external; // creates contained object on the heap
  88. template<typename _Tp>
  89. using _Manager = conditional_t<_Internal<_Tp>::value,
  90. _Manager_internal<_Tp>,
  91. _Manager_external<_Tp>>;
  92. template<typename _Tp, typename _Decayed = decay_t<_Tp>>
  93. using _Decay = enable_if_t<!is_same<_Decayed, any>::value, _Decayed>;
  94. public:
  95. // construct/destruct
  96. /// Default constructor, creates an empty object.
  97. any() noexcept : _M_manager(nullptr) { }
  98. /// Copy constructor, copies the state of @p __other
  99. any(const any& __other) : _M_manager(__other._M_manager)
  100. {
  101. if (!__other.empty())
  102. {
  103. _Arg __arg;
  104. __arg._M_any = this;
  105. _M_manager(_Op_clone, &__other, &__arg);
  106. }
  107. }
  108. /**
  109. * @brief Move constructor, transfer the state from @p __other
  110. *
  111. * @post @c __other.empty() (not guaranteed for other implementations)
  112. */
  113. any(any&& __other) noexcept
  114. : _M_manager(__other._M_manager),
  115. _M_storage(__other._M_storage)
  116. { __other._M_manager = nullptr; }
  117. /// Construct with a copy of @p __value as the contained object.
  118. template <typename _ValueType, typename _Tp = _Decay<_ValueType>,
  119. typename _Mgr = _Manager<_Tp>>
  120. any(_ValueType&& __value)
  121. : _M_manager(&_Mgr::_S_manage),
  122. _M_storage(_Mgr::_S_create(std::forward<_ValueType>(__value)))
  123. {
  124. static_assert(is_copy_constructible<_Tp>::value,
  125. "The contained object must be CopyConstructible");
  126. }
  127. /// Destructor, calls @c clear()
  128. ~any() { clear(); }
  129. // assignments
  130. /// Copy the state of
  131. any& operator=(const any& __rhs)
  132. {
  133. any(__rhs).swap(*this);
  134. return *this;
  135. }
  136. /**
  137. * @brief Move assignment operator
  138. *
  139. * @post @c __rhs.empty() (not guaranteed for other implementations)
  140. */
  141. any& operator=(any&& __rhs) noexcept
  142. {
  143. any(std::move(__rhs)).swap(*this);
  144. return *this;
  145. }
  146. /// Store a copy of @p __rhs as the contained object.
  147. template<typename _ValueType>
  148. any& operator=(_ValueType&& __rhs)
  149. {
  150. any(std::forward<_ValueType>(__rhs)).swap(*this);
  151. return *this;
  152. }
  153. // modifiers
  154. /// If not empty, destroy the contained object.
  155. void clear() noexcept
  156. {
  157. if (!empty())
  158. {
  159. _M_manager(_Op_destroy, this, nullptr);
  160. _M_manager = nullptr;
  161. }
  162. }
  163. /// Exchange state with another object.
  164. void swap(any& __rhs) noexcept
  165. {
  166. std::swap(_M_manager, __rhs._M_manager);
  167. std::swap(_M_storage, __rhs._M_storage);
  168. }
  169. // observers
  170. /// Reports whether there is a contained object or not.
  171. bool empty() const noexcept { return _M_manager == nullptr; }
  172. #if __cpp_rtti
  173. /// The @c typeid of the contained object, or @c typeid(void) if empty.
  174. const type_info& type() const noexcept
  175. {
  176. if (empty())
  177. return typeid(void);
  178. _Arg __arg;
  179. _M_manager(_Op_get_type_info, this, &__arg);
  180. return *__arg._M_typeinfo;
  181. }
  182. #endif
  183. template<typename _Tp>
  184. static constexpr bool __is_valid_cast()
  185. { return __or_<is_reference<_Tp>, is_copy_constructible<_Tp>>::value; }
  186. private:
  187. enum _Op { _Op_access, _Op_get_type_info, _Op_clone, _Op_destroy };
  188. union _Arg
  189. {
  190. void* _M_obj;
  191. const std::type_info* _M_typeinfo;
  192. any* _M_any;
  193. };
  194. void (*_M_manager)(_Op, const any*, _Arg*);
  195. _Storage _M_storage;
  196. template<typename _Tp>
  197. friend void* __any_caster(const any* __any)
  198. {
  199. if (__any->_M_manager != &_Manager<decay_t<_Tp>>::_S_manage)
  200. return nullptr;
  201. _Arg __arg;
  202. __any->_M_manager(_Op_access, __any, &__arg);
  203. return __arg._M_obj;
  204. }
  205. // Manage in-place contained object.
  206. template<typename _Tp>
  207. struct _Manager_internal
  208. {
  209. static void
  210. _S_manage(_Op __which, const any* __anyp, _Arg* __arg);
  211. template<typename _Up>
  212. static _Storage
  213. _S_create(_Up&& __value)
  214. {
  215. _Storage __storage;
  216. void* __addr = &__storage._M_buffer;
  217. ::new (__addr) _Tp(std::forward<_Up>(__value));
  218. return __storage;
  219. }
  220. template<typename _Alloc, typename _Up>
  221. static _Storage
  222. _S_alloc(const _Alloc&, _Up&& __value)
  223. {
  224. return _S_create(std::forward<_Up>(__value));
  225. }
  226. };
  227. // Manage external contained object.
  228. template<typename _Tp>
  229. struct _Manager_external
  230. {
  231. static void
  232. _S_manage(_Op __which, const any* __anyp, _Arg* __arg);
  233. template<typename _Up>
  234. static _Storage
  235. _S_create(_Up&& __value)
  236. {
  237. _Storage __storage;
  238. __storage._M_ptr = new _Tp(std::forward<_Up>(__value));
  239. return __storage;
  240. }
  241. };
  242. };
  243. /// Exchange the states of two @c any objects.
  244. inline void swap(any& __x, any& __y) noexcept { __x.swap(__y); }
  245. /**
  246. * @brief Access the contained object.
  247. *
  248. * @tparam _ValueType A const-reference or CopyConstructible type.
  249. * @param __any The object to access.
  250. * @return The contained object.
  251. * @throw bad_any_cast If <code>
  252. * __any.type() != typeid(remove_reference_t<_ValueType>)
  253. * </code>
  254. */
  255. template<typename _ValueType>
  256. inline _ValueType any_cast(const any& __any)
  257. {
  258. static_assert(any::__is_valid_cast<_ValueType>(),
  259. "Template argument must be a reference or CopyConstructible type");
  260. auto __p = any_cast<add_const_t<remove_reference_t<_ValueType>>>(&__any);
  261. if (__p)
  262. return *__p;
  263. __throw_bad_any_cast();
  264. }
  265. /**
  266. * @brief Access the contained object.
  267. *
  268. * @tparam _ValueType A reference or CopyConstructible type.
  269. * @param __any The object to access.
  270. * @return The contained object.
  271. * @throw bad_any_cast If <code>
  272. * __any.type() != typeid(remove_reference_t<_ValueType>)
  273. * </code>
  274. *
  275. * @{
  276. */
  277. template<typename _ValueType>
  278. inline _ValueType any_cast(any& __any)
  279. {
  280. static_assert(any::__is_valid_cast<_ValueType>(),
  281. "Template argument must be a reference or CopyConstructible type");
  282. auto __p = any_cast<remove_reference_t<_ValueType>>(&__any);
  283. if (__p)
  284. return *__p;
  285. __throw_bad_any_cast();
  286. }
  287. template<typename _ValueType>
  288. inline _ValueType any_cast(any&& __any)
  289. {
  290. static_assert(any::__is_valid_cast<_ValueType>(),
  291. "Template argument must be a reference or CopyConstructible type");
  292. auto __p = any_cast<remove_reference_t<_ValueType>>(&__any);
  293. if (__p)
  294. return *__p;
  295. __throw_bad_any_cast();
  296. }
  297. // @}
  298. /**
  299. * @brief Access the contained object.
  300. *
  301. * @tparam _ValueType The type of the contained object.
  302. * @param __any A pointer to the object to access.
  303. * @return The address of the contained object if <code>
  304. * __any != nullptr && __any.type() == typeid(_ValueType)
  305. * </code>, otherwise a null pointer.
  306. *
  307. * @{
  308. */
  309. template<typename _ValueType>
  310. inline const _ValueType* any_cast(const any* __any) noexcept
  311. {
  312. if (__any)
  313. return static_cast<_ValueType*>(__any_caster<_ValueType>(__any));
  314. return nullptr;
  315. }
  316. template<typename _ValueType>
  317. inline _ValueType* any_cast(any* __any) noexcept
  318. {
  319. if (__any)
  320. return static_cast<_ValueType*>(__any_caster<_ValueType>(__any));
  321. return nullptr;
  322. }
  323. // @}
  324. template<typename _Tp>
  325. void
  326. any::_Manager_internal<_Tp>::
  327. _S_manage(_Op __which, const any* __any, _Arg* __arg)
  328. {
  329. // The contained object is in _M_storage._M_buffer
  330. auto __ptr = reinterpret_cast<const _Tp*>(&__any->_M_storage._M_buffer);
  331. switch (__which)
  332. {
  333. case _Op_access:
  334. __arg->_M_obj = const_cast<_Tp*>(__ptr);
  335. break;
  336. case _Op_get_type_info:
  337. #if __cpp_rtti
  338. __arg->_M_typeinfo = &typeid(_Tp);
  339. #endif
  340. break;
  341. case _Op_clone:
  342. ::new(&__arg->_M_any->_M_storage._M_buffer) _Tp(*__ptr);
  343. break;
  344. case _Op_destroy:
  345. __ptr->~_Tp();
  346. break;
  347. }
  348. }
  349. template<typename _Tp>
  350. void
  351. any::_Manager_external<_Tp>::
  352. _S_manage(_Op __which, const any* __any, _Arg* __arg)
  353. {
  354. // The contained object is *_M_storage._M_ptr
  355. auto __ptr = static_cast<const _Tp*>(__any->_M_storage._M_ptr);
  356. switch (__which)
  357. {
  358. case _Op_access:
  359. __arg->_M_obj = const_cast<_Tp*>(__ptr);
  360. break;
  361. case _Op_get_type_info:
  362. #if __cpp_rtti
  363. __arg->_M_typeinfo = &typeid(_Tp);
  364. #endif
  365. break;
  366. case _Op_clone:
  367. __arg->_M_any->_M_storage._M_ptr = new _Tp(*__ptr);
  368. break;
  369. case _Op_destroy:
  370. delete __ptr;
  371. break;
  372. }
  373. }
  374. // @} group any
  375. _GLIBCXX_END_NAMESPACE_VERSION
  376. } // namespace fundamentals_v1
  377. } // namespace experimental
  378. } // namespace std
  379. #endif // C++14
  380. #endif // _GLIBCXX_EXPERIMENTAL_ANY