]> git.ipfire.org Git - thirdparty/gcc.git/blobdiff - libstdc++-v3/include/experimental/any
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / include / experimental / any
index 8c205d5cad0890945eea3939ec5b70935903c329..43455de479f45631072721e3ef501617d9dd0d0f 100644 (file)
@@ -1,6 +1,6 @@
 // <experimental/any> -*- C++ -*-
 
-// Copyright (C) 2014-2015 Free Software Foundation, Inc.
+// Copyright (C) 2014-2020 Free Software Foundation, Inc.
 //
 // This file is part of the GNU ISO C++ Library.  This library is free
 // software; you can redistribute it and/or modify it under the
@@ -24,6 +24,7 @@
 
 /** @file experimental/any
  *  This is a TS C++ Library header.
+ *  @ingroup libfund-ts
  */
 
 #ifndef _GLIBCXX_EXPERIMENTAL_ANY
 
 #pragma GCC system_header
 
-#if __cplusplus <= 201103L
-# include <bits/c++14_warning.h>
-#else
+#if __cplusplus >= 201402L
 
 #include <typeinfo>
 #include <new>
 #include <utility>
 #include <type_traits>
+#include <experimental/bits/lfts_config.h>
 
 namespace std _GLIBCXX_VISIBILITY(default)
 {
+_GLIBCXX_BEGIN_NAMESPACE_VERSION
+
 namespace experimental
 {
 inline namespace fundamentals_v1
 {
-_GLIBCXX_BEGIN_NAMESPACE_VERSION
-
   /**
    * @defgroup any Type-safe container of any type
-   * @ingroup experimental
+   * @ingroup libfund-ts
    *
    * A type-safe container for single values of value types, as
    * described in n3804 "Any Library Proposal (Revision 3)".
@@ -70,6 +70,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     virtual const char* what() const noexcept { return "bad any_cast"; }
   };
 
+  /// @cond undocumented
   [[gnu::noreturn]] inline void __throw_bad_any_cast()
   {
 #if __cpp_exceptions
@@ -78,6 +79,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     __builtin_abort();
 #endif
   }
+  /// @endcond
 
   /**
    *  @brief A type-safe container of any type.
@@ -90,12 +92,20 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     // Holds either pointer to a heap object or the contained object itself.
     union _Storage
     {
+      // This constructor intentionally doesn't initialize anything.
+      _Storage() = default;
+
+      // Prevent trivial copies of this type, buffer might hold a non-POD.
+      _Storage(const _Storage&) = delete;
+      _Storage& operator=(const _Storage&) = delete;
+
       void* _M_ptr;
-      std::aligned_storage<sizeof(_M_ptr), sizeof(_M_ptr)>::type _M_buffer;
+      aligned_storage<sizeof(_M_ptr), alignof(void*)>::type _M_buffer;
     };
 
     template<typename _Tp, typename _Safe = is_nothrow_move_constructible<_Tp>,
-            bool _Fits = (sizeof(_Tp) <= sizeof(_Storage))>
+            bool _Fits = (sizeof(_Tp) <= sizeof(_Storage))
+                         && (alignof(_Tp) <= alignof(_Storage))>
       using _Internal = std::integral_constant<bool, _Safe::value && _Fits>;
 
     template<typename _Tp>
@@ -119,33 +129,57 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     any() noexcept : _M_manager(nullptr) { }
 
     /// Copy constructor, copies the state of @p __other
-    any(const any& __other) : _M_manager(__other._M_manager)
+    any(const any& __other)
     {
-      if (!__other.empty())
+      if (__other.empty())
+       _M_manager = nullptr;
+      else
        {
          _Arg __arg;
          __arg._M_any = this;
-         _M_manager(_Op_clone, &__other, &__arg);
+         __other._M_manager(_Op_clone, &__other, &__arg);
        }
     }
 
     /**
      * @brief Move constructor, transfer the state from @p __other
      *
-     * @post @c __other.empty() (not guaranteed for other implementations)
+     * @post @c __other.empty() (this postcondition is a GNU extension)
      */
     any(any&& __other) noexcept
-    : _M_manager(__other._M_manager),
-      _M_storage(__other._M_storage)
-    { __other._M_manager = nullptr; }
+    {
+      if (__other.empty())
+       _M_manager = nullptr;
+      else
+       {
+         _Arg __arg;
+         __arg._M_any = this;
+         __other._M_manager(_Op_xfer, &__other, &__arg);
+       }
+    }
+
+    /// Construct with a copy of @p __value as the contained object.
+    template <typename _ValueType, typename _Tp = _Decay<_ValueType>,
+             typename _Mgr = _Manager<_Tp>,
+              typename enable_if<is_constructible<_Tp, _ValueType&&>::value,
+                                 bool>::type = true>
+      any(_ValueType&& __value)
+      : _M_manager(&_Mgr::_S_manage)
+      {
+        _Mgr::_S_create(_M_storage, std::forward<_ValueType>(__value));
+       static_assert(is_copy_constructible<_Tp>::value,
+                     "The contained object must be CopyConstructible");
+      }
 
     /// Construct with a copy of @p __value as the contained object.
     template <typename _ValueType, typename _Tp = _Decay<_ValueType>,
-             typename _Mgr = _Manager<_Tp>>
+             typename _Mgr = _Manager<_Tp>,
+              typename enable_if<!is_constructible<_Tp, _ValueType&&>::value,
+                                 bool>::type = false>
       any(_ValueType&& __value)
-      : _M_manager(&_Mgr::_S_manage),
-        _M_storage(_Mgr::_S_create(std::forward<_ValueType>(__value)))
+      : _M_manager(&_Mgr::_S_manage)
       {
+        _Mgr::_S_create(_M_storage, __value);
        static_assert(is_copy_constructible<_Tp>::value,
                      "The contained object must be CopyConstructible");
       }
@@ -155,10 +189,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
     // assignments
 
-    /// Copy the state of 
+    /// Copy the state of another object.
     any& operator=(const any& __rhs)
     {
-      any(__rhs).swap(*this);
+      *this = any(__rhs);
       return *this;
     }
 
@@ -169,15 +203,24 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
      */
     any& operator=(any&& __rhs) noexcept
     {
-      any(std::move(__rhs)).swap(*this);
+      if (__rhs.empty())
+       clear();
+      else if (this != &__rhs)
+       {
+         clear();
+         _Arg __arg;
+         __arg._M_any = this;
+         __rhs._M_manager(_Op_xfer, &__rhs, &__arg);
+       }
       return *this;
     }
 
     /// Store a copy of @p __rhs as the contained object.
     template<typename _ValueType>
-      any& operator=(_ValueType&& __rhs)
+      enable_if_t<!is_same<any, decay_t<_ValueType>>::value, any&>
+      operator=(_ValueType&& __rhs)
       {
-       any(std::forward<_ValueType>(__rhs)).swap(*this);
+       *this = any(std::forward<_ValueType>(__rhs));
        return *this;
       }
 
@@ -196,14 +239,37 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     /// Exchange state with another object.
     void swap(any& __rhs) noexcept
     {
-      std::swap(_M_manager, __rhs._M_manager);
-      std::swap(_M_storage, __rhs._M_storage);
+      if (empty() && __rhs.empty())
+       return;
+
+      if (!empty() && !__rhs.empty())
+       {
+         if (this == &__rhs)
+           return;
+
+         any __tmp;
+         _Arg __arg;
+         __arg._M_any = &__tmp;
+         __rhs._M_manager(_Op_xfer, &__rhs, &__arg);
+         __arg._M_any = &__rhs;
+         _M_manager(_Op_xfer, this, &__arg);
+         __arg._M_any = this;
+         __tmp._M_manager(_Op_xfer, &__tmp, &__arg);
+       }
+      else
+       {
+         any* __empty = empty() ? this : &__rhs;
+         any* __full = empty() ? &__rhs : this;
+         _Arg __arg;
+         __arg._M_any = __empty;
+         __full->_M_manager(_Op_xfer, __full, &__arg);
+       }
     }
 
     // observers
 
     /// Reports whether there is a contained object or not.
-    bool empty() const noexcept { return _M_manager == nullptr; }
+    _GLIBCXX_NODISCARD bool empty() const noexcept { return _M_manager == nullptr; }
 
 #if __cpp_rtti
     /// The @c typeid of the contained object, or @c typeid(void) if empty.
@@ -222,7 +288,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       { return __or_<is_reference<_Tp>, is_copy_constructible<_Tp>>::value; }
 
   private:
-    enum _Op { _Op_access, _Op_get_type_info, _Op_clone, _Op_destroy };
+    enum _Op {
+       _Op_access, _Op_get_type_info, _Op_clone, _Op_destroy, _Op_xfer
+    };
 
     union _Arg
     {
@@ -235,14 +303,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     _Storage _M_storage;
 
     template<typename _Tp>
-      friend void* __any_caster(const any* __any)
-      {
-       if (__any->_M_manager != &_Manager<decay_t<_Tp>>::_S_manage)
-         return nullptr;
-       _Arg __arg;
-       __any->_M_manager(_Op_access, __any, &__arg);
-       return __arg._M_obj;
-      }
+      friend enable_if_t<is_object<_Tp>::value, void*>
+      __any_caster(const any* __any);
 
     // Manage in-place contained object.
     template<typename _Tp>
@@ -252,20 +314,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        _S_manage(_Op __which, const any* __anyp, _Arg* __arg);
 
        template<typename _Up>
-         static _Storage
-         _S_create(_Up&& __value)
+         static void
+         _S_create(_Storage& __storage, _Up&& __value)
          {
-           _Storage __storage;
            void* __addr = &__storage._M_buffer;
            ::new (__addr) _Tp(std::forward<_Up>(__value));
-           return __storage;
-         }
-
-       template<typename _Alloc, typename _Up>
-         static _Storage
-         _S_alloc(const _Alloc&, _Up&& __value)
-         {
-           return _S_create(std::forward<_Up>(__value));
          }
       };
 
@@ -277,12 +330,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        _S_manage(_Op __which, const any* __anyp, _Arg* __arg);
 
        template<typename _Up>
-         static _Storage
-         _S_create(_Up&& __value)
+         static void
+         _S_create(_Storage& __storage, _Up&& __value)
          {
-           _Storage __storage;
            __storage._M_ptr = new _Tp(std::forward<_Up>(__value));
-           return __storage;
          }
       };
   };
@@ -334,7 +385,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       __throw_bad_any_cast();
     }
 
-  template<typename _ValueType>
+  template<typename _ValueType,
+           typename enable_if<!is_move_constructible<_ValueType>::value
+                              || is_lvalue_reference<_ValueType>::value,
+                              bool>::type = true>
     inline _ValueType any_cast(any&& __any)
     {
       static_assert(any::__is_valid_cast<_ValueType>(),
@@ -344,8 +398,61 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        return *__p;
       __throw_bad_any_cast();
     }
+
+  template<typename _ValueType,
+           typename enable_if<is_move_constructible<_ValueType>::value
+                              && !is_lvalue_reference<_ValueType>::value,
+                              bool>::type = false>
+    inline _ValueType any_cast(any&& __any)
+    {
+      static_assert(any::__is_valid_cast<_ValueType>(),
+         "Template argument must be a reference or CopyConstructible type");
+      auto __p = any_cast<remove_reference_t<_ValueType>>(&__any);
+      if (__p)
+       return std::move(*__p);
+      __throw_bad_any_cast();
+    }
   // @}
 
+  /// @cond undocumented
+  template<typename _Tp>
+    enable_if_t<is_object<_Tp>::value, void*>
+    __any_caster(const any* __any)
+    {
+      // any_cast<T> returns non-null if __any->type() == typeid(T) and
+      // typeid(T) ignores cv-qualifiers so remove them:
+      using _Up = remove_cv_t<_Tp>;
+      // The contained value has a decayed type, so if decay_t<U> is not U,
+      // then it's not possible to have a contained value of type U.
+      using __does_not_decay = is_same<decay_t<_Up>, _Up>;
+      // Only copy constructible types can be used for contained values.
+      using __is_copyable = is_copy_constructible<_Up>;
+      // If the type _Tp could never be stored in an any we don't want to
+      // instantiate _Manager<_Tp>, so use _Manager<any::_Op> instead, which
+      // is explicitly specialized and has a no-op _S_manage function.
+      using _Vp = conditional_t<__and_<__does_not_decay, __is_copyable>::value,
+                               _Up, any::_Op>;
+      // First try comparing function addresses, which works without RTTI
+      if (__any->_M_manager == &any::_Manager<_Vp>::_S_manage
+#if __cpp_rtti
+         || __any->type() == typeid(_Tp)
+#endif
+         )
+       {
+         any::_Arg __arg;
+         __any->_M_manager(any::_Op_access, __any, &__arg);
+         return __arg._M_obj;
+       }
+      return nullptr;
+    }
+
+  // This overload exists so that std::any_cast<void(*)()>(a) is well-formed.
+  template<typename _Tp>
+    enable_if_t<!is_object<_Tp>::value, _Tp*>
+    __any_caster(const any*) noexcept
+    { return nullptr; }
+  /// @endcond
+
   /**
    * @brief Access the contained object.
    *
@@ -393,10 +500,18 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        break;
       case _Op_clone:
        ::new(&__arg->_M_any->_M_storage._M_buffer) _Tp(*__ptr);
+       __arg->_M_any->_M_manager = __any->_M_manager;
        break;
       case _Op_destroy:
        __ptr->~_Tp();
        break;
+      case _Op_xfer:
+       ::new(&__arg->_M_any->_M_storage._M_buffer) _Tp
+         (std::move(*const_cast<_Tp*>(__ptr)));
+       __ptr->~_Tp();
+       __arg->_M_any->_M_manager = __any->_M_manager;
+       const_cast<any*>(__any)->_M_manager = nullptr;
+       break;
       }
     }
 
@@ -419,17 +534,32 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        break;
       case _Op_clone:
        __arg->_M_any->_M_storage._M_ptr = new _Tp(*__ptr);
+       __arg->_M_any->_M_manager = __any->_M_manager;
        break;
       case _Op_destroy:
        delete __ptr;
        break;
+      case _Op_xfer:
+       __arg->_M_any->_M_storage._M_ptr = __any->_M_storage._M_ptr;
+       __arg->_M_any->_M_manager = __any->_M_manager;
+       const_cast<any*>(__any)->_M_manager = nullptr;
+       break;
       }
     }
 
+  // Dummy specialization used by __any_caster.
+  template<>
+    struct any::_Manager_internal<any::_Op>
+    {
+      static void
+      _S_manage(_Op, const any*, _Arg*) { }
+    };
+
   // @} group any
-_GLIBCXX_END_NAMESPACE_VERSION
 } // namespace fundamentals_v1
 } // namespace experimental
+
+_GLIBCXX_END_NAMESPACE_VERSION
 } // namespace std
 
 #endif // C++14