libstdc++
|
00001 // <experimental/any> -*- C++ -*- 00002 00003 // Copyright (C) 2014-2016 Free Software Foundation, Inc. 00004 // 00005 // This file is part of the GNU ISO C++ Library. This library is free 00006 // software; you can redistribute it and/or modify it under the 00007 // terms of the GNU General Public License as published by the 00008 // Free Software Foundation; either version 3, or (at your option) 00009 // any later version. 00010 00011 // This library is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 00016 // Under Section 7 of GPL version 3, you are granted additional 00017 // permissions described in the GCC Runtime Library Exception, version 00018 // 3.1, as published by the Free Software Foundation. 00019 00020 // You should have received a copy of the GNU General Public License and 00021 // a copy of the GCC Runtime Library Exception along with this program; 00022 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00023 // <http://www.gnu.org/licenses/>. 00024 00025 /** @file experimental/any 00026 * This is a TS C++ Library header. 00027 */ 00028 00029 #ifndef _GLIBCXX_EXPERIMENTAL_ANY 00030 #define _GLIBCXX_EXPERIMENTAL_ANY 1 00031 00032 #pragma GCC system_header 00033 00034 #if __cplusplus <= 201103L 00035 # include <bits/c++14_warning.h> 00036 #else 00037 00038 #include <typeinfo> 00039 #include <new> 00040 #include <utility> 00041 #include <type_traits> 00042 #include <experimental/bits/lfts_config.h> 00043 00044 namespace std _GLIBCXX_VISIBILITY(default) 00045 { 00046 namespace experimental 00047 { 00048 inline namespace fundamentals_v1 00049 { 00050 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00051 00052 /** 00053 * @defgroup any Type-safe container of any type 00054 * @ingroup experimental 00055 * 00056 * A type-safe container for single values of value types, as 00057 * described in n3804 "Any Library Proposal (Revision 3)". 00058 * 00059 * @{ 00060 */ 00061 00062 #define __cpp_lib_experimental_any 201411 00063 00064 /** 00065 * @brief Exception class thrown by a failed @c any_cast 00066 * @ingroup exceptions 00067 */ 00068 class bad_any_cast : public bad_cast 00069 { 00070 public: 00071 virtual const char* what() const noexcept { return "bad any_cast"; } 00072 }; 00073 00074 [[gnu::noreturn]] inline void __throw_bad_any_cast() 00075 { 00076 #if __cpp_exceptions 00077 throw bad_any_cast{}; 00078 #else 00079 __builtin_abort(); 00080 #endif 00081 } 00082 00083 /** 00084 * @brief A type-safe container of any type. 00085 * 00086 * An @c any object's state is either empty or it stores a contained object 00087 * of CopyConstructible type. 00088 */ 00089 class any 00090 { 00091 // Holds either pointer to a heap object or the contained object itself. 00092 union _Storage 00093 { 00094 // This constructor intentionally doesn't initialize anything. 00095 _Storage() = default; 00096 00097 // Prevent trivial copies of this type, buffer might hold a non-POD. 00098 _Storage(const _Storage&) = delete; 00099 _Storage& operator=(const _Storage&) = delete; 00100 00101 void* _M_ptr; 00102 aligned_storage<sizeof(_M_ptr), alignof(void*)>::type _M_buffer; 00103 }; 00104 00105 template<typename _Tp, typename _Safe = is_nothrow_move_constructible<_Tp>, 00106 bool _Fits = (sizeof(_Tp) <= sizeof(_Storage)) 00107 && (alignof(_Tp) <= alignof(_Storage))> 00108 using _Internal = std::integral_constant<bool, _Safe::value && _Fits>; 00109 00110 template<typename _Tp> 00111 struct _Manager_internal; // uses small-object optimization 00112 00113 template<typename _Tp> 00114 struct _Manager_external; // creates contained object on the heap 00115 00116 template<typename _Tp> 00117 using _Manager = conditional_t<_Internal<_Tp>::value, 00118 _Manager_internal<_Tp>, 00119 _Manager_external<_Tp>>; 00120 00121 template<typename _Tp, typename _Decayed = decay_t<_Tp>> 00122 using _Decay = enable_if_t<!is_same<_Decayed, any>::value, _Decayed>; 00123 00124 public: 00125 // construct/destruct 00126 00127 /// Default constructor, creates an empty object. 00128 any() noexcept : _M_manager(nullptr) { } 00129 00130 /// Copy constructor, copies the state of @p __other 00131 any(const any& __other) 00132 { 00133 if (__other.empty()) 00134 _M_manager = nullptr; 00135 else 00136 { 00137 _Arg __arg; 00138 __arg._M_any = this; 00139 __other._M_manager(_Op_clone, &__other, &__arg); 00140 } 00141 } 00142 00143 /** 00144 * @brief Move constructor, transfer the state from @p __other 00145 * 00146 * @post @c __other.empty() (this postcondition is a GNU extension) 00147 */ 00148 any(any&& __other) noexcept 00149 { 00150 if (__other.empty()) 00151 _M_manager = nullptr; 00152 else 00153 { 00154 _Arg __arg; 00155 __arg._M_any = this; 00156 __other._M_manager(_Op_xfer, &__other, &__arg); 00157 } 00158 } 00159 00160 /// Construct with a copy of @p __value as the contained object. 00161 template <typename _ValueType, typename _Tp = _Decay<_ValueType>, 00162 typename _Mgr = _Manager<_Tp>, 00163 typename enable_if<is_constructible<_Tp, _ValueType&&>::value, 00164 bool>::type = true> 00165 any(_ValueType&& __value) 00166 : _M_manager(&_Mgr::_S_manage) 00167 { 00168 _Mgr::_S_create(_M_storage, std::forward<_ValueType>(__value)); 00169 static_assert(is_copy_constructible<_Tp>::value, 00170 "The contained object must be CopyConstructible"); 00171 } 00172 00173 /// Construct with a copy of @p __value as the contained object. 00174 template <typename _ValueType, typename _Tp = _Decay<_ValueType>, 00175 typename _Mgr = _Manager<_Tp>, 00176 typename enable_if<!is_constructible<_Tp, _ValueType&&>::value, 00177 bool>::type = false> 00178 any(_ValueType&& __value) 00179 : _M_manager(&_Mgr::_S_manage) 00180 { 00181 _Mgr::_S_create(_M_storage, __value); 00182 static_assert(is_copy_constructible<_Tp>::value, 00183 "The contained object must be CopyConstructible"); 00184 } 00185 00186 /// Destructor, calls @c clear() 00187 ~any() { clear(); } 00188 00189 // assignments 00190 00191 /// Copy the state of another object. 00192 any& operator=(const any& __rhs) 00193 { 00194 if (__rhs.empty()) 00195 clear(); 00196 else if (this != &__rhs) 00197 { 00198 if (!empty()) 00199 _M_manager(_Op_destroy, this, nullptr); 00200 _Arg __arg; 00201 __arg._M_any = this; 00202 __rhs._M_manager(_Op_clone, &__rhs, &__arg); 00203 } 00204 return *this; 00205 } 00206 00207 /** 00208 * @brief Move assignment operator 00209 * 00210 * @post @c __rhs.empty() (not guaranteed for other implementations) 00211 */ 00212 any& operator=(any&& __rhs) noexcept 00213 { 00214 if (__rhs.empty()) 00215 clear(); 00216 else if (this != &__rhs) 00217 { 00218 if (!empty()) 00219 _M_manager(_Op_destroy, this, nullptr); 00220 _Arg __arg; 00221 __arg._M_any = this; 00222 __rhs._M_manager(_Op_xfer, &__rhs, &__arg); 00223 } 00224 return *this; 00225 } 00226 00227 /// Store a copy of @p __rhs as the contained object. 00228 template<typename _ValueType> 00229 enable_if_t<!is_same<any, decay_t<_ValueType>>::value, any&> 00230 operator=(_ValueType&& __rhs) 00231 { 00232 *this = any(std::forward<_ValueType>(__rhs)); 00233 return *this; 00234 } 00235 00236 // modifiers 00237 00238 /// If not empty, destroy the contained object. 00239 void clear() noexcept 00240 { 00241 if (!empty()) 00242 { 00243 _M_manager(_Op_destroy, this, nullptr); 00244 _M_manager = nullptr; 00245 } 00246 } 00247 00248 /// Exchange state with another object. 00249 void swap(any& __rhs) noexcept 00250 { 00251 if (empty() && __rhs.empty()) 00252 return; 00253 00254 if (!empty() && !__rhs.empty()) 00255 { 00256 if (this == &__rhs) 00257 return; 00258 00259 any __tmp; 00260 _Arg __arg; 00261 __arg._M_any = &__tmp; 00262 __rhs._M_manager(_Op_xfer, &__rhs, &__arg); 00263 __arg._M_any = &__rhs; 00264 _M_manager(_Op_xfer, this, &__arg); 00265 __arg._M_any = this; 00266 __tmp._M_manager(_Op_xfer, &__tmp, &__arg); 00267 } 00268 else 00269 { 00270 any* __empty = empty() ? this : &__rhs; 00271 any* __full = empty() ? &__rhs : this; 00272 _Arg __arg; 00273 __arg._M_any = __empty; 00274 __full->_M_manager(_Op_xfer, __full, &__arg); 00275 } 00276 } 00277 00278 // observers 00279 00280 /// Reports whether there is a contained object or not. 00281 bool empty() const noexcept { return _M_manager == nullptr; } 00282 00283 #if __cpp_rtti 00284 /// The @c typeid of the contained object, or @c typeid(void) if empty. 00285 const type_info& type() const noexcept 00286 { 00287 if (empty()) 00288 return typeid(void); 00289 _Arg __arg; 00290 _M_manager(_Op_get_type_info, this, &__arg); 00291 return *__arg._M_typeinfo; 00292 } 00293 #endif 00294 00295 template<typename _Tp> 00296 static constexpr bool __is_valid_cast() 00297 { return __or_<is_reference<_Tp>, is_copy_constructible<_Tp>>::value; } 00298 00299 private: 00300 enum _Op { 00301 _Op_access, _Op_get_type_info, _Op_clone, _Op_destroy, _Op_xfer 00302 }; 00303 00304 union _Arg 00305 { 00306 void* _M_obj; 00307 const std::type_info* _M_typeinfo; 00308 any* _M_any; 00309 }; 00310 00311 void (*_M_manager)(_Op, const any*, _Arg*); 00312 _Storage _M_storage; 00313 00314 template<typename _Tp> 00315 friend void* __any_caster(const any* __any); 00316 00317 // Manage in-place contained object. 00318 template<typename _Tp> 00319 struct _Manager_internal 00320 { 00321 static void 00322 _S_manage(_Op __which, const any* __anyp, _Arg* __arg); 00323 00324 template<typename _Up> 00325 static void 00326 _S_create(_Storage& __storage, _Up&& __value) 00327 { 00328 void* __addr = &__storage._M_buffer; 00329 ::new (__addr) _Tp(std::forward<_Up>(__value)); 00330 } 00331 }; 00332 00333 // Manage external contained object. 00334 template<typename _Tp> 00335 struct _Manager_external 00336 { 00337 static void 00338 _S_manage(_Op __which, const any* __anyp, _Arg* __arg); 00339 00340 template<typename _Up> 00341 static void 00342 _S_create(_Storage& __storage, _Up&& __value) 00343 { 00344 __storage._M_ptr = new _Tp(std::forward<_Up>(__value)); 00345 } 00346 }; 00347 }; 00348 00349 /// Exchange the states of two @c any objects. 00350 inline void swap(any& __x, any& __y) noexcept { __x.swap(__y); } 00351 00352 /** 00353 * @brief Access the contained object. 00354 * 00355 * @tparam _ValueType A const-reference or CopyConstructible type. 00356 * @param __any The object to access. 00357 * @return The contained object. 00358 * @throw bad_any_cast If <code> 00359 * __any.type() != typeid(remove_reference_t<_ValueType>) 00360 * </code> 00361 */ 00362 template<typename _ValueType> 00363 inline _ValueType any_cast(const any& __any) 00364 { 00365 static_assert(any::__is_valid_cast<_ValueType>(), 00366 "Template argument must be a reference or CopyConstructible type"); 00367 auto __p = any_cast<add_const_t<remove_reference_t<_ValueType>>>(&__any); 00368 if (__p) 00369 return *__p; 00370 __throw_bad_any_cast(); 00371 } 00372 00373 /** 00374 * @brief Access the contained object. 00375 * 00376 * @tparam _ValueType A reference or CopyConstructible type. 00377 * @param __any The object to access. 00378 * @return The contained object. 00379 * @throw bad_any_cast If <code> 00380 * __any.type() != typeid(remove_reference_t<_ValueType>) 00381 * </code> 00382 * 00383 * @{ 00384 */ 00385 template<typename _ValueType> 00386 inline _ValueType any_cast(any& __any) 00387 { 00388 static_assert(any::__is_valid_cast<_ValueType>(), 00389 "Template argument must be a reference or CopyConstructible type"); 00390 auto __p = any_cast<remove_reference_t<_ValueType>>(&__any); 00391 if (__p) 00392 return *__p; 00393 __throw_bad_any_cast(); 00394 } 00395 00396 template<typename _ValueType, 00397 typename enable_if<!is_move_constructible<_ValueType>::value 00398 || is_lvalue_reference<_ValueType>::value, 00399 bool>::type = true> 00400 inline _ValueType any_cast(any&& __any) 00401 { 00402 static_assert(any::__is_valid_cast<_ValueType>(), 00403 "Template argument must be a reference or CopyConstructible type"); 00404 auto __p = any_cast<remove_reference_t<_ValueType>>(&__any); 00405 if (__p) 00406 return *__p; 00407 __throw_bad_any_cast(); 00408 } 00409 00410 template<typename _ValueType, 00411 typename enable_if<is_move_constructible<_ValueType>::value 00412 && !is_lvalue_reference<_ValueType>::value, 00413 bool>::type = false> 00414 inline _ValueType any_cast(any&& __any) 00415 { 00416 static_assert(any::__is_valid_cast<_ValueType>(), 00417 "Template argument must be a reference or CopyConstructible type"); 00418 auto __p = any_cast<remove_reference_t<_ValueType>>(&__any); 00419 if (__p) 00420 return std::move(*__p); 00421 __throw_bad_any_cast(); 00422 } 00423 // @} 00424 00425 template<typename _Tp> 00426 void* __any_caster(const any* __any) 00427 { 00428 struct _None { }; 00429 using _Up = decay_t<_Tp>; 00430 using _Vp = conditional_t<is_copy_constructible<_Up>::value, _Up, _None>; 00431 if (__any->_M_manager != &any::_Manager<_Vp>::_S_manage) 00432 return nullptr; 00433 any::_Arg __arg; 00434 __any->_M_manager(any::_Op_access, __any, &__arg); 00435 return __arg._M_obj; 00436 } 00437 00438 /** 00439 * @brief Access the contained object. 00440 * 00441 * @tparam _ValueType The type of the contained object. 00442 * @param __any A pointer to the object to access. 00443 * @return The address of the contained object if <code> 00444 * __any != nullptr && __any.type() == typeid(_ValueType) 00445 * </code>, otherwise a null pointer. 00446 * 00447 * @{ 00448 */ 00449 template<typename _ValueType> 00450 inline const _ValueType* any_cast(const any* __any) noexcept 00451 { 00452 if (__any) 00453 return static_cast<_ValueType*>(__any_caster<_ValueType>(__any)); 00454 return nullptr; 00455 } 00456 00457 template<typename _ValueType> 00458 inline _ValueType* any_cast(any* __any) noexcept 00459 { 00460 if (__any) 00461 return static_cast<_ValueType*>(__any_caster<_ValueType>(__any)); 00462 return nullptr; 00463 } 00464 // @} 00465 00466 template<typename _Tp> 00467 void 00468 any::_Manager_internal<_Tp>:: 00469 _S_manage(_Op __which, const any* __any, _Arg* __arg) 00470 { 00471 // The contained object is in _M_storage._M_buffer 00472 auto __ptr = reinterpret_cast<const _Tp*>(&__any->_M_storage._M_buffer); 00473 switch (__which) 00474 { 00475 case _Op_access: 00476 __arg->_M_obj = const_cast<_Tp*>(__ptr); 00477 break; 00478 case _Op_get_type_info: 00479 #if __cpp_rtti 00480 __arg->_M_typeinfo = &typeid(_Tp); 00481 #endif 00482 break; 00483 case _Op_clone: 00484 ::new(&__arg->_M_any->_M_storage._M_buffer) _Tp(*__ptr); 00485 __arg->_M_any->_M_manager = __any->_M_manager; 00486 break; 00487 case _Op_destroy: 00488 __ptr->~_Tp(); 00489 break; 00490 case _Op_xfer: 00491 ::new(&__arg->_M_any->_M_storage._M_buffer) _Tp(*__ptr); 00492 __ptr->~_Tp(); 00493 __arg->_M_any->_M_manager = __any->_M_manager; 00494 const_cast<any*>(__any)->_M_manager = nullptr; 00495 break; 00496 } 00497 } 00498 00499 template<typename _Tp> 00500 void 00501 any::_Manager_external<_Tp>:: 00502 _S_manage(_Op __which, const any* __any, _Arg* __arg) 00503 { 00504 // The contained object is *_M_storage._M_ptr 00505 auto __ptr = static_cast<const _Tp*>(__any->_M_storage._M_ptr); 00506 switch (__which) 00507 { 00508 case _Op_access: 00509 __arg->_M_obj = const_cast<_Tp*>(__ptr); 00510 break; 00511 case _Op_get_type_info: 00512 #if __cpp_rtti 00513 __arg->_M_typeinfo = &typeid(_Tp); 00514 #endif 00515 break; 00516 case _Op_clone: 00517 __arg->_M_any->_M_storage._M_ptr = new _Tp(*__ptr); 00518 __arg->_M_any->_M_manager = __any->_M_manager; 00519 break; 00520 case _Op_destroy: 00521 delete __ptr; 00522 break; 00523 case _Op_xfer: 00524 __arg->_M_any->_M_storage._M_ptr = __any->_M_storage._M_ptr; 00525 __arg->_M_any->_M_manager = __any->_M_manager; 00526 const_cast<any*>(__any)->_M_manager = nullptr; 00527 break; 00528 } 00529 } 00530 00531 // @} group any 00532 _GLIBCXX_END_NAMESPACE_VERSION 00533 } // namespace fundamentals_v1 00534 } // namespace experimental 00535 } // namespace std 00536 00537 #endif // C++14 00538 00539 #endif // _GLIBCXX_EXPERIMENTAL_ANY