Skip to content

Commit

Permalink
Implement move_sentinel and C++20 move_iterator.
Browse files Browse the repository at this point in the history
Differential Revision: https://reviews.llvm.org/D117656
  • Loading branch information
Arthur O'Dwyer authored and ldionne committed Apr 13, 2022
1 parent 7d70b1a commit 2fb026e
Show file tree
Hide file tree
Showing 25 changed files with 1,295 additions and 220 deletions.
2 changes: 1 addition & 1 deletion libcxx/docs/Status/Cxx2bIssues.csv
Expand Up @@ -104,7 +104,7 @@
`3123 <https://wg21.link/LWG3123>`__,"``duration`` constructor from representation shouldn't be effectively non-throwing","October 2021","","","|chrono|"
`3146 <https://wg21.link/LWG3146>`__,"Excessive unwrapping in ``std::ref/cref``","October 2021","|Complete|","14.0"
`3152 <https://wg21.link/LWG3152>`__,"``common_type`` and ``common_reference`` have flaws in common","October 2021","",""
`3293 <https://wg21.link/LWG3293>`__,"``move_iterator operator+()`` has incorrect constraints","October 2021","","","|ranges|"
`3293 <https://wg21.link/LWG3293>`__,"``move_iterator operator+()`` has incorrect constraints","October 2021","|Complete|","15.0","|ranges|"
`3361 <https://wg21.link/LWG3361>`__,"``safe_range<SomeRange&>`` case","October 2021","|Nothing To Do|","","|ranges|"
`3392 <https://wg21.link/LWG3392>`__,"``ranges::distance()`` cannot be used on a move-only iterator with a sized sentinel","October 2021","|Complete|","14.0","|ranges|"
`3407 <https://wg21.link/LWG3407>`__,"Some problems with the wording changes of P1739R4","October 2021","","","|ranges|"
Expand Down
1 change: 1 addition & 0 deletions libcxx/include/CMakeLists.txt
Expand Up @@ -270,6 +270,7 @@ set(files
__iterator/iterator_traits.h
__iterator/mergeable.h
__iterator/move_iterator.h
__iterator/move_sentinel.h
__iterator/next.h
__iterator/ostream_iterator.h
__iterator/ostreambuf_iterator.h
Expand Down
170 changes: 162 additions & 8 deletions libcxx/include/__iterator/move_iterator.h
Expand Up @@ -10,8 +10,20 @@
#ifndef _LIBCPP___ITERATOR_MOVE_ITERATOR_H
#define _LIBCPP___ITERATOR_MOVE_ITERATOR_H

#include <__compare/compare_three_way_result.h>
#include <__compare/three_way_comparable.h>
#include <__concepts/assignable.h>
#include <__concepts/convertible_to.h>
#include <__concepts/derived_from.h>
#include <__concepts/same_as.h>
#include <__config>
#include <__iterator/concepts.h>
#include <__iterator/incrementable_traits.h>
#include <__iterator/iter_move.h>
#include <__iterator/iter_swap.h>
#include <__iterator/iterator_traits.h>
#include <__iterator/move_sentinel.h>
#include <__iterator/readable_traits.h>
#include <__utility/move.h>
#include <type_traits>

Expand All @@ -21,20 +33,48 @@

_LIBCPP_BEGIN_NAMESPACE_STD

#if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_CONCEPTS)
template<class _Iter, class = void>
struct __move_iter_category_base {};

template<class _Iter>
requires requires { typename iterator_traits<_Iter>::iterator_category; }
struct __move_iter_category_base<_Iter> {
using iterator_category = _If<
derived_from<typename iterator_traits<_Iter>::iterator_category, random_access_iterator_tag>,
random_access_iterator_tag,
typename iterator_traits<_Iter>::iterator_category
>;
};

template<class _Iter, class _Sent>
concept __move_iter_comparable = requires {
{ declval<const _Iter&>() == declval<_Sent>() } -> convertible_to<bool>;
};
#endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_CONCEPTS)

template <class _Iter>
class _LIBCPP_TEMPLATE_VIS move_iterator
#if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_CONCEPTS)
: public __move_iter_category_base<_Iter>
#endif
{
public:
#if _LIBCPP_STD_VER > 17
typedef input_iterator_tag iterator_concept;
#endif

#if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_CONCEPTS)
using iterator_type = _Iter;
using iterator_concept = input_iterator_tag;
// iterator_category is inherited and not always present
using value_type = iter_value_t<_Iter>;
using difference_type = iter_difference_t<_Iter>;
using pointer = _Iter;
using reference = iter_rvalue_reference_t<_Iter>;
#else
typedef _Iter iterator_type;
typedef _If<
__is_cpp17_random_access_iterator<_Iter>::value,
random_access_iterator_tag,
typename iterator_traits<_Iter>::iterator_category
> iterator_category;
> iterator_category;
typedef typename iterator_traits<iterator_type>::value_type value_type;
typedef typename iterator_traits<iterator_type>::difference_type difference_type;
typedef iterator_type pointer;
Expand All @@ -50,11 +90,56 @@ class _LIBCPP_TEMPLATE_VIS move_iterator
typedef typename iterator_traits<iterator_type>::reference reference;
#endif

#endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_CONCEPTS)

#if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_CONCEPTS)
_LIBCPP_HIDE_FROM_ABI constexpr
explicit move_iterator(_Iter __i) : __current_(std::move(__i)) {}

_LIBCPP_HIDE_FROM_ABI constexpr
move_iterator() requires is_constructible_v<_Iter> : __current_() {}

template <class _Up>
requires (!_IsSame<_Up, _Iter>::value) && convertible_to<const _Up&, _Iter>
_LIBCPP_HIDE_FROM_ABI constexpr
move_iterator(const move_iterator<_Up>& __u) : __current_(__u.base()) {}

template <class _Up>
requires (!_IsSame<_Up, _Iter>::value) &&
convertible_to<const _Up&, _Iter> &&
assignable_from<_Iter&, const _Up&>
_LIBCPP_HIDE_FROM_ABI constexpr
move_iterator& operator=(const move_iterator<_Up>& __u) {
__current_ = __u.base();
return *this;
}

_LIBCPP_HIDE_FROM_ABI constexpr const _Iter& base() const & noexcept { return __current_; }
_LIBCPP_HIDE_FROM_ABI constexpr _Iter base() && { return std::move(__current_); }

_LIBCPP_HIDE_FROM_ABI constexpr
reference operator*() const { return ranges::iter_move(__current_); }
_LIBCPP_HIDE_FROM_ABI constexpr
reference operator[](difference_type __n) const { return ranges::iter_move(__current_ + __n); }

_LIBCPP_HIDE_FROM_ABI constexpr
move_iterator& operator++() { ++__current_; return *this; }

_LIBCPP_HIDE_FROM_ABI constexpr
auto operator++(int)
requires forward_iterator<_Iter>
{
move_iterator __tmp(*this); ++__current_; return __tmp;
}

_LIBCPP_HIDE_FROM_ABI constexpr
void operator++(int) { ++__current_; }
#else
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
move_iterator() : __current_() {}
explicit move_iterator(_Iter __i) : __current_(std::move(__i)) {}

_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
explicit move_iterator(_Iter __i) : __current_(_VSTD::move(__i)) {}
move_iterator() : __current_() {}

template <class _Up, class = __enable_if_t<
!is_same<_Up, _Iter>::value && is_convertible<const _Up&, _Iter>::value
Expand Down Expand Up @@ -87,6 +172,8 @@ class _LIBCPP_TEMPLATE_VIS move_iterator
move_iterator& operator++() { ++__current_; return *this; }
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
move_iterator operator++(int) { move_iterator __tmp(*this); ++__current_; return __tmp; }
#endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_CONCEPTS)

_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
move_iterator& operator--() { --__current_; return *this; }
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
Expand All @@ -100,7 +187,48 @@ class _LIBCPP_TEMPLATE_VIS move_iterator
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
move_iterator& operator-=(difference_type __n) { __current_ -= __n; return *this; }

#if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_CONCEPTS)
template<sentinel_for<_Iter> _Sent>
friend _LIBCPP_HIDE_FROM_ABI constexpr
bool operator==(const move_iterator& __x, const move_sentinel<_Sent>& __y)
requires __move_iter_comparable<_Iter, _Sent>
{
return __x.base() == __y.base();
}

template<sized_sentinel_for<_Iter> _Sent>
friend _LIBCPP_HIDE_FROM_ABI constexpr
iter_difference_t<_Iter> operator-(const move_sentinel<_Sent>& __x, const move_iterator& __y)
{
return __x.base() - __y.base();
}

template<sized_sentinel_for<_Iter> _Sent>
friend _LIBCPP_HIDE_FROM_ABI constexpr
iter_difference_t<_Iter> operator-(const move_iterator& __x, const move_sentinel<_Sent>& __y)
{
return __x.base() - __y.base();
}

friend _LIBCPP_HIDE_FROM_ABI constexpr
iter_rvalue_reference_t<_Iter> iter_move(const move_iterator& __i)
noexcept(noexcept(ranges::iter_move(__i.__current_)))
{
return ranges::iter_move(__i.__current_);
}

template<indirectly_swappable<_Iter> _It2>
friend _LIBCPP_HIDE_FROM_ABI constexpr
void iter_swap(const move_iterator& __x, const move_iterator<_It2>& __y)
noexcept(noexcept(ranges::iter_swap(__x.__current_, __y.__current_)))
{
return ranges::iter_swap(__x.__current_, __y.__current_);
}
#endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_CONCEPTS)

private:
template<class _It2> friend class move_iterator;

_Iter __current_;
};

Expand All @@ -111,12 +239,14 @@ bool operator==(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& _
return __x.base() == __y.base();
}

#if _LIBCPP_STD_VER <= 17
template <class _Iter1, class _Iter2>
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
bool operator!=(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
{
return __x.base() != __y.base();
}
#endif // _LIBCPP_STD_VER <= 17

template <class _Iter1, class _Iter2>
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
Expand Down Expand Up @@ -146,6 +276,20 @@ bool operator>=(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& _
return __x.base() >= __y.base();
}

#if _LIBCPP_STD_VER > 17
# ifdef _LIBCPP_HAS_NO_CONCEPTS
template <class _Iter1, class _Iter2>
# else
template <class _Iter1, three_way_comparable_with<_Iter1> _Iter2>
# endif // _LIBCPP_HAS_NO_CONCEPTS
inline _LIBCPP_HIDE_FROM_ABI constexpr
auto operator<=>(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
-> compare_three_way_result_t<_Iter1, _Iter2>
{
return __x.base() <=> __y.base();
}
#endif // _LIBCPP_STD_VER > 17

#ifndef _LIBCPP_CXX03_LANG
template <class _Iter1, class _Iter2>
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
Expand All @@ -164,20 +308,30 @@ operator-(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
}
#endif

#if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_CONCEPTS)
template <class _Iter>
inline _LIBCPP_HIDE_FROM_ABI constexpr
move_iterator<_Iter> operator+(iter_difference_t<_Iter> __n, const move_iterator<_Iter>& __x)
requires requires { { __x.base() + __n } -> same_as<_Iter>; }
{
return __x + __n;
}
#else
template <class _Iter>
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
move_iterator<_Iter>
operator+(typename move_iterator<_Iter>::difference_type __n, const move_iterator<_Iter>& __x)
{
return move_iterator<_Iter>(__x.base() + __n);
}
#endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_CONCEPTS)

template <class _Iter>
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
move_iterator<_Iter>
make_move_iterator(_Iter __i)
{
return move_iterator<_Iter>(_VSTD::move(__i));
return move_iterator<_Iter>(std::move(__i));
}

_LIBCPP_END_NAMESPACE_STD
Expand Down
57 changes: 57 additions & 0 deletions libcxx/include/__iterator/move_sentinel.h
@@ -0,0 +1,57 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef _LIBCPP___ITERATOR_MOVE_SENTINEL_H
#define _LIBCPP___ITERATOR_MOVE_SENTINEL_H

#include <__concepts/assignable.h>
#include <__concepts/convertible_to.h>
#include <__concepts/semiregular.h>
#include <__config>
#include <__utility/move.h>

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
#endif

_LIBCPP_BEGIN_NAMESPACE_STD

#if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_CONCEPTS)

template <semiregular _Sent>
class _LIBCPP_TEMPLATE_VIS move_sentinel
{
public:
_LIBCPP_HIDE_FROM_ABI
move_sentinel() = default;

_LIBCPP_HIDE_FROM_ABI constexpr
explicit move_sentinel(_Sent __s) : __last_(_VSTD::move(__s)) {}

template <class _S2>
requires convertible_to<const _S2&, _Sent>
_LIBCPP_HIDE_FROM_ABI constexpr
move_sentinel(const move_sentinel<_S2>& __s) : __last_(__s.base()) {}

template <class _S2>
requires assignable_from<_Sent&, const _S2&>
_LIBCPP_HIDE_FROM_ABI constexpr
move_sentinel& operator=(const move_sentinel<_S2>& __s)
{ __last_ = __s.base(); return *this; }

constexpr _Sent base() const { return __last_; }

private:
_Sent __last_ = _Sent();
};

#endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_CONCEPTS)

_LIBCPP_END_NAMESPACE_STD

#endif // _LIBCPP___ITERATOR_MOVE_SENTINEL_H
1 change: 1 addition & 0 deletions libcxx/include/iterator
Expand Up @@ -660,6 +660,7 @@ template <class E> constexpr const E* data(initializer_list<E> il) noexcept;
#include <__iterator/iterator_traits.h>
#include <__iterator/mergeable.h>
#include <__iterator/move_iterator.h>
#include <__iterator/move_sentinel.h>
#include <__iterator/next.h>
#include <__iterator/ostream_iterator.h>
#include <__iterator/ostreambuf_iterator.h>
Expand Down
1 change: 1 addition & 0 deletions libcxx/include/module.modulemap
Expand Up @@ -661,6 +661,7 @@ module std [system] {
module iterator_traits { private header "__iterator/iterator_traits.h" }
module mergeable { private header "__iterator/mergeable.h" }
module move_iterator { private header "__iterator/move_iterator.h" }
module move_sentinel { private header "__iterator/move_sentinel.h" }
module next { private header "__iterator/next.h" }
module ostream_iterator { private header "__iterator/ostream_iterator.h" }
module ostreambuf_iterator {
Expand Down
@@ -0,0 +1,15 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// REQUIRES: modules-build

// WARNING: This test was generated by 'generate_private_header_tests.py'
// and should not be edited manually.

// expected-error@*:* {{use of private header from outside its module: '__iterator/move_sentinel.h'}}
#include <__iterator/move_sentinel.h>

0 comments on commit 2fb026e

Please sign in to comment.