Skip to content

Commit

Permalink
Add test for <ranges> (#16)
Browse files Browse the repository at this point in the history
* Fix ranges::data

* Add test for <ranges>

* Update CMakeLists.txt

* Update ranges.cc

* Update ranges.cc

* Create ranges_concepts.cc

* Fix zip_view

* Sync enable_borrwed_range with STL

* Fix android
  • Loading branch information
lackhole authored Jun 22, 2024
1 parent a633094 commit 9f9023a
Show file tree
Hide file tree
Showing 34 changed files with 505 additions and 129 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -717,16 +717,16 @@ Description
| `ranges::chunk_view`<br/>`views::chunk` | ![][c23no] | |
| `ranges::chunk_by_view`<br/>`views::chunk_by` | ![][c23no] | |
| `ranges::concat_view`<br/>`views::concat` | ![][c26ok] | |
| `ranges::begin` | ![][c20ok] | |
| `ranges::end` | ![][c20ok] | |
| `ranges::cbegin` | ![][c20ok] | |
| `ranges::cend` | ![][c20ok] | |
| `ranges::crbegin` | ![][c20ok] | |
| `ranges::crend` | ![][c20ok] | |
| `ranges::size` | ![][c20ok] | |
| `ranges::ssize` | ![][c20ok] | |
| `ranges::empty` | ![][c20ok] | |
| `ranges::data` | ![][c20ok] | |
| `ranges::begin` | ![][c20ok] | ![][c23ok] |
| `ranges::end` | ![][c20ok] | ![][c23ok] |
| `ranges::cbegin` | ![][c20ok] | ![][c23ok] |
| `ranges::cend` | ![][c20ok] | ![][c23ok] |
| `ranges::crbegin` | ![][c20ok] | ![][c23ok] |
| `ranges::crend` | ![][c20ok] | ![][c23ok] |
| `ranges::size` | ![][c20ok] | ![][c23ok] |
| `ranges::ssize` | ![][c20ok] | ![][c23ok] |
| `ranges::empty` | ![][c20ok] | ![][c23ok] |
| `ranges::data` | ![][c20ok] | ![][c23ok] |
| `ranges::cdata` | ![][c20ok] | ![][c23ok] |
| `ranges::subrange_kind` | ![][c20ok] | |
| `ranges::from_range_t`<br/>`ranges::from_range` | ![][c23ok] | |
Expand Down
2 changes: 1 addition & 1 deletion include/preview/__ranges/begin.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ struct begin_category
: std::conditional_t<
disjunction<
std::is_lvalue_reference<T>,
enable_borrowed_range<remove_cvref_t<T>>
enable_borrowed_range_t<remove_cvref_t<T>>
>::value,
typename begin_category_impl<T>::category,
return_category<0>
Expand Down
2 changes: 1 addition & 1 deletion include/preview/__ranges/borrowed_range.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ struct borrowed_range
range<R>,
disjunction<
std::is_lvalue_reference<R>,
enable_borrowed_range<remove_cvref_t<R>>
enable_borrowed_range_t<remove_cvref_t<R>>
>
> {};

Expand Down
2 changes: 1 addition & 1 deletion include/preview/__ranges/cbegin.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace detail {
struct cbegin_niebloid {
template<typename T, std::enable_if_t<disjunction<
std::is_lvalue_reference<T>,
enable_borrowed_range<std::remove_cv_t<T>>
enable_borrowed_range_t<std::remove_cv_t<T>>
>::value, int> = 0>
constexpr auto operator()(T&& t) const {
return const_iterator<decltype(ranges::begin(preview::ranges::possibly_const_range(t)))>(
Expand Down
2 changes: 1 addition & 1 deletion include/preview/__ranges/cdata.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace detail {
struct cdata_niebloid {
template<typename T, std::enable_if_t<disjunction<
std::is_lvalue_reference<T>,
enable_borrowed_range<std::remove_cv_t<T>>
enable_borrowed_range_t<std::remove_cv_t<T>>
>::value, int> = 0>
constexpr std::remove_reference_t<range_const_reference_t<T>>* operator()(T&& t) const {
return preview::ranges::as_const_pointer(ranges::data(possibly_const_range(t)));
Expand Down
2 changes: 1 addition & 1 deletion include/preview/__ranges/cend.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace detail {
struct cend_niebloid {
template<typename T, std::enable_if_t<disjunction<
std::is_lvalue_reference<T>,
enable_borrowed_range<std::remove_cv_t<T>>
enable_borrowed_range_t<std::remove_cv_t<T>>
>::value, int> = 0>
constexpr auto operator()(T&& t) const {
return preview::const_sentinel<decltype(ranges::end(possibly_const_range(t)))>(ranges::end(possibly_const_range(t)));
Expand Down
2 changes: 1 addition & 1 deletion include/preview/__ranges/crbegin.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace detail {
struct crbegin_niebloid {
template<typename T, std::enable_if_t<disjunction<
std::is_lvalue_reference<T>,
enable_borrowed_range<std::remove_cv_t<T>>
enable_borrowed_range_t<std::remove_cv_t<T>>
>::value, int> = 0>
constexpr auto operator()(T&& t) const {
return preview::const_iterator<decltype(ranges::rbegin(possibly_const_range(t)))>(ranges::rbegin(possibly_const_range(t)));
Expand Down
2 changes: 1 addition & 1 deletion include/preview/__ranges/crend.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace detail {
struct crend_niebloid {
template<typename T, std::enable_if_t<disjunction<
std::is_lvalue_reference<T>,
enable_borrowed_range<std::remove_cv_t<T>>
enable_borrowed_range_t<std::remove_cv_t<T>>
>::value, int> = 0>
constexpr auto operator()(T&& t) const {
return preview::const_sentinel<decltype(ranges::rend(possibly_const_range(t)))>(ranges::rend(possibly_const_range(t)));
Expand Down
118 changes: 65 additions & 53 deletions include/preview/__ranges/data.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,75 +11,87 @@
#include "preview/__core/decay_copy.h"
#include "preview/__iterator/contiguous_iterator.h"
#include "preview/__memory/to_address.h"
#include "preview/__ranges/detail/not_incomplete_array.h"
#include "preview/__ranges/begin.h"
#include "preview/__type_traits/detail/return_category.h"
#include "preview/__ranges/range_reference_t.h"
#include "preview/__type_traits/disjunction.h"
#include "preview/__type_traits/void_t.h"

namespace preview {
namespace ranges {
namespace detail {

using preview::detail::return_category;

template<typename P>
struct data_member_check_2 : std::false_type {
using category = return_category<0>;
};
template<typename P>
struct data_member_check_2<P*> : std::is_object<P> {
using category = return_category<1, P*>;
};

template<typename T, typename = void>
struct data_member_check : std::false_type {
using category = return_category<0>;
};
template<typename T>
struct data_member_check<T, void_t<decltype( preview_decay_copy(std::declval<T>().data()) )>>
: data_member_check_2<decltype( preview_decay_copy(std::declval<T>().data()) )> {};


template<typename T, typename = void>
struct data_ranges_begin_check : std::false_type {
using category = return_category<0>;
};
template<typename T>
struct data_ranges_begin_check<T, void_t<decltype( ranges::begin(std::declval<T>()) )>>
: contiguous_iterator<decltype( ranges::begin(std::declval<T>()) )>
{
using category = return_category<2, decltype( preview::to_address( ranges::begin(std::declval<T>()) ) )>;
};
struct data_niebloid {
private:
template<typename T>
struct is_pointer_to_object : std::false_type {};
template<typename T>
struct is_pointer_to_object<T*> : std::is_object<T*> {};

template<typename T, typename = void>
struct member_function_data_is_pointer_to_object
: std::false_type {};
template<typename T>
struct member_function_data_is_pointer_to_object<T, void_t<decltype( preview_decay_copy(std::declval<T>().data()) )>>
: conjunction<
is_pointer_to_object<decltype( preview_decay_copy(std::declval<T>().data()) )>,
std::is_convertible<
decltype( preview_decay_copy(std::declval<T>().data()) ),
std::remove_reference_t<ranges::range_reference_t<T>>*
>
> {};

template<typename T, typename = void>
struct ranges_begin_is_contiguous_iterator
: std::false_type {};
template<typename T>
struct ranges_begin_is_contiguous_iterator<T, void_t<decltype( ranges::begin(std::declval<T>()) )>>
: conjunction<
contiguous_iterator<decltype( ranges::begin(std::declval<T>()) )>,
std::is_convertible<
decltype( ranges::begin(std::declval<T>()) ),
std::remove_reference_t<ranges::range_reference_t<T>>*
>
> {};

template<typename T>
constexpr std::remove_reference_t<ranges::range_reference_t<T>>*
call(T&& t, std::true_type) const {
return preview_decay_copy(t.data());
}

template<typename T>
struct data_category
: std::conditional_t<
data_member_check<T>::value, typename data_member_check<T>::category,
std::conditional_t<
data_ranges_begin_check<T>::value, typename data_ranges_begin_check<T>::category,
return_category<0>
>>{};
template<typename T>
constexpr std::remove_reference_t<ranges::range_reference_t<T>>*
call(T&& t, std::false_type) const {
return preview::to_address(ranges::begin(t));
}

struct data_niebloid {
template<typename T, std::enable_if_t<disjunction<
std::is_lvalue_reference<T&&>,
ranges::enable_borrowed_range<remove_cvref_t<T>>
public:
template<typename T, std::enable_if_t<conjunction<
preview::detail::not_incomplete_array<T>,
disjunction<
std::is_lvalue_reference<T&&>,
enable_borrowed_range_t<remove_cvref_t<T>>
>,
has_typename_type<range_reference<T>>,
disjunction<
member_function_data_is_pointer_to_object<T&&>,
ranges_begin_is_contiguous_iterator<T&&>
>
>::value, int> = 0>
constexpr typename data_category<T&&>::return_type
constexpr std::remove_reference_t<ranges::range_reference_t<T>>*
operator()(T&& t) const {
return (*this)(std::forward<T>(t), data_category<T&&>{});
return call(std::forward<T>(t), member_function_data_is_pointer_to_object<T&&>{});
}

private:
template<typename T, typename R>
constexpr R operator()(T&& t, return_category<1, R>) const {
return preview_decay_copy(t.data());
}
#if PREVIEW_CXX_VERSION < 17
// non-const version of std::basic_string::data was introduced since C++17

template<typename T, typename R>
constexpr R operator()(T&& t, return_category<2, R>) const {
return preview::to_address(ranges::begin(t));
template<typename CharT, typename Traits, typename Allocator>
constexpr CharT* operator()(std::basic_string<CharT, Traits, Allocator>& str) const {
return const_cast<CharT*>(str.data());
}
#endif
};

} // namespace detail
Expand Down
16 changes: 16 additions & 0 deletions include/preview/__ranges/detail/have_cxx20_ranges.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# /**
# * Created by YongGyu Lee on 2024. 6. 22
# */
#
#ifndef PREVIEW_RANGES_DETAIL_HAVE_CXX20_RANGES_H_
#define PREVIEW_RANGES_DETAIL_HAVE_CXX20_RANGES_H_
#
#include "preview/core.h"
#
#if PREVIEW_CXX_VERSION >= 20 && (PREVIEW_ANDROID == 0 || (defined(PREVIEW_NDK_VERSION_MAJOR) && PREVIEW_NDK_VERSION_MAJOR >= 26))
# define PREVIEW_HAVE_CXX20_RANGES 1
#else
# define PREVIEW_HAVE_CXX20_RANGES 0
#endif
#
#endif // PREVIEW_RANGES_DETAIL_HAVE_CXX20_RANGES_H_
34 changes: 32 additions & 2 deletions include/preview/__ranges/enable_borrowed_range.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,46 @@

#ifndef PREVIEW_RANGES_ENABLE_BORROWED_RANGE_H_
#define PREVIEW_RANGES_ENABLE_BORROWED_RANGE_H_

#
#include <type_traits>
#
#include "preview/core.h"
#include "preview/__ranges/detail/have_cxx20_ranges.h"

#if PREVIEW_HAVE_CXX20_RANGES
#include <ranges>
#endif

namespace preview {
namespace ranges {

#if PREVIEW_HAVE_CXX20_RANGES
template<typename R>
PREVIEW_INLINE_VARIABLE constexpr bool enable_borrowed_range = std::ranges::enable_borrowed_range<R>;
#else
template<typename R>
PREVIEW_INLINE_VARIABLE constexpr bool enable_borrowed_range = false;
#endif

template<typename R>
struct enable_borrowed_range : std::false_type {};
struct enable_borrowed_range_t : std::integral_constant<bool, enable_borrowed_range<R>> {};

} // namespace preview
} // namespace ranges

#if PREVIEW_HAVE_CXX20_RANGES
# define PREVIEW_SPECIALIZE_ENABLE_BORROWED_RANGE(...) \
inline constexpr bool std::ranges::enable_borrowed_range<__VA_ARGS__>
#else
# define PREVIEW_SPECIALIZE_ENABLE_BORROWED_RANGE(...) \
PREVIEW_INLINE_VARIABLE constexpr bool preview::ranges::enable_borrowed_range<__VA_ARGS__>
#endif

#if 17 <= PREVIEW_CXX_VERSION && PREVIEW_CXX_VERSION < 20
#include <string_view>

template<typename CharT, typename Traits>
PREVIEW_SPECIALIZE_ENABLE_BORROWED_RANGE(std::basic_string_view<CharT, Traits>) = true;
#endif

#endif // PREVIEW_RANGES_ENABLE_BORROWED_RANGE_H_
1 change: 1 addition & 0 deletions include/preview/__ranges/enable_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace preview {
namespace ranges {

// TODO: Sync with std::ranges::enable_view
template<typename T>
struct enable_view : disjunction<
derived_from<T, view_base>,
Expand Down
2 changes: 1 addition & 1 deletion include/preview/__ranges/end.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ struct end_niebloid {
: std::conditional_t<
disjunction<
std::is_lvalue_reference<T>,
enable_borrowed_range<remove_cvref_t<T>>
enable_borrowed_range_t<remove_cvref_t<T>>
>::value,
typename end_category_impl<T>::category,
return_category<0>
Expand Down
13 changes: 7 additions & 6 deletions include/preview/__ranges/owning_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Created by yonggyulee on 2024/01/02.
//

#ifndef PREVIEW_RANGES_OWINING_VIEW_H_
#define PREVIEW_RANGES_OWINING_VIEW_H_
#ifndef PREVIEW_RANGES_OWNING_VIEW_H_
#define PREVIEW_RANGES_OWNING_VIEW_H_

#include <utility>

Expand Down Expand Up @@ -112,10 +112,11 @@ owning_view(R&&) -> owning_view<R>;

#endif

template<typename T>
struct enable_borrowed_range<owning_view<T>> : enable_borrowed_range<T> {};

} // namespace ranges
} // namespace namespace preview

#endif // PREVIEW_RANGES_OWINING_VIEW_H_
template<typename T>
PREVIEW_SPECIALIZE_ENABLE_BORROWED_RANGE(preview::ranges::owning_view<T>)
= preview::ranges::enable_borrowed_range<T>;

#endif // PREVIEW_RANGES_OWNING_VIEW_H_
2 changes: 1 addition & 1 deletion include/preview/__ranges/rbegin.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ struct rbegin_niebloid {
preview::detail::not_incomplete_array<T>,
disjunction<
std::is_lvalue_reference<std::remove_cv_t<T>>,
enable_borrowed_range<std::remove_cv_t<T>>
enable_borrowed_range_t<std::remove_cv_t<T>>
>,
bool_constant<(rbegin_tag<T>::value > 0)>
>::value, int> = 0>
Expand Down
6 changes: 3 additions & 3 deletions include/preview/__ranges/ref_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ ref_view(R&) -> ref_view<R>;

#endif

template<typename T>
struct enable_borrowed_range<ref_view<T>> : std::true_type {};

} // namespace ranges
} // namespace namespace preview

template<typename T>
PREVIEW_SPECIALIZE_ENABLE_BORROWED_RANGE(preview::ranges::ref_view<T>) = true;

#endif // PREVIEW_RANGES_REF_VIEW_H_
2 changes: 1 addition & 1 deletion include/preview/__ranges/rend.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ struct rend_niebloid {
preview::detail::not_incomplete_array<T>,
disjunction<
std::is_lvalue_reference<std::remove_cv_t<T>>,
enable_borrowed_range<std::remove_cv_t<T>>
enable_borrowed_range_t<std::remove_cv_t<T>>
>,
bool_constant<(rend_tag<T>::value > 0)>
>::value, int> = 0>
Expand Down
6 changes: 3 additions & 3 deletions include/preview/__ranges/subrange.h
Original file line number Diff line number Diff line change
Expand Up @@ -374,9 +374,6 @@ constexpr auto get(subrange<I, S, K>&& r) {
return detail::get_subrange<N>::get(std::move(r));
}

template<typename I, typename S, subrange_kind K>
struct enable_borrowed_range<subrange<I, S, K>> : std::true_type {};

} // namespace ranges

namespace internal {
Expand Down Expand Up @@ -407,4 +404,7 @@ using ::preview::ranges::get;

} // namespace std

template<typename I, typename S, preview::ranges::subrange_kind K>
PREVIEW_SPECIALIZE_ENABLE_BORROWED_RANGE(preview::ranges::subrange<I, S, K>) = true;

#endif // PREVIEW_RANGES_SUBRANGE_H_
Loading

0 comments on commit 9f9023a

Please sign in to comment.