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