]> git.ipfire.org Git - thirdparty/gcc.git/blobdiff - libstdc++-v3/include/debug/array
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / include / debug / array
index 1cde4e216d040e5c7c553e268a7669077ab91f4d..3f87e98fe8d20c3d816b2ca7e480b7f75ab29877 100644 (file)
@@ -1,6 +1,6 @@
 // Debugging array implementation -*- C++ -*-
 
-// Copyright (C) 2012 Free Software Foundation, Inc.
+// Copyright (C) 2012-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
 
 #pragma GCC system_header
 
-#include <debug/safe_sequence.h>
+#include <array>
 
-#ifndef _GLIBCXX_THROW_OR_ABORT
-# if __EXCEPTIONS
-#  define _GLIBCXX_THROW_OR_ABORT(_Exc) (throw (_Exc))
-# else
-#  define _GLIBCXX_THROW_OR_ABORT(_Exc) (__builtin_abort())
-# endif
-#endif
+#include <debug/formatter.h>
+#include <debug/macros.h>
 
 namespace std _GLIBCXX_VISIBILITY(default)
 {
@@ -76,7 +71,7 @@ namespace __debug
       template<std::size_t _Size>
        struct _Array_check_nonempty
        {
-         bool empty() { return _Size == 0; }
+         _GLIBCXX_NODISCARD bool empty() { return _Size == 0; }
 
          _Array_check_nonempty()
          { __glibcxx_check_nonempty(); }
@@ -85,77 +80,77 @@ namespace __debug
       // No explicit construct/copy/destroy for aggregate type.
 
       // DR 776.
-      void
+      _GLIBCXX20_CONSTEXPR void
       fill(const value_type& __u)
       { std::fill_n(begin(), size(), __u); }
 
-      void
+      _GLIBCXX20_CONSTEXPR void
       swap(array& __other)
-      noexcept(noexcept(swap(std::declval<_Tp&>(), std::declval<_Tp&>())))
+      noexcept(_AT_Type::_Is_nothrow_swappable::value)
       { std::swap_ranges(begin(), end(), __other.begin()); }
 
       // Iterators.
-      iterator
+      _GLIBCXX17_CONSTEXPR iterator
       begin() noexcept
       { return iterator(data()); }
 
-      const_iterator
+      _GLIBCXX17_CONSTEXPR const_iterator
       begin() const noexcept
       { return const_iterator(data()); }
 
-      iterator
+      _GLIBCXX17_CONSTEXPR iterator
       end() noexcept
       { return iterator(data() + _Nm); }
 
-      const_iterator
+      _GLIBCXX17_CONSTEXPR const_iterator
       end() const noexcept
       { return const_iterator(data() + _Nm); }
 
-      reverse_iterator 
+      _GLIBCXX17_CONSTEXPR reverse_iterator
       rbegin() noexcept
       { return reverse_iterator(end()); }
 
-      const_reverse_iterator 
+      _GLIBCXX17_CONSTEXPR const_reverse_iterator
       rbegin() const noexcept
       { return const_reverse_iterator(end()); }
 
-      reverse_iterator 
+      _GLIBCXX17_CONSTEXPR reverse_iterator
       rend() noexcept
       { return reverse_iterator(begin()); }
 
-      const_reverse_iterator 
+      _GLIBCXX17_CONSTEXPR const_reverse_iterator
       rend() const noexcept
       { return const_reverse_iterator(begin()); }
 
-      const_iterator
+      _GLIBCXX17_CONSTEXPR const_iterator
       cbegin() const noexcept
       { return const_iterator(data()); }
 
-      const_iterator
+      _GLIBCXX17_CONSTEXPR const_iterator
       cend() const noexcept
       { return const_iterator(data() + _Nm); }
 
-      const_reverse_iterator 
+      _GLIBCXX17_CONSTEXPR const_reverse_iterator
       crbegin() const noexcept
       { return const_reverse_iterator(end()); }
 
-      const_reverse_iterator 
+      _GLIBCXX17_CONSTEXPR const_reverse_iterator
       crend() const noexcept
       { return const_reverse_iterator(begin()); }
 
       // Capacity.
-      constexpr size_type 
+      constexpr size_type
       size() const noexcept { return _Nm; }
 
-      constexpr size_type 
+      constexpr size_type
       max_size() const noexcept { return _Nm; }
 
-      constexpr bool 
+      _GLIBCXX_NODISCARD constexpr bool
       empty() const noexcept { return size() == 0; }
 
       // Element access.
-      reference
-      operator[](size_type __n)
+      _GLIBCXX17_CONSTEXPR reference
+      operator[](size_type __n) noexcept
       {
        __glibcxx_check_subscript(__n);
        return _AT_Type::_S_ref(_M_elems, __n);
@@ -165,15 +160,17 @@ namespace __debug
       operator[](size_type __n) const noexcept
       {
        return __n < _Nm ? _AT_Type::_S_ref(_M_elems, __n)
-        : (_GLIBCXX_THROW_OR_ABORT (_Array_check_subscript<_Nm>(__n)),
+        : (_GLIBCXX_THROW_OR_ABORT(_Array_check_subscript<_Nm>(__n)),
            _AT_Type::_S_ref(_M_elems, 0));
       }
 
-      reference
+      _GLIBCXX17_CONSTEXPR reference
       at(size_type __n)
       {
        if (__n >= _Nm)
-         std::__throw_out_of_range(__N("array::at"));
+         std::__throw_out_of_range_fmt(__N("array::at: __n (which is %zu) "
+                                           ">= _Nm (which is %zu)"),
+                                       __n, _Nm);
        return _AT_Type::_S_ref(_M_elems, __n);
       }
 
@@ -183,85 +180,109 @@ namespace __debug
        // Result of conditional expression must be an lvalue so use
        // boolean ? lvalue : (throw-expr, lvalue)
        return __n < _Nm ? _AT_Type::_S_ref(_M_elems, __n)
-         : (std::__throw_out_of_range(__N("array::at")),
+         : (std::__throw_out_of_range_fmt(__N("array::at: __n (which is %zu) "
+                                              ">= _Nm (which is %zu)"),
+                                          __n, _Nm),
             _AT_Type::_S_ref(_M_elems, 0));
       }
 
-      reference 
-      front()
+      _GLIBCXX17_CONSTEXPR reference
+      front() noexcept
       {
        __glibcxx_check_nonempty();
        return *begin();
       }
 
-      constexpr const_reference 
-      front() const
+      constexpr const_reference
+      front() const noexcept
       {
        return _Nm ? _AT_Type::_S_ref(_M_elems, 0)
-         : (_GLIBCXX_THROW_OR_ABORT (_Array_check_nonempty<_Nm>()),
+         : (_GLIBCXX_THROW_OR_ABORT(_Array_check_nonempty<_Nm>()),
             _AT_Type::_S_ref(_M_elems, 0));
       }
 
-      reference 
-      back()
+      _GLIBCXX17_CONSTEXPR reference
+      back() noexcept
       {
        __glibcxx_check_nonempty();
        return _Nm ? *(end() - 1) : *end();
       }
 
-      constexpr const_reference 
-      back() const
+      constexpr const_reference
+      back() const noexcept
       {
        return _Nm ? _AT_Type::_S_ref(_M_elems, _Nm - 1)
-         : (_GLIBCXX_THROW_OR_ABORT (_Array_check_nonempty<_Nm>()),
+         : (_GLIBCXX_THROW_OR_ABORT(_Array_check_nonempty<_Nm>()),
             _AT_Type::_S_ref(_M_elems, 0));
       }
 
-      pointer
+      _GLIBCXX17_CONSTEXPR pointer
       data() noexcept
-      { return std::__addressof(_AT_Type::_S_ref(_M_elems, 0)); }
+      { return _AT_Type::_S_ptr(_M_elems); }
 
-      const_pointer
+      _GLIBCXX17_CONSTEXPR const_pointer
       data() const noexcept
-      { return std::__addressof(_AT_Type::_S_ref(_M_elems, 0)); }
+      { return _AT_Type::_S_ptr(_M_elems); }
     };
 
+#if __cpp_deduction_guides >= 201606
+  template<typename _Tp, typename... _Up>
+    array(_Tp, _Up...)
+      -> array<std::enable_if_t<(std::is_same_v<_Tp, _Up> && ...), _Tp>,
+              1 + sizeof...(_Up)>;
+#endif
+
   // Array comparisons.
   template<typename _Tp, std::size_t _Nm>
-    inline bool 
+    _GLIBCXX20_CONSTEXPR
+    inline bool
     operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
     { return std::equal(__one.begin(), __one.end(), __two.begin()); }
 
   template<typename _Tp, std::size_t _Nm>
+    _GLIBCXX20_CONSTEXPR
     inline bool
     operator!=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
     { return !(__one == __two); }
 
   template<typename _Tp, std::size_t _Nm>
+    _GLIBCXX20_CONSTEXPR
     inline bool
     operator<(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b)
-    { 
+    {
       return std::lexicographical_compare(__a.begin(), __a.end(),
-                                         __b.begin(), __b.end()); 
+                                         __b.begin(), __b.end());
     }
 
   template<typename _Tp, std::size_t _Nm>
+    _GLIBCXX20_CONSTEXPR
     inline bool
     operator>(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
     { return __two < __one; }
 
   template<typename _Tp, std::size_t _Nm>
+    _GLIBCXX20_CONSTEXPR
     inline bool
     operator<=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
     { return !(__one > __two); }
 
   template<typename _Tp, std::size_t _Nm>
+    _GLIBCXX20_CONSTEXPR
     inline bool
     operator>=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
     { return !(__one < __two); }
 
   // Specialized algorithms.
+
+#if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
+  template<typename _Tp, size_t _Nm>
+    typename enable_if<
+      !_GLIBCXX_STD_C::__array_traits<_Tp, _Nm>::_Is_swappable::value>::type
+    swap(array<_Tp, _Nm>&, array<_Tp, _Nm>&) = delete;
+#endif
+
   template<typename _Tp, std::size_t _Nm>
+    _GLIBCXX20_CONSTEXPR
     inline void
     swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two)
     noexcept(noexcept(__one.swap(__two)))
@@ -281,7 +302,7 @@ namespace __debug
     get(array<_Tp, _Nm>&& __arr) noexcept
     {
       static_assert(_Int < _Nm, "index is out of bounds");
-      return std::move(get<_Int>(__arr));
+      return std::move(__debug::get<_Int>(__arr));
     }
 
   template<std::size_t _Int, typename _Tp, std::size_t _Nm>
@@ -292,30 +313,76 @@ namespace __debug
       return _GLIBCXX_STD_C::__array_traits<_Tp, _Nm>::
        _S_ref(__arr._M_elems, _Int);
     }
+
+  template<std::size_t _Int, typename _Tp, std::size_t _Nm>
+    constexpr const _Tp&&
+    get(const array<_Tp, _Nm>&& __arr) noexcept
+    {
+      static_assert(_Int < _Nm, "index is out of bounds");
+      return std::move(__debug::get<_Int>(__arr));
+    }
+
+#if __cplusplus > 201703L
+#define __cpp_lib_to_array 201907L
+
+  template<bool _Move = false, typename _Tp, size_t... _Idx>
+    constexpr array<remove_cv_t<_Tp>, sizeof...(_Idx)>
+    __to_array(_Tp (&__a)[sizeof...(_Idx)], index_sequence<_Idx...>)
+    {
+      if constexpr (_Move)
+       return {{std::move(__a[_Idx])...}};
+      else
+       return {{__a[_Idx]...}};
+    }
+
+  template<typename _Tp, size_t _Nm>
+    constexpr array<remove_cv_t<_Tp>, _Nm>
+    to_array(_Tp (&__a)[_Nm])
+    noexcept(is_nothrow_constructible_v<_Tp, _Tp&>)
+    {
+      static_assert(!is_array_v<_Tp>);
+      static_assert(is_constructible_v<_Tp, _Tp&>);
+      if constexpr (is_constructible_v<_Tp, _Tp&>)
+       return __debug::__to_array(__a, make_index_sequence<_Nm>{});
+      __builtin_unreachable(); // FIXME: see PR c++/91388
+    }
+
+  template<typename _Tp, size_t _Nm>
+    constexpr array<remove_cv_t<_Tp>, _Nm>
+    to_array(_Tp (&&__a)[_Nm])
+    noexcept(is_nothrow_move_constructible_v<_Tp>)
+    {
+      static_assert(!is_array_v<_Tp>);
+      static_assert(is_move_constructible_v<_Tp>);
+      if constexpr (is_move_constructible_v<_Tp>)
+       return __debug::__to_array<1>(__a, make_index_sequence<_Nm>{});
+      __builtin_unreachable(); // FIXME: see PR c++/91388
+    }
+#endif // C++20
+
 } // namespace __debug
 
+_GLIBCXX_BEGIN_NAMESPACE_VERSION
   // Tuple interface to class template array.
 
   /// tuple_size
-  template<typename _Tp> 
-    class tuple_size;
-
   template<typename _Tp, std::size_t _Nm>
-    struct tuple_size<__debug::array<_Tp, _Nm>>
+    struct tuple_size<std::__debug::array<_Tp, _Nm>>
     : public integral_constant<std::size_t, _Nm> { };
 
   /// tuple_element
-  template<std::size_t _Int, typename _Tp>
-    class tuple_element;
-
   template<std::size_t _Int, typename _Tp, std::size_t _Nm>
-    struct tuple_element<_Int, __debug::array<_Tp, _Nm>>
+    struct tuple_element<_Int, std::__debug::array<_Tp, _Nm>>
     {
       static_assert(_Int < _Nm, "index is out of bounds");
       typedef _Tp type;
     };
-} // namespace std
 
-#undef _GLIBCXX_THROW_OR_ABORT
+  template<typename _Tp, std::size_t _Nm>
+    struct __is_tuple_like_impl<std::__debug::array<_Tp, _Nm>> : true_type
+    { };
+
+_GLIBCXX_END_NAMESPACE_VERSION
+} // namespace std
 
 #endif // _GLIBCXX_DEBUG_ARRAY