cnstream_any.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. /*************************************************************************
  2. * Copyright (C) [2019] by Cambricon, Inc. All rights reserved
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  13. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  15. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. * THE SOFTWARE.
  19. *************************************************************************/
  20. //===------------------------------ any -----------------------------------===//
  21. //
  22. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  23. // See https://llvm.org/LICENSE.txt for license information.
  24. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  25. //
  26. //===----------------------------------------------------------------------===//
  27. #ifndef LINB_ANY_HPP
  28. #define LINB_ANY_HPP
  29. #include <memory>
  30. #include <new>
  31. #include <typeinfo>
  32. #include <type_traits>
  33. #include <iostream>
  34. #include <cstdlib>
  35. #include <utility>
  36. template< class T >
  37. using decay_t = typename std::decay<T>::type;
  38. template< bool B, class T = void >
  39. using enable_if_t = typename std::enable_if<B,T>::type;
  40. template< class T >
  41. using add_const_t = typename std::add_const<T>::type;
  42. template< bool B, class T, class F >
  43. using conditional_t = typename std::conditional<B,T,F>::type;
  44. template< std::size_t Len, std::size_t Align >
  45. using aligned_storage_t = typename std::aligned_storage<Len, Align>::type;
  46. template< class T >
  47. using add_pointer_t = typename std::add_pointer<T>::type;
  48. template <class _Tp>
  49. struct in_place_type_t {
  50. explicit in_place_type_t() = default;
  51. };
  52. class bad_any_cast : public std::bad_cast {
  53. public:
  54. const char* what() const noexcept override { return "bad any cast"; }
  55. };
  56. template <class _Alloc>
  57. class __allocator_destructor {
  58. typedef std::allocator_traits<_Alloc> __alloc_traits;
  59. public:
  60. typedef typename __alloc_traits::pointer pointer;
  61. typedef typename __alloc_traits::size_type size_type;
  62. private:
  63. _Alloc& __alloc_;
  64. size_type __s_;
  65. public:
  66. __allocator_destructor(_Alloc& __a, size_type __s) : __alloc_(__a), __s_(__s) {}
  67. void operator()(pointer __p) {__alloc_traits::deallocate(__alloc_, __p, __s_);}
  68. };
  69. template <class _Tp>
  70. struct __uncvref {
  71. typedef typename std::remove_cv<typename std::remove_reference<_Tp>::type>::type type;
  72. };
  73. template <class _Tp>
  74. using __uncvref_t = typename __uncvref<_Tp>::type;
  75. template <class _Tp> struct __is_inplace_type_imp : std::false_type {};
  76. // template <class _Tp> struct __is_inplace_type_imp<in_place_type_t<_Tp>> : true_type {};
  77. template <class _Tp>
  78. using __is_inplace_type = __is_inplace_type_imp<__uncvref_t<_Tp>>;
  79. namespace cnstream {
  80. namespace __any_imp {
  81. using _Buffer = aligned_storage_t<3*sizeof(void*), std::alignment_of<void*>::value>;
  82. template <class _Tp>
  83. using _IsSmallObject = std::integral_constant<bool,
  84. sizeof(_Tp) <= sizeof(_Buffer) &&
  85. std::alignment_of<_Buffer>::value % std::alignment_of<_Tp>::value == 0 &&
  86. std::is_nothrow_move_constructible<_Tp>::value>;
  87. enum class _Action {
  88. _Destroy,
  89. _Copy,
  90. _Move,
  91. _Get,
  92. _TypeInfo
  93. };
  94. template <class _Tp> struct _SmallHandler;
  95. template <class _Tp> struct _LargeHandler;
  96. template <class _Tp>
  97. struct __unique_typeinfo { static constexpr int __id = 0; };
  98. template <class _Tp> constexpr int __unique_typeinfo<_Tp>::__id;
  99. template <class _Tp>
  100. constexpr const void* __get_fallback_typeid() {
  101. return &__unique_typeinfo<decay_t<_Tp>>::__id;
  102. }
  103. template <class _Tp>
  104. bool __compare_typeid(std::type_info const* __id, const void* __fallback_id) {
  105. #if !defined(_LIBCPP_NO_RTTI)
  106. if (__id && *__id == typeid(_Tp))
  107. return true;
  108. #endif
  109. if (!__id && __fallback_id == __any_imp::__get_fallback_typeid<_Tp>())
  110. return true;
  111. return false;
  112. }
  113. template <class _Tp>
  114. using _Handler = conditional_t<
  115. _IsSmallObject<_Tp>::value, _SmallHandler<_Tp>, _LargeHandler<_Tp>>;
  116. } // namespace __any_imp
  117. class any final {
  118. public:
  119. // construct/destruct
  120. constexpr any() : __h(nullptr) {}
  121. any(any const & __other) : __h(nullptr) {
  122. if (__other.__h) __other.__call(_Action::_Copy, this);
  123. }
  124. any(any && __other) : __h(nullptr) {
  125. if (__other.__h) __other.__call(_Action::_Move, this);
  126. }
  127. template <class _ValueType, class _Tp = decay_t<_ValueType>,
  128. class = enable_if_t< !std::is_same<_Tp, any>::value &&
  129. !__is_inplace_type<_ValueType>::value &&
  130. std::is_copy_constructible<_Tp>::value>>
  131. any(_ValueType && __value);
  132. template <class _ValueType, class ..._Args,
  133. class _Tp = decay_t<_ValueType>,
  134. class = enable_if_t<std::is_constructible<_Tp, _Args...>::value &&
  135. std::is_copy_constructible<_Tp>::value>>
  136. explicit any(in_place_type_t<_ValueType>, _Args&&... __args);
  137. template <class _ValueType, class _Up, class ..._Args,
  138. class _Tp = decay_t<_ValueType>,
  139. class = enable_if_t<
  140. std::is_constructible<_Tp, std::initializer_list<_Up>&, _Args...>::value &&
  141. std::is_copy_constructible<_Tp>::value>>
  142. explicit any(in_place_type_t<_ValueType>, std::initializer_list<_Up>, _Args&&... __args);
  143. ~any() { this->reset(); }
  144. // assignments
  145. any & operator=(any const & __rhs) {
  146. any(__rhs).swap(*this);
  147. return *this;
  148. }
  149. any & operator=(any && __rhs) {
  150. any(std::move(__rhs)).swap(*this);
  151. return *this;
  152. }
  153. template <class _ValueType,
  154. class _Tp = decay_t<_ValueType>,
  155. class = enable_if_t< !std::is_same<_Tp, any>::value &&
  156. std::is_copy_constructible<_Tp>::value>>
  157. any & operator=(_ValueType && __rhs);
  158. template <class _ValueType,
  159. class ..._Args,
  160. class _Tp = decay_t<_ValueType>,
  161. class = enable_if_t< std::is_constructible<_Tp, _Args...>::value &&
  162. std::is_copy_constructible<_Tp>::value>>
  163. _Tp& emplace(_Args&&... args);
  164. template <class _ValueType,
  165. class _Up,
  166. class ..._Args,
  167. class _Tp = decay_t<_ValueType>,
  168. class = enable_if_t<std::is_constructible<_Tp, std::initializer_list<_Up>&, _Args...>::value &&
  169. std::is_copy_constructible<_Tp>::value>>
  170. _Tp& emplace(std::initializer_list<_Up>, _Args&&...);
  171. // 6.3.3 any modifiers
  172. void reset() { if (__h) this->__call(_Action::_Destroy); }
  173. void swap(any & __rhs) {
  174. if (this == &__rhs)
  175. return;
  176. if (__h && __rhs.__h) {
  177. any __tmp;
  178. __rhs.__call(_Action::_Move, &__tmp);
  179. this->__call(_Action::_Move, &__rhs);
  180. __tmp.__call(_Action::_Move, this);
  181. }
  182. else if (__h) {
  183. this->__call(_Action::_Move, &__rhs);
  184. }
  185. else if (__rhs.__h) {
  186. __rhs.__call(_Action::_Move, this);
  187. }
  188. }
  189. // 6.3.4 any observers
  190. bool has_value() const { return __h != nullptr; }
  191. #if !defined(_LIBCPP_NO_RTTI)
  192. const std::type_info & type() const {
  193. if (__h) {
  194. return *static_cast<std::type_info const *>(this->__call(_Action::_TypeInfo));
  195. } else {
  196. return typeid(void);
  197. }
  198. }
  199. #endif
  200. private:
  201. typedef __any_imp::_Action _Action;
  202. using _HandleFuncPtr = void* (*)(_Action, any const *, any *, const std::type_info *,
  203. const void* __fallback_info);
  204. union _Storage {
  205. constexpr _Storage() : __ptr(nullptr) {}
  206. void* __ptr;
  207. __any_imp::_Buffer __buf;
  208. };
  209. void* __call(_Action __a, any * __other = nullptr,
  210. std::type_info const * __info = nullptr,
  211. const void* __fallback_info = nullptr) const {
  212. return __h(__a, this, __other, __info, __fallback_info);
  213. }
  214. void* __call(_Action __a, any * __other = nullptr,
  215. std::type_info const * __info = nullptr,
  216. const void* __fallback_info = nullptr) {
  217. return __h(__a, this, __other, __info, __fallback_info);
  218. }
  219. template <class>
  220. friend struct __any_imp::_SmallHandler;
  221. template <class>
  222. friend struct __any_imp::_LargeHandler;
  223. template <class _ValueType>
  224. friend add_pointer_t<add_const_t<_ValueType>> any_cast(any const *);
  225. template <class _ValueType>
  226. friend add_pointer_t<_ValueType> any_cast(any *);
  227. _HandleFuncPtr __h = nullptr;
  228. _Storage __s;
  229. };
  230. namespace __any_imp {
  231. template <class _Tp>
  232. struct _SmallHandler {
  233. static void* __handle(_Action __act, any const * __this, any * __other,
  234. std::type_info const * __info, const void* __fallback_info) {
  235. switch (__act) {
  236. case _Action::_Destroy:
  237. __destroy(const_cast<any &>(*__this));
  238. return nullptr;
  239. case _Action::_Copy:
  240. __copy(*__this, *__other);
  241. return nullptr;
  242. case _Action::_Move:
  243. __move(const_cast<any &>(*__this), *__other);
  244. return nullptr;
  245. case _Action::_Get:
  246. return __get(const_cast<any &>(*__this), __info, __fallback_info);
  247. case _Action::_TypeInfo:
  248. return __type_info();
  249. }
  250. return nullptr;
  251. }
  252. template <class ..._Args>
  253. static _Tp& __create(any & __dest, _Args&&... __args) {
  254. _Tp* __ret = ::new (static_cast<void*>(&__dest.__s.__buf)) _Tp(std::forward<_Args>(__args)...);
  255. __dest.__h = &_SmallHandler::__handle;
  256. return *__ret;
  257. }
  258. private:
  259. static void __destroy(any & __this) {
  260. _Tp & __value = *static_cast<_Tp *>(static_cast<void*>(&__this.__s.__buf));
  261. __value.~_Tp();
  262. __this.__h = nullptr;
  263. }
  264. static void __copy(any const & __this, any & __dest) {
  265. _SmallHandler::__create(__dest, *static_cast<_Tp const *>(
  266. static_cast<void const *>(&__this.__s.__buf)));
  267. }
  268. static void __move(any & __this, any & __dest) {
  269. _SmallHandler::__create(__dest, std::move(
  270. *static_cast<_Tp*>(static_cast<void*>(&__this.__s.__buf))));
  271. __destroy(__this);
  272. }
  273. static void* __get(any & __this,
  274. std::type_info const * __info,
  275. const void* __fallback_id) {
  276. if (__any_imp::__compare_typeid<_Tp>(__info, __fallback_id))
  277. return static_cast<void*>(&__this.__s.__buf);
  278. return nullptr;
  279. }
  280. static void* __type_info() {
  281. #if !defined(_LIBCPP_NO_RTTI)
  282. return const_cast<void*>(static_cast<void const *>(&typeid(_Tp)));
  283. #else
  284. return nullptr;
  285. #endif
  286. }
  287. };
  288. template <class _Tp>
  289. struct _LargeHandler {
  290. static void* __handle(_Action __act, any const * __this,
  291. any * __other, std::type_info const * __info,
  292. void const* __fallback_info) {
  293. switch (__act) {
  294. case _Action::_Destroy:
  295. __destroy(const_cast<any &>(*__this));
  296. return nullptr;
  297. case _Action::_Copy:
  298. __copy(*__this, *__other);
  299. return nullptr;
  300. case _Action::_Move:
  301. __move(const_cast<any &>(*__this), *__other);
  302. return nullptr;
  303. case _Action::_Get:
  304. return __get(const_cast<any &>(*__this), __info, __fallback_info);
  305. case _Action::_TypeInfo:
  306. return __type_info();
  307. }
  308. return nullptr;
  309. }
  310. template <class ..._Args>
  311. static _Tp& __create(any & __dest, _Args&&... __args) {
  312. typedef std::allocator<_Tp> _Alloc;
  313. typedef __allocator_destructor<_Alloc> _Dp;
  314. _Alloc __a;
  315. std::unique_ptr<_Tp, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
  316. _Tp* __ret = ::new ((void*)__hold.get()) _Tp(std::forward<_Args>(__args)...);
  317. __dest.__s.__ptr = __hold.release();
  318. __dest.__h = &_LargeHandler::__handle;
  319. return *__ret;
  320. }
  321. private:
  322. static void __destroy(any & __this) {
  323. delete static_cast<_Tp*>(__this.__s.__ptr);
  324. __this.__h = nullptr;
  325. }
  326. static void __copy(any const & __this, any & __dest) {
  327. _LargeHandler::__create(__dest, *static_cast<_Tp const *>(__this.__s.__ptr));
  328. }
  329. static void __move(any & __this, any & __dest) {
  330. __dest.__s.__ptr = __this.__s.__ptr;
  331. __dest.__h = &_LargeHandler::__handle;
  332. __this.__h = nullptr;
  333. }
  334. static void* __get(any & __this, std::type_info const * __info,
  335. void const* __fallback_info) {
  336. if (__any_imp::__compare_typeid<_Tp>(__info, __fallback_info))
  337. return static_cast<void*>(__this.__s.__ptr);
  338. return nullptr;
  339. }
  340. static void* __type_info() {
  341. #if !defined(_LIBCPP_NO_RTTI)
  342. return const_cast<void*>(static_cast<void const *>(&typeid(_Tp)));
  343. #else
  344. return nullptr;
  345. #endif
  346. }
  347. };
  348. } // namespace __any_imp
  349. template <class _ValueType, class _Tp, class>
  350. any::any(_ValueType && __v) : __h(nullptr) {
  351. __any_imp::_Handler<_Tp>::__create(*this, std::forward<_ValueType>(__v));
  352. }
  353. template <class _ValueType, class ..._Args, class _Tp, class>
  354. any::any(in_place_type_t<_ValueType>, _Args&&... __args) {
  355. __any_imp::_Handler<_Tp>::__create(*this, std::forward<_Args>(__args)...);
  356. }
  357. template <class _ValueType, class _Up, class ..._Args, class _Tp, class>
  358. any::any(in_place_type_t<_ValueType>, std::initializer_list<_Up> __il, _Args&&... __args) {
  359. __any_imp::_Handler<_Tp>::__create(*this, __il, std::forward<_Args>(__args)...);
  360. }
  361. template <class _ValueType, class, class>
  362. any& any::operator=(_ValueType && __v) {
  363. any(std::forward<_ValueType>(__v)).swap(*this);
  364. return *this;
  365. }
  366. template <class _ValueType, class ..._Args, class _Tp, class>
  367. _Tp& any::emplace(_Args&&... __args) {
  368. reset();
  369. return __any_imp::_Handler<_Tp>::__create(*this, std::forward<_Args>(__args)...);
  370. }
  371. template <class _ValueType, class _Up, class ..._Args, class _Tp, class>
  372. _Tp& any::emplace(std::initializer_list<_Up> __il, _Args&&... __args) {
  373. reset();
  374. return __any_imp::_Handler<_Tp>::__create(*this, __il, std::forward<_Args>(__args)...);
  375. }
  376. // 6.4 Non-member functions
  377. /*
  378. void swap(any & __lhs, any & __rhs) {
  379. __lhs.swap(__rhs);
  380. }
  381. template <class _Tp, class ..._Args>
  382. any make_any(_Args&&... __args) {
  383. return any(in_place_type<_Tp>, std::forward<_Args>(__args)...);
  384. }
  385. template <class _Tp, class _Up, class ..._Args>
  386. any make_any(initializer_list<_Up> __il, _Args&&... __args) {
  387. return any(in_place_type<_Tp>, __il, std::forward<_Args>(__args)...);
  388. }
  389. */
  390. template <class _ValueType>
  391. _ValueType any_cast(any const & __v) {
  392. using _RawValueType = __uncvref_t<_ValueType>;
  393. static_assert(std::is_constructible<_ValueType, _RawValueType const &>::value,
  394. "ValueType is required to be a const lvalue reference "
  395. "or a CopyConstructible type");
  396. auto __tmp = any_cast<add_const_t<_RawValueType>>(&__v);
  397. if (__tmp == nullptr)
  398. throw bad_any_cast();
  399. return static_cast<_ValueType>(*__tmp);
  400. }
  401. template <class _ValueType>
  402. _ValueType any_cast(any & __v) {
  403. using _RawValueType = __uncvref_t<_ValueType>;
  404. static_assert(std::is_constructible<_ValueType, _RawValueType &>::value,
  405. "ValueType is required to be an lvalue reference "
  406. "or a CopyConstructible type");
  407. auto __tmp = any_cast<_RawValueType>(&__v);
  408. if (__tmp == nullptr)
  409. throw bad_any_cast();
  410. return static_cast<_ValueType>(*__tmp);
  411. }
  412. template <class _ValueType>
  413. _ValueType any_cast(any && __v) {
  414. using _RawValueType = __uncvref_t<_ValueType>;
  415. static_assert(std::is_constructible<_ValueType, _RawValueType>::value,
  416. "ValueType is required to be an rvalue reference "
  417. "or a CopyConstructible type");
  418. auto __tmp = any_cast<_RawValueType>(&__v);
  419. if (__tmp == nullptr)
  420. throw bad_any_cast();
  421. return static_cast<_ValueType>(std::move(*__tmp));
  422. }
  423. template <class _ValueType>
  424. add_pointer_t<add_const_t<_ValueType>> any_cast(any const * __any) {
  425. static_assert(!std::is_reference<_ValueType>::value,
  426. "_ValueType may not be a reference.");
  427. return any_cast<_ValueType>(const_cast<any *>(__any));
  428. }
  429. template <class _RetType>
  430. _RetType __pointer_or_func_cast(void* __p, /*IsFunction*/std::false_type) noexcept {
  431. return static_cast<_RetType>(__p);
  432. }
  433. template <class _RetType>
  434. _RetType __pointer_or_func_cast(void*, /*IsFunction*/std::true_type) noexcept {
  435. return nullptr;
  436. }
  437. template <class _ValueType>
  438. add_pointer_t<_ValueType> any_cast(any * __any) {
  439. using __any_imp::_Action;
  440. static_assert(!std::is_reference<_ValueType>::value,
  441. "_ValueType may not be a reference.");
  442. typedef typename std::add_pointer<_ValueType>::type _ReturnType;
  443. if (__any && __any->__h) {
  444. void *__p = __any->__call(_Action::_Get, nullptr,
  445. #if !defined(_LIBCPP_NO_RTTI)
  446. &typeid(_ValueType),
  447. #else
  448. nullptr,
  449. #endif
  450. __any_imp::__get_fallback_typeid<_ValueType>());
  451. return __pointer_or_func_cast<_ReturnType>(
  452. __p, std::is_function<_ValueType>{});
  453. }
  454. return nullptr;
  455. }
  456. } // namespace cnstream
  457. #endif