Skip to content

Commit e0adf7e

Browse files
zoecarverldionne
authored andcommitted
[libc++][NFC] Move incrementable_traits and indirectly_readable_traits into separate headers.
Differential Revision: https://reviews.llvm.org/D100682
1 parent 87afefc commit e0adf7e

File tree

6 files changed

+196
-93
lines changed

6 files changed

+196
-93
lines changed

libcxx/include/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ set(files
1111
__functional_base
1212
__functional_base_03
1313
__hash_table
14+
__iterator/concepts.h
15+
__iterator/incrementable_traits.h
16+
__iterator/readable_traits.h
1417
__libcpp_version
1518
__locale
1619
__memory/addressof.h
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// -*- C++ -*-
2+
//===----------------------------------------------------------------------===//
3+
//
4+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5+
// See https://llvm.org/LICENSE.txt for license information.
6+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
#ifndef _LIBCPP___ITERATOR_CONCEPTS_H
11+
#define _LIBCPP___ITERATOR_CONCEPTS_H
12+
13+
#include <__config>
14+
#include <concepts>
15+
16+
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
17+
#pragma GCC system_header
18+
#endif
19+
20+
_LIBCPP_PUSH_MACROS
21+
#include <__undef_macros>
22+
23+
_LIBCPP_BEGIN_NAMESPACE_STD
24+
25+
#if !defined(_LIBCPP_HAS_NO_RANGES)
26+
27+
template<class _Tp>
28+
using __with_reference = _Tp&;
29+
30+
template<class _Tp>
31+
concept __referenceable = requires {
32+
typename __with_reference<_Tp>;
33+
};
34+
35+
template<class _Tp>
36+
concept __dereferenceable = requires(_Tp& __t) {
37+
{ *__t } -> __referenceable; // not required to be equality-preserving
38+
};
39+
40+
#endif // !defined(_LIBCPP_HAS_NO_RANGES)
41+
42+
_LIBCPP_END_NAMESPACE_STD
43+
44+
_LIBCPP_POP_MACROS
45+
46+
#endif // _LIBCPP___ITERATOR_CONCEPTS_H
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// -*- C++ -*-
2+
//===----------------------------------------------------------------------===//
3+
//
4+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5+
// See https://llvm.org/LICENSE.txt for license information.
6+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
#ifndef _LIBCPP___ITERATOR_INCREMENTABLE_TRAITS_H
11+
#define _LIBCPP___ITERATOR_INCREMENTABLE_TRAITS_H
12+
13+
#include <__config>
14+
#include <__iterator/concepts.h>
15+
16+
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
17+
#pragma GCC system_header
18+
#endif
19+
20+
_LIBCPP_PUSH_MACROS
21+
#include <__undef_macros>
22+
23+
_LIBCPP_BEGIN_NAMESPACE_STD
24+
25+
#if !defined(_LIBCPP_HAS_NO_RANGES)
26+
27+
// [incrementable.traits]
28+
template<class> struct incrementable_traits {};
29+
30+
template<class _Tp>
31+
requires is_object_v<_Tp>
32+
struct incrementable_traits<_Tp*> {
33+
using difference_type = ptrdiff_t;
34+
};
35+
36+
template<class _Ip>
37+
struct incrementable_traits<const _Ip> : incrementable_traits<_Ip> {};
38+
39+
template<class _Tp>
40+
concept __has_member_difference_type = requires { typename _Tp::difference_type; };
41+
42+
template<__has_member_difference_type _Tp>
43+
struct incrementable_traits<_Tp> {
44+
using difference_type = typename _Tp::difference_type;
45+
};
46+
47+
template<class _Tp>
48+
concept __has_integral_minus =
49+
requires(const _Tp& __x, const _Tp& __y) {
50+
{ __x - __y } -> integral;
51+
};
52+
53+
template<__has_integral_minus _Tp>
54+
requires (!__has_member_difference_type<_Tp>)
55+
struct incrementable_traits<_Tp> {
56+
using difference_type = make_signed_t<decltype(declval<_Tp>() - declval<_Tp>())>;
57+
};
58+
59+
#endif // !defined(_LIBCPP_HAS_NO_RANGES)
60+
61+
_LIBCPP_END_NAMESPACE_STD
62+
63+
_LIBCPP_POP_MACROS
64+
65+
#endif // _LIBCPP___ITERATOR_INCREMENTABLE_TRAITS_H
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// -*- C++ -*-
2+
//===----------------------------------------------------------------------===//
3+
//
4+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5+
// See https://llvm.org/LICENSE.txt for license information.
6+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
#ifndef _LIBCPP___ITERATOR_READABLE_TRAITS_H
11+
#define _LIBCPP___ITERATOR_READABLE_TRAITS_H
12+
13+
#include <__config>
14+
#include <__iterator/concepts.h>
15+
16+
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
17+
#pragma GCC system_header
18+
#endif
19+
20+
_LIBCPP_PUSH_MACROS
21+
#include <__undef_macros>
22+
23+
_LIBCPP_BEGIN_NAMESPACE_STD
24+
25+
#if !defined(_LIBCPP_HAS_NO_RANGES)
26+
27+
// [readable.traits]
28+
template<class> struct __cond_value_type {};
29+
30+
template<class _Tp>
31+
requires is_object_v<_Tp>
32+
struct __cond_value_type<_Tp> { using value_type = remove_cv_t<_Tp>; };
33+
34+
template<class _Tp>
35+
concept __has_member_value_type = requires { typename _Tp::value_type; };
36+
37+
template<class _Tp>
38+
concept __has_member_element_type = requires { typename _Tp::element_type; };
39+
40+
template<class> struct indirectly_readable_traits {};
41+
42+
template<class _Ip>
43+
requires is_array_v<_Ip>
44+
struct indirectly_readable_traits<_Ip> {
45+
using value_type = remove_cv_t<remove_extent_t<_Ip>>;
46+
};
47+
48+
template<class _Ip>
49+
struct indirectly_readable_traits<const _Ip> : indirectly_readable_traits<_Ip> {};
50+
51+
template<class _Tp>
52+
struct indirectly_readable_traits<_Tp*> : __cond_value_type<_Tp> {};
53+
54+
template<__has_member_value_type _Tp>
55+
struct indirectly_readable_traits<_Tp>
56+
: __cond_value_type<typename _Tp::value_type> {};
57+
58+
template<__has_member_element_type _Tp>
59+
struct indirectly_readable_traits<_Tp>
60+
: __cond_value_type<typename _Tp::element_type> {};
61+
62+
// Pre-emptively applies LWG3541
63+
template<__has_member_value_type _Tp>
64+
requires __has_member_element_type<_Tp>
65+
struct indirectly_readable_traits<_Tp> {};
66+
template<__has_member_value_type _Tp>
67+
requires __has_member_element_type<_Tp> &&
68+
same_as<remove_cv_t<typename _Tp::element_type>,
69+
remove_cv_t<typename _Tp::value_type>>
70+
struct indirectly_readable_traits<_Tp>
71+
: __cond_value_type<typename _Tp::value_type> {};
72+
73+
#endif // !defined(_LIBCPP_HAS_NO_RANGES)
74+
75+
_LIBCPP_END_NAMESPACE_STD
76+
77+
_LIBCPP_POP_MACROS
78+
79+
#endif // _LIBCPP___ITERATOR_READABLE_TRAITS_H

libcxx/include/concepts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,7 @@ concept equivalence_relation = relation<_Rp, _Tp, _Up>;
439439
// [concept.strictweakorder]
440440
template<class _Rp, class _Tp, class _Up>
441441
concept strict_weak_order = relation<_Rp, _Tp, _Up>;
442+
442443
#endif //_LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_CONCEPTS)
443444

444445
_LIBCPP_END_NAMESPACE_STD

libcxx/include/iterator

Lines changed: 2 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,8 @@ template <class E> constexpr const E* data(initializer_list<E> il) noexcept;
429429
#include <concepts>
430430
#include <cstddef>
431431
#include <initializer_list>
432+
#include <__iterator/incrementable_traits.h>
433+
#include <__iterator/readable_traits.h>
432434
#include <__memory/addressof.h>
433435
#include <__memory/pointer_traits.h>
434436
#include <version>
@@ -443,99 +445,6 @@ _LIBCPP_BEGIN_NAMESPACE_STD
443445

444446
#if !defined(_LIBCPP_HAS_NO_RANGES)
445447

446-
template<class _Tp>
447-
using __with_reference = _Tp&;
448-
449-
template<class _Tp>
450-
concept __referenceable = requires {
451-
typename __with_reference<_Tp>;
452-
};
453-
454-
template<class _Tp>
455-
concept __dereferenceable = requires(_Tp& __t) {
456-
{ *__t } -> __referenceable; // not required to be equality-preserving
457-
};
458-
459-
// [incrementable.traits]
460-
template<class> struct incrementable_traits {};
461-
462-
template<class _Tp>
463-
requires is_object_v<_Tp>
464-
struct incrementable_traits<_Tp*> {
465-
using difference_type = ptrdiff_t;
466-
};
467-
468-
template<class _Ip>
469-
struct incrementable_traits<const _Ip> : incrementable_traits<_Ip> {};
470-
471-
template<class _Tp>
472-
concept __has_member_difference_type = requires { typename _Tp::difference_type; };
473-
474-
template<__has_member_difference_type _Tp>
475-
struct incrementable_traits<_Tp> {
476-
using difference_type = typename _Tp::difference_type;
477-
};
478-
479-
template<class _Tp>
480-
concept __has_integral_minus =
481-
requires(const _Tp& __x, const _Tp& __y) {
482-
{ __x - __y } -> integral;
483-
};
484-
485-
template<__has_integral_minus _Tp>
486-
requires (!__has_member_difference_type<_Tp>)
487-
struct incrementable_traits<_Tp> {
488-
using difference_type = make_signed_t<decltype(declval<_Tp>() - declval<_Tp>())>;
489-
};
490-
491-
// TODO(cjdb): add iter_difference_t once iterator_traits is cleaned up.
492-
493-
// [readable.traits]
494-
template<class> struct __cond_value_type {};
495-
496-
template<class _Tp>
497-
requires is_object_v<_Tp>
498-
struct __cond_value_type<_Tp> { using value_type = remove_cv_t<_Tp>; };
499-
500-
template<class _Tp>
501-
concept __has_member_value_type = requires { typename _Tp::value_type; };
502-
503-
template<class _Tp>
504-
concept __has_member_element_type = requires { typename _Tp::element_type; };
505-
506-
template<class> struct indirectly_readable_traits {};
507-
508-
template<class _Ip>
509-
requires is_array_v<_Ip>
510-
struct indirectly_readable_traits<_Ip> {
511-
using value_type = remove_cv_t<remove_extent_t<_Ip>>;
512-
};
513-
514-
template<class _Ip>
515-
struct indirectly_readable_traits<const _Ip> : indirectly_readable_traits<_Ip> {};
516-
517-
template<class _Tp>
518-
struct indirectly_readable_traits<_Tp*> : __cond_value_type<_Tp> {};
519-
520-
template<__has_member_value_type _Tp>
521-
struct indirectly_readable_traits<_Tp>
522-
: __cond_value_type<typename _Tp::value_type> {};
523-
524-
template<__has_member_element_type _Tp>
525-
struct indirectly_readable_traits<_Tp>
526-
: __cond_value_type<typename _Tp::element_type> {};
527-
528-
// Pre-emptively applies LWG3541
529-
template<__has_member_value_type _Tp>
530-
requires __has_member_element_type<_Tp>
531-
struct indirectly_readable_traits<_Tp> {};
532-
template<__has_member_value_type _Tp>
533-
requires __has_member_element_type<_Tp> &&
534-
same_as<remove_cv_t<typename _Tp::element_type>,
535-
remove_cv_t<typename _Tp::value_type>>
536-
struct indirectly_readable_traits<_Tp>
537-
: __cond_value_type<typename _Tp::value_type> {};
538-
539448
// [iterator.traits]
540449
template<__dereferenceable _Tp>
541450
using iter_reference_t = decltype(*declval<_Tp&>());

0 commit comments

Comments
 (0)