any.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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 INFER_SERVER_ANY_H_
  28. #define INFER_SERVER_ANY_H_
  29. #include <memory>
  30. #include <new>
  31. #include <typeinfo>
  32. #include <type_traits>
  33. #include <iostream>
  34. #include <cstdlib>
  35. #include <utility>
  36. namespace infer_server {
  37. template< class T >
  38. using decay_t = typename std::decay<T>::type;
  39. template< bool B, class T = void >
  40. using enable_if_t = typename std::enable_if<B,T>::type;
  41. template< class T >
  42. using add_const_t = typename std::add_const<T>::type;
  43. template< bool B, class T, class F >
  44. using conditional_t = typename std::conditional<B,T,F>::type;
  45. template< std::size_t Len, std::size_t Align >
  46. using aligned_storage_t = typename std::aligned_storage<Len, Align>::type;
  47. template< class T >
  48. using add_pointer_t = typename std::add_pointer<T>::type;
  49. template <class _Tp>
  50. struct in_place_type_t {
  51. explicit in_place_type_t() = default;
  52. };
  53. class bad_any_cast : public std::bad_cast {
  54. public:
  55. const char* what() const noexcept override { return "bad any cast"; }
  56. };
  57. template <class _Alloc>
  58. class __allocator_destructor {
  59. typedef std::allocator_traits<_Alloc> __alloc_traits;
  60. public:
  61. typedef typename __alloc_traits::pointer pointer;
  62. typedef typename __alloc_traits::size_type size_type;
  63. private:
  64. _Alloc& __alloc_;
  65. size_type __s_;
  66. public:
  67. __allocator_destructor(_Alloc& __a, size_type __s) : __alloc_(__a), __s_(__s) {}
  68. void operator()(pointer __p) {__alloc_traits::deallocate(__alloc_, __p, __s_);}
  69. };
  70. template <class _Tp>
  71. struct __uncvref {
  72. typedef typename std::remove_cv<typename std::remove_reference<_Tp>::type>::type type;
  73. };
  74. template <class _Tp>
  75. using __uncvref_t = typename __uncvref<_Tp>::type;
  76. template <class _Tp> struct __is_inplace_type_imp : std::false_type {};
  77. // template <class _Tp> struct __is_inplace_type_imp<in_place_type_t<_Tp>> : true_type {};
  78. template <class _Tp>
  79. using __is_inplace_type = __is_inplace_type_imp<__uncvref_t<_Tp>>;
  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. /**
  118. * @brief Same as std::any
  119. */
  120. class any final {
  121. public:
  122. // construct/destruct
  123. constexpr any() : __h(nullptr) {}
  124. any(any const & __other) : __h(nullptr) {
  125. if (__other.__h) __other.__call(_Action::_Copy, this);
  126. }
  127. any(any && __other) : __h(nullptr) {
  128. if (__other.__h) __other.__call(_Action::_Move, this);
  129. }
  130. template <class _ValueType, class _Tp = decay_t<_ValueType>,
  131. class = enable_if_t< !std::is_same<_Tp, any>::value &&
  132. !__is_inplace_type<_ValueType>::value &&
  133. std::is_copy_constructible<_Tp>::value>>
  134. any(_ValueType && __value);
  135. template <class _ValueType, class ..._Args,
  136. class _Tp = decay_t<_ValueType>,
  137. class = enable_if_t<std::is_constructible<_Tp, _Args...>::value &&
  138. std::is_copy_constructible<_Tp>::value>>
  139. explicit any(in_place_type_t<_ValueType>, _Args&&... __args);
  140. template <class _ValueType, class _Up, class ..._Args,
  141. class _Tp = decay_t<_ValueType>,
  142. class = enable_if_t<
  143. std::is_constructible<_Tp, std::initializer_list<_Up>&, _Args...>::value &&
  144. std::is_copy_constructible<_Tp>::value>>
  145. explicit any(in_place_type_t<_ValueType>, std::initializer_list<_Up>, _Args&&... __args);
  146. ~any() { this->reset(); }
  147. // assignments
  148. any & operator=(any const & __rhs) {
  149. any(__rhs).swap(*this);
  150. return *this;
  151. }
  152. any & operator=(any && __rhs) {
  153. any(std::move(__rhs)).swap(*this);
  154. return *this;
  155. }
  156. template <class _ValueType,
  157. class _Tp = decay_t<_ValueType>,
  158. class = enable_if_t< !std::is_same<_Tp, any>::value &&
  159. std::is_copy_constructible<_Tp>::value>>
  160. any & operator=(_ValueType && __rhs);
  161. template <class _ValueType,
  162. class ..._Args,
  163. class _Tp = decay_t<_ValueType>,
  164. class = enable_if_t< std::is_constructible<_Tp, _Args...>::value &&
  165. std::is_copy_constructible<_Tp>::value>>
  166. _Tp& emplace(_Args&&... args);
  167. template <class _ValueType,
  168. class _Up,
  169. class ..._Args,
  170. class _Tp = decay_t<_ValueType>,
  171. class = enable_if_t<std::is_constructible<_Tp, std::initializer_list<_Up>&, _Args...>::value &&
  172. std::is_copy_constructible<_Tp>::value>>
  173. _Tp& emplace(std::initializer_list<_Up>, _Args&&...);
  174. // 6.3.3 any modifiers
  175. void reset() { if (__h) this->__call(_Action::_Destroy); }
  176. void swap(any & __rhs) {
  177. if (this == &__rhs)
  178. return;
  179. if (__h && __rhs.__h) {
  180. any __tmp;
  181. __rhs.__call(_Action::_Move, &__tmp);
  182. this->__call(_Action::_Move, &__rhs);
  183. __tmp.__call(_Action::_Move, this);
  184. }
  185. else if (__h) {
  186. this->__call(_Action::_Move, &__rhs);
  187. }
  188. else if (__rhs.__h) {
  189. __rhs.__call(_Action::_Move, this);
  190. }
  191. }
  192. // 6.3.4 any observers
  193. bool has_value() const { return __h != nullptr; }
  194. #if !defined(_LIBCPP_NO_RTTI)
  195. const std::type_info & type() const {
  196. if (__h) {
  197. return *static_cast<std::type_info const *>(this->__call(_Action::_TypeInfo));
  198. } else {
  199. return typeid(void);
  200. }
  201. }
  202. #endif
  203. private:
  204. typedef __any_imp::_Action _Action;
  205. using _HandleFuncPtr = void* (*)(_Action, any const *, any *, const std::type_info *,
  206. const void* __fallback_info);
  207. union _Storage {
  208. constexpr _Storage() : __ptr(nullptr) {}
  209. void* __ptr;
  210. __any_imp::_Buffer __buf;
  211. };
  212. void* __call(_Action __a, any * __other = nullptr,
  213. std::type_info const * __info = nullptr,
  214. const void* __fallback_info = nullptr) const {
  215. return __h(__a, this, __other, __info, __fallback_info);
  216. }
  217. void* __call(_Action __a, any * __other = nullptr,
  218. std::type_info const * __info = nullptr,
  219. const void* __fallback_info = nullptr) {
  220. return __h(__a, this, __other, __info, __fallback_info);
  221. }
  222. template <class>
  223. friend struct __any_imp::_SmallHandler;
  224. template <class>
  225. friend struct __any_imp::_LargeHandler;
  226. template <class _ValueType>
  227. friend add_pointer_t<add_const_t<_ValueType>> any_cast(any const *);
  228. template <class _ValueType>
  229. friend add_pointer_t<_ValueType> any_cast(any *);
  230. _HandleFuncPtr __h = nullptr;
  231. _Storage __s;
  232. };
  233. namespace __any_imp {
  234. template <class _Tp>
  235. struct _SmallHandler {
  236. static void* __handle(_Action __act, any const * __this, any * __other,
  237. std::type_info const * __info, const void* __fallback_info) {
  238. switch (__act) {
  239. case _Action::_Destroy:
  240. __destroy(const_cast<any &>(*__this));
  241. return nullptr;
  242. case _Action::_Copy:
  243. __copy(*__this, *__other);
  244. return nullptr;
  245. case _Action::_Move:
  246. __move(const_cast<any &>(*__this), *__other);
  247. return nullptr;
  248. case _Action::_Get:
  249. return __get(const_cast<any &>(*__this), __info, __fallback_info);
  250. case _Action::_TypeInfo:
  251. return __type_info();
  252. }
  253. return nullptr;
  254. }
  255. template <class ..._Args>
  256. static _Tp& __create(any & __dest, _Args&&... __args) {
  257. _Tp* __ret = ::new (static_cast<void*>(&__dest.__s.__buf)) _Tp(std::forward<_Args>(__args)...);
  258. __dest.__h = &_SmallHandler::__handle;
  259. return *__ret;
  260. }
  261. private:
  262. static void __destroy(any & __this) {
  263. _Tp & __value = *static_cast<_Tp *>(static_cast<void*>(&__this.__s.__buf));
  264. __value.~_Tp();
  265. __this.__h = nullptr;
  266. }
  267. static void __copy(any const & __this, any & __dest) {
  268. _SmallHandler::__create(__dest, *static_cast<_Tp const *>(
  269. static_cast<void const *>(&__this.__s.__buf)));
  270. }
  271. static void __move(any & __this, any & __dest) {
  272. _SmallHandler::__create(__dest, std::move(
  273. *static_cast<_Tp*>(static_cast<void*>(&__this.__s.__buf))));
  274. __destroy(__this);
  275. }
  276. static void* __get(any & __this,
  277. std::type_info const * __info,
  278. const void* __fallback_id) {
  279. if (__any_imp::__compare_typeid<_Tp>(__info, __fallback_id))
  280. return static_cast<void*>(&__this.__s.__buf);
  281. return nullptr;
  282. }
  283. static void* __type_info() {
  284. #if !defined(_LIBCPP_NO_RTTI)
  285. return const_cast<void*>(static_cast<void const *>(&typeid(_Tp)));
  286. #else
  287. return nullptr;
  288. #endif
  289. }
  290. };
  291. template <class _Tp>
  292. struct _LargeHandler {
  293. static void* __handle(_Action __act, any const * __this,
  294. any * __other, std::type_info const * __info,
  295. void const* __fallback_info) {
  296. switch (__act) {
  297. case _Action::_Destroy:
  298. __destroy(const_cast<any &>(*__this));
  299. return nullptr;
  300. case _Action::_Copy:
  301. __copy(*__this, *__other);
  302. return nullptr;
  303. case _Action::_Move:
  304. __move(const_cast<any &>(*__this), *__other);
  305. return nullptr;
  306. case _Action::_Get:
  307. return __get(const_cast<any &>(*__this), __info, __fallback_info);
  308. case _Action::_TypeInfo:
  309. return __type_info();
  310. }
  311. return nullptr;
  312. }
  313. template <class ..._Args>
  314. static _Tp& __create(any & __dest, _Args&&... __args) {
  315. typedef std::allocator<_Tp> _Alloc;
  316. typedef __allocator_destructor<_Alloc> _Dp;
  317. _Alloc __a;
  318. std::unique_ptr<_Tp, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
  319. _Tp* __ret = ::new ((void*)__hold.get()) _Tp(std::forward<_Args>(__args)...);
  320. __dest.__s.__ptr = __hold.release();
  321. __dest.__h = &_LargeHandler::__handle;
  322. return *__ret;
  323. }
  324. private:
  325. static void __destroy(any & __this) {
  326. delete static_cast<_Tp*>(__this.__s.__ptr);
  327. __this.__h = nullptr;
  328. }
  329. static void __copy(any const & __this, any & __dest) {
  330. _LargeHandler::__create(__dest, *static_cast<_Tp const *>(__this.__s.__ptr));
  331. }
  332. static void __move(any & __this, any & __dest) {
  333. __dest.__s.__ptr = __this.__s.__ptr;
  334. __dest.__h = &_LargeHandler::__handle;
  335. __this.__h = nullptr;
  336. }
  337. static void* __get(any & __this, std::type_info const * __info,
  338. void const* __fallback_info) {
  339. if (__any_imp::__compare_typeid<_Tp>(__info, __fallback_info))
  340. return static_cast<void*>(__this.__s.__ptr);
  341. return nullptr;
  342. }
  343. static void* __type_info() {
  344. #if !defined(_LIBCPP_NO_RTTI)
  345. return const_cast<void*>(static_cast<void const *>(&typeid(_Tp)));
  346. #else
  347. return nullptr;
  348. #endif
  349. }
  350. };
  351. } // namespace __any_imp
  352. template <class _ValueType, class _Tp, class>
  353. any::any(_ValueType && __v) : __h(nullptr) {
  354. __any_imp::_Handler<_Tp>::__create(*this, std::forward<_ValueType>(__v));
  355. }
  356. template <class _ValueType, class ..._Args, class _Tp, class>
  357. any::any(in_place_type_t<_ValueType>, _Args&&... __args) {
  358. __any_imp::_Handler<_Tp>::__create(*this, std::forward<_Args>(__args)...);
  359. }
  360. template <class _ValueType, class _Up, class ..._Args, class _Tp, class>
  361. any::any(in_place_type_t<_ValueType>, std::initializer_list<_Up> __il, _Args&&... __args) {
  362. __any_imp::_Handler<_Tp>::__create(*this, __il, std::forward<_Args>(__args)...);
  363. }
  364. template <class _ValueType, class, class>
  365. any& any::operator=(_ValueType && __v) {
  366. any(std::forward<_ValueType>(__v)).swap(*this);
  367. return *this;
  368. }
  369. template <class _ValueType, class ..._Args, class _Tp, class>
  370. _Tp& any::emplace(_Args&&... __args) {
  371. reset();
  372. return __any_imp::_Handler<_Tp>::__create(*this, std::forward<_Args>(__args)...);
  373. }
  374. template <class _ValueType, class _Up, class ..._Args, class _Tp, class>
  375. _Tp& any::emplace(std::initializer_list<_Up> __il, _Args&&... __args) {
  376. reset();
  377. return __any_imp::_Handler<_Tp>::__create(*this, __il, std::forward<_Args>(__args)...);
  378. }
  379. // 6.4 Non-member functions
  380. /*
  381. void swap(any & __lhs, any & __rhs) {
  382. __lhs.swap(__rhs);
  383. }
  384. template <class _Tp, class ..._Args>
  385. any make_any(_Args&&... __args) {
  386. return any(in_place_type<_Tp>, std::forward<_Args>(__args)...);
  387. }
  388. template <class _Tp, class _Up, class ..._Args>
  389. any make_any(initializer_list<_Up> __il, _Args&&... __args) {
  390. return any(in_place_type<_Tp>, __il, std::forward<_Args>(__args)...);
  391. }
  392. */
  393. template <class _ValueType>
  394. _ValueType any_cast(any const & __v) {
  395. using _RawValueType = __uncvref_t<_ValueType>;
  396. static_assert(std::is_constructible<_ValueType, _RawValueType const &>::value,
  397. "ValueType is required to be a const lvalue reference "
  398. "or a CopyConstructible type");
  399. auto __tmp = any_cast<add_const_t<_RawValueType>>(&__v);
  400. if (__tmp == nullptr)
  401. throw bad_any_cast();
  402. return static_cast<_ValueType>(*__tmp);
  403. }
  404. template <class _ValueType>
  405. _ValueType any_cast(any & __v) {
  406. using _RawValueType = __uncvref_t<_ValueType>;
  407. static_assert(std::is_constructible<_ValueType, _RawValueType &>::value,
  408. "ValueType is required to be an lvalue reference "
  409. "or a CopyConstructible type");
  410. auto __tmp = any_cast<_RawValueType>(&__v);
  411. if (__tmp == nullptr)
  412. throw bad_any_cast();
  413. return static_cast<_ValueType>(*__tmp);
  414. }
  415. template <class _ValueType>
  416. _ValueType any_cast(any && __v) {
  417. using _RawValueType = __uncvref_t<_ValueType>;
  418. static_assert(std::is_constructible<_ValueType, _RawValueType>::value,
  419. "ValueType is required to be an rvalue reference "
  420. "or a CopyConstructible type");
  421. auto __tmp = any_cast<_RawValueType>(&__v);
  422. if (__tmp == nullptr)
  423. throw bad_any_cast();
  424. return static_cast<_ValueType>(std::move(*__tmp));
  425. }
  426. template <class _ValueType>
  427. add_pointer_t<add_const_t<_ValueType>> any_cast(any const * __any) {
  428. static_assert(!std::is_reference<_ValueType>::value,
  429. "_ValueType may not be a reference.");
  430. return any_cast<_ValueType>(const_cast<any *>(__any));
  431. }
  432. template <class _RetType>
  433. _RetType __pointer_or_func_cast(void* __p, /*IsFunction*/std::false_type) noexcept {
  434. return static_cast<_RetType>(__p);
  435. }
  436. template <class _RetType>
  437. _RetType __pointer_or_func_cast(void*, /*IsFunction*/std::true_type) noexcept {
  438. return nullptr;
  439. }
  440. template <class _ValueType>
  441. add_pointer_t<_ValueType> any_cast(any * __any) {
  442. using __any_imp::_Action;
  443. static_assert(!std::is_reference<_ValueType>::value,
  444. "_ValueType may not be a reference.");
  445. typedef typename std::add_pointer<_ValueType>::type _ReturnType;
  446. if (__any && __any->__h) {
  447. void *__p = __any->__call(_Action::_Get, nullptr,
  448. #if !defined(_LIBCPP_NO_RTTI)
  449. &typeid(_ValueType),
  450. #else
  451. nullptr,
  452. #endif
  453. __any_imp::__get_fallback_typeid<_ValueType>());
  454. // add domain to protect from unintended ADL
  455. return infer_server::__pointer_or_func_cast<_ReturnType>(
  456. __p, std::is_function<_ValueType>{});
  457. }
  458. return nullptr;
  459. }
  460. } // namespace infer_server
  461. #endif // INFER_SERVER_ANY_H_