|
libstdc++
|
00001 // <experimental/any> -*- C++ -*- 00002 00003 // Copyright (C) 2014-2015 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/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 void* _M_ptr; 00095 std::aligned_storage<sizeof(_M_ptr), sizeof(_M_ptr)>::type _M_buffer; 00096 }; 00097 00098 template<typename _Tp, typename _Safe = is_trivially_copyable<_Tp>, 00099 bool _Fits = (sizeof(_Tp) <= sizeof(_Storage))> 00100 using _Internal = std::integral_constant<bool, _Safe::value && _Fits>; 00101 00102 template<typename _Tp> 00103 struct _Manager_internal; // uses small-object optimization 00104 00105 template<typename _Tp> 00106 struct _Manager_external; // creates contained object on the heap 00107 00108 template<typename _Tp> 00109 using _Manager = conditional_t<_Internal<_Tp>::value, 00110 _Manager_internal<_Tp>, 00111 _Manager_external<_Tp>>; 00112 00113 template<typename _Tp, typename _Decayed = decay_t<_Tp>> 00114 using _Decay = enable_if_t<!is_same<_Decayed, any>::value, _Decayed>; 00115 00116 public: 00117 // construct/destruct 00118 00119 /// Default constructor, creates an empty object. 00120 any() noexcept : _M_manager(nullptr) { } 00121 00122 /// Copy constructor, copies the state of @p __other 00123 any(const any& __other) : _M_manager(__other._M_manager) 00124 { 00125 if (!__other.empty()) 00126 { 00127 _Arg __arg; 00128 __arg._M_any = this; 00129 _M_manager(_Op_clone, &__other, &__arg); 00130 } 00131 } 00132 00133 /** 00134 * @brief Move constructor, transfer the state from @p __other 00135 * 00136 * @post @c __other.empty() (not guaranteed for other implementations) 00137 */ 00138 any(any&& __other) noexcept 00139 : _M_manager(__other._M_manager), 00140 _M_storage(__other._M_storage) 00141 { __other._M_manager = nullptr; } 00142 00143 /// Construct with a copy of @p __value as the contained object. 00144 template <typename _ValueType, typename _Tp = _Decay<_ValueType>, 00145 typename _Mgr = _Manager<_Tp>> 00146 any(_ValueType&& __value) 00147 : _M_manager(&_Mgr::_S_manage), 00148 _M_storage(_Mgr::_S_create(std::forward<_ValueType>(__value))) 00149 { 00150 static_assert(is_copy_constructible<_Tp>::value, 00151 "The contained object must be CopyConstructible"); 00152 } 00153 00154 /// Destructor, calls @c clear() 00155 ~any() { clear(); } 00156 00157 // assignments 00158 00159 /// Copy the state of 00160 any& operator=(const any& __rhs) 00161 { 00162 any(__rhs).swap(*this); 00163 return *this; 00164 } 00165 00166 /** 00167 * @brief Move assignment operator 00168 * 00169 * @post @c __rhs.empty() (not guaranteed for other implementations) 00170 */ 00171 any& operator=(any&& __rhs) noexcept 00172 { 00173 any(std::move(__rhs)).swap(*this); 00174 return *this; 00175 } 00176 00177 /// Store a copy of @p __rhs as the contained object. 00178 template<typename _ValueType> 00179 any& operator=(_ValueType&& __rhs) 00180 { 00181 any(std::forward<_ValueType>(__rhs)).swap(*this); 00182 return *this; 00183 } 00184 00185 // modifiers 00186 00187 /// If not empty, destroy the contained object. 00188 void clear() noexcept 00189 { 00190 if (!empty()) 00191 { 00192 _M_manager(_Op_destroy, this, nullptr); 00193 _M_manager = nullptr; 00194 } 00195 } 00196 00197 /// Exchange state with another object. 00198 void swap(any& __rhs) noexcept 00199 { 00200 std::swap(_M_manager, __rhs._M_manager); 00201 std::swap(_M_storage, __rhs._M_storage); 00202 } 00203 00204 // observers 00205 00206 /// Reports whether there is a contained object or not. 00207 bool empty() const noexcept { return _M_manager == nullptr; } 00208 00209 #if __cpp_rtti 00210 /// The @c typeid of the contained object, or @c typeid(void) if empty. 00211 const type_info& type() const noexcept 00212 { 00213 if (empty()) 00214 return typeid(void); 00215 _Arg __arg; 00216 _M_manager(_Op_get_type_info, this, &__arg); 00217 return *__arg._M_typeinfo; 00218 } 00219 #endif 00220 00221 template<typename _Tp> 00222 static constexpr bool __is_valid_cast() 00223 { return __or_<is_reference<_Tp>, is_copy_constructible<_Tp>>::value; } 00224 00225 private: 00226 enum _Op { _Op_access, _Op_get_type_info, _Op_clone, _Op_destroy }; 00227 00228 union _Arg 00229 { 00230 void* _M_obj; 00231 const std::type_info* _M_typeinfo; 00232 any* _M_any; 00233 }; 00234 00235 void (*_M_manager)(_Op, const any*, _Arg*); 00236 _Storage _M_storage; 00237 00238 template<typename _Tp> 00239 friend void* __any_caster(const any* __any); 00240 00241 // Manage in-place contained object. 00242 template<typename _Tp> 00243 struct _Manager_internal 00244 { 00245 static void 00246 _S_manage(_Op __which, const any* __anyp, _Arg* __arg); 00247 00248 template<typename _Up> 00249 static _Storage 00250 _S_create(_Up&& __value) 00251 { 00252 _Storage __storage; 00253 void* __addr = &__storage._M_buffer; 00254 ::new (__addr) _Tp(std::forward<_Up>(__value)); 00255 return __storage; 00256 } 00257 00258 template<typename _Alloc, typename _Up> 00259 static _Storage 00260 _S_alloc(const _Alloc&, _Up&& __value) 00261 { 00262 return _S_create(std::forward<_Up>(__value)); 00263 } 00264 }; 00265 00266 // Manage external contained object. 00267 template<typename _Tp> 00268 struct _Manager_external 00269 { 00270 static void 00271 _S_manage(_Op __which, const any* __anyp, _Arg* __arg); 00272 00273 template<typename _Up> 00274 static _Storage 00275 _S_create(_Up&& __value) 00276 { 00277 _Storage __storage; 00278 __storage._M_ptr = new _Tp(std::forward<_Up>(__value)); 00279 return __storage; 00280 } 00281 }; 00282 }; 00283 00284 /// Exchange the states of two @c any objects. 00285 inline void swap(any& __x, any& __y) noexcept { __x.swap(__y); } 00286 00287 /** 00288 * @brief Access the contained object. 00289 * 00290 * @tparam _ValueType A const-reference or CopyConstructible type. 00291 * @param __any The object to access. 00292 * @return The contained object. 00293 * @throw bad_any_cast If <code> 00294 * __any.type() != typeid(remove_reference_t<_ValueType>) 00295 * </code> 00296 */ 00297 template<typename _ValueType> 00298 inline _ValueType any_cast(const any& __any) 00299 { 00300 static_assert(any::__is_valid_cast<_ValueType>(), 00301 "Template argument must be a reference or CopyConstructible type"); 00302 auto __p = any_cast<add_const_t<remove_reference_t<_ValueType>>>(&__any); 00303 if (__p) 00304 return *__p; 00305 __throw_bad_any_cast(); 00306 } 00307 00308 /** 00309 * @brief Access the contained object. 00310 * 00311 * @tparam _ValueType A reference or CopyConstructible type. 00312 * @param __any The object to access. 00313 * @return The contained object. 00314 * @throw bad_any_cast If <code> 00315 * __any.type() != typeid(remove_reference_t<_ValueType>) 00316 * </code> 00317 * 00318 * @{ 00319 */ 00320 template<typename _ValueType> 00321 inline _ValueType any_cast(any& __any) 00322 { 00323 static_assert(any::__is_valid_cast<_ValueType>(), 00324 "Template argument must be a reference or CopyConstructible type"); 00325 auto __p = any_cast<remove_reference_t<_ValueType>>(&__any); 00326 if (__p) 00327 return *__p; 00328 __throw_bad_any_cast(); 00329 } 00330 00331 template<typename _ValueType> 00332 inline _ValueType any_cast(any&& __any) 00333 { 00334 static_assert(any::__is_valid_cast<_ValueType>(), 00335 "Template argument must be a reference or CopyConstructible type"); 00336 auto __p = any_cast<remove_reference_t<_ValueType>>(&__any); 00337 if (__p) 00338 return *__p; 00339 __throw_bad_any_cast(); 00340 } 00341 // @} 00342 00343 template<typename _Tp> 00344 void* __any_caster(const any* __any) 00345 { 00346 struct _None { }; 00347 using _Up = decay_t<_Tp>; 00348 using _Vp = conditional_t<is_copy_constructible<_Up>::value, _Up, _None>; 00349 if (__any->_M_manager != &any::_Manager<_Vp>::_S_manage) 00350 return nullptr; 00351 any::_Arg __arg; 00352 __any->_M_manager(any::_Op_access, __any, &__arg); 00353 return __arg._M_obj; 00354 } 00355 00356 /** 00357 * @brief Access the contained object. 00358 * 00359 * @tparam _ValueType The type of the contained object. 00360 * @param __any A pointer to the object to access. 00361 * @return The address of the contained object if <code> 00362 * __any != nullptr && __any.type() == typeid(_ValueType) 00363 * </code>, otherwise a null pointer. 00364 * 00365 * @{ 00366 */ 00367 template<typename _ValueType> 00368 inline const _ValueType* any_cast(const any* __any) noexcept 00369 { 00370 if (__any) 00371 return static_cast<_ValueType*>(__any_caster<_ValueType>(__any)); 00372 return nullptr; 00373 } 00374 00375 template<typename _ValueType> 00376 inline _ValueType* any_cast(any* __any) noexcept 00377 { 00378 if (__any) 00379 return static_cast<_ValueType*>(__any_caster<_ValueType>(__any)); 00380 return nullptr; 00381 } 00382 // @} 00383 00384 template<typename _Tp> 00385 void 00386 any::_Manager_internal<_Tp>:: 00387 _S_manage(_Op __which, const any* __any, _Arg* __arg) 00388 { 00389 // The contained object is in _M_storage._M_buffer 00390 auto __ptr = reinterpret_cast<const _Tp*>(&__any->_M_storage._M_buffer); 00391 switch (__which) 00392 { 00393 case _Op_access: 00394 __arg->_M_obj = const_cast<_Tp*>(__ptr); 00395 break; 00396 case _Op_get_type_info: 00397 #if __cpp_rtti 00398 __arg->_M_typeinfo = &typeid(_Tp); 00399 #endif 00400 break; 00401 case _Op_clone: 00402 ::new(&__arg->_M_any->_M_storage._M_buffer) _Tp(*__ptr); 00403 break; 00404 case _Op_destroy: 00405 __ptr->~_Tp(); 00406 break; 00407 } 00408 } 00409 00410 template<typename _Tp> 00411 void 00412 any::_Manager_external<_Tp>:: 00413 _S_manage(_Op __which, const any* __any, _Arg* __arg) 00414 { 00415 // The contained object is *_M_storage._M_ptr 00416 auto __ptr = static_cast<const _Tp*>(__any->_M_storage._M_ptr); 00417 switch (__which) 00418 { 00419 case _Op_access: 00420 __arg->_M_obj = const_cast<_Tp*>(__ptr); 00421 break; 00422 case _Op_get_type_info: 00423 #if __cpp_rtti 00424 __arg->_M_typeinfo = &typeid(_Tp); 00425 #endif 00426 break; 00427 case _Op_clone: 00428 __arg->_M_any->_M_storage._M_ptr = new _Tp(*__ptr); 00429 break; 00430 case _Op_destroy: 00431 delete __ptr; 00432 break; 00433 } 00434 } 00435 00436 // @} group any 00437 _GLIBCXX_END_NAMESPACE_VERSION 00438 } // namespace fundamentals_v1 00439 } // namespace experimental 00440 } // namespace std 00441 00442 #endif // C++14 00443 00444 #endif // _GLIBCXX_EXPERIMENTAL_ANY