5#ifndef INCLUDE_V8_MEMORY_SPAN_H_
6#define INCLUDE_V8_MEMORY_SPAN_H_
19#if __has_include(<version>)
22#if defined(__cpp_lib_three_way_comparison) && \
23 __cpp_lib_three_way_comparison >= 201711L
24#define V8_HAVE_SPACESHIP_OPERATOR 1
26#define V8_HAVE_SPACESHIP_OPERATOR 0
31#if __has_include(<ranges>)
45inline constexpr bool std::ranges::enable_view<v8::MemorySpan<T>> =
true;
47inline constexpr bool std::ranges::enable_borrowed_range<v8::MemorySpan<T>> =
67 template <
typename From,
typename To>
68 using is_array_convertible = std::is_convertible<From (*)[], To (*)[]>;
69 template <
typename From,
typename To>
70 static constexpr bool is_array_convertible_v =
71 is_array_convertible<From, To>::value;
73 template <
typename It>
74 using iter_reference_t =
decltype(*std::declval<It&>());
76 template <
typename It,
typename =
void>
77 struct is_compatible_iterator : std::false_type {};
78 template <
typename It>
79 struct is_compatible_iterator<
82 std::is_base_of<std::random_access_iterator_tag,
83 typename std::iterator_traits<It>::iterator_category>,
84 is_array_convertible<std::remove_reference_t<iter_reference_t<It>>,
85 T>>> : std::true_type {};
86 template <
typename It>
87 static constexpr bool is_compatible_iterator_v =
88 is_compatible_iterator<It>::value;
91 [[nodiscard]]
static constexpr U* to_address(U* p)
noexcept {
95 template <
typename It,
96 typename = std::void_t<decltype(std::declval<It&>().operator->())>>
97 [[nodiscard]]
static constexpr auto to_address(It it)
noexcept {
98 return it.operator->();
111 template <
typename Iterator,
112 std::enable_if_t<is_compatible_iterator_v<Iterator>,
bool> =
true>
115 : data_(to_address(first)), size_(count) {}
118 template <
typename Iterator,
119 std::enable_if_t<is_compatible_iterator_v<Iterator> &&
120 !std::is_convertible_v<Iterator, size_t>,
124 : data_(to_address(first)), size_(last - first) {}
129 : data_(a), size_(N) {}
132 template <
typename U,
size_t N,
133 std::enable_if_t<is_array_convertible_v<U, T>,
bool> =
true>
135 std::array<U, N>& a)
noexcept
136 : data_(a.data()), size_{N} {}
139 template <
typename U,
size_t N,
140 std::enable_if_t<is_array_convertible_v<const U, T>,
bool> =
true>
142 const std::array<U, N>& a)
noexcept
143 : data_(a.data()), size_{N} {}
146 [[nodiscard]]
constexpr T*
data()
const {
return data_; }
148 [[nodiscard]]
constexpr size_t size()
const {
return size_; }
150 [[nodiscard]]
constexpr T&
operator[](
size_t i)
const {
return data_[i]; }
153 [[nodiscard]]
constexpr bool empty()
const {
return size() == 0; }
167#if __has_include(<ranges>)
168 using iterator_concept = std::contiguous_iterator_tag;
178 return a.ptr_ == b.ptr_;
180#if V8_HAVE_SPACESHIP_OPERATOR
181 [[nodiscard]]
friend constexpr auto operator<=>(
const Iterator&,
188 return a.ptr_ != b.ptr_;
193 return a.ptr_ < b.ptr_;
197 return a.ptr_ <= b.ptr_;
201 return a.ptr_ > b.ptr_;
205 return a.ptr_ >= b.ptr_;
260 return lhs.ptr_ - rhs.ptr_;
265 [[nodiscard]]
constexpr reference operator[](
size_t offset)
const {
272 constexpr explicit Iterator(T* ptr) : ptr_(ptr) {}
297template <
class T, std::size_t N, std::size_t... I>
299 T (&a)[N], std::index_sequence<I...>) {
303template <
class T, std::size_t N, std::size_t... I>
305 T (&&a)[N], std::index_sequence<I...>) {
306 return {{std::move(a[I])...}};
310template <
class T, std::
size_t N>
311[[nodiscard]]
constexpr std::array<std::remove_cv_t<T>, N>
to_array(T (&a)[N]) {
315template <
class T, std::
size_t N>
316[[nodiscard]]
constexpr std::array<std::remove_cv_t<T>, N>
to_array(
319 std::make_index_sequence<N>{});
Definition: v8-memory-span.h:155
std::ptrdiff_t difference_type
Definition: v8-memory-span.h:157
friend constexpr bool operator>(const Iterator &a, const Iterator &b)
Definition: v8-memory-span.h:199
friend constexpr difference_type operator-(const Iterator &lhs, const Iterator &rhs)
Definition: v8-memory-span.h:258
constexpr Iterator & operator+=(difference_type rhs)
Definition: v8-memory-span.h:231
friend constexpr Iterator operator+(Iterator lhs, difference_type rhs)
Definition: v8-memory-span.h:236
constexpr Iterator & operator++()
Definition: v8-memory-span.h:209
friend constexpr bool operator>=(const Iterator &a, const Iterator &b)
Definition: v8-memory-span.h:203
friend constexpr bool operator==(const Iterator &a, const Iterator &b)
Definition: v8-memory-span.h:174
std::random_access_iterator_tag iterator_category
Definition: v8-memory-span.h:161
T value_type
Definition: v8-memory-span.h:158
constexpr Iterator()=default
friend constexpr bool operator!=(const Iterator &a, const Iterator &b)
Definition: v8-memory-span.h:186
value_type & reference
Definition: v8-memory-span.h:160
constexpr reference operator*() const
Definition: v8-memory-span.h:263
friend constexpr Iterator operator+(difference_type lhs, const Iterator &rhs)
Definition: v8-memory-span.h:242
constexpr Iterator operator--(int)
Definition: v8-memory-span.h:225
constexpr Iterator operator++(int)
Definition: v8-memory-span.h:214
value_type * pointer
Definition: v8-memory-span.h:159
friend constexpr bool operator<(const Iterator &a, const Iterator &b)
Definition: v8-memory-span.h:191
constexpr Iterator & operator-=(difference_type rhs)
Definition: v8-memory-span.h:247
friend constexpr Iterator operator-(Iterator lhs, difference_type rhs)
Definition: v8-memory-span.h:252
friend constexpr bool operator<=(const Iterator &a, const Iterator &b)
Definition: v8-memory-span.h:195
constexpr Iterator & operator--()
Definition: v8-memory-span.h:220
constexpr pointer operator->() const
Definition: v8-memory-span.h:264
Definition: v8-memory-span.h:64
constexpr MemorySpan(std::nullptr_t, size_t)
Definition: v8-memory-span.h:108
Iterator end() const
Definition: v8-memory-span.h:278
constexpr MemorySpan(std::array< U, N > &a) noexcept
Definition: v8-memory-span.h:134
constexpr MemorySpan()=default
constexpr T & operator[](size_t i) const
Definition: v8-memory-span.h:150
constexpr MemorySpan(const std::array< U, N > &a) noexcept
Definition: v8-memory-span.h:141
constexpr MemorySpan(Iterator first, size_t count)
Definition: v8-memory-span.h:113
constexpr MemorySpan(T(&a)[N]) noexcept
Definition: v8-memory-span.h:128
constexpr T * data() const
Definition: v8-memory-span.h:146
constexpr MemorySpan(Iterator first, Iterator last)
Definition: v8-memory-span.h:122
constexpr bool empty() const
Definition: v8-memory-span.h:153
Iterator begin() const
Definition: v8-memory-span.h:277
constexpr size_t size() const
Definition: v8-memory-span.h:148
constexpr std::array< std::remove_cv_t< T >, N > to_array_rvalue_impl(T(&&a)[N], std::index_sequence< I... >)
Definition: v8-memory-span.h:304
constexpr std::array< std::remove_cv_t< T >, N > to_array_lvalue_impl(T(&a)[N], std::index_sequence< I... >)
Definition: v8-memory-span.h:298
Definition: libplatform.h:15
constexpr std::array< std::remove_cv_t< T >, N > to_array(T(&a)[N])
Definition: v8-memory-span.h:311
#define V8_EXPORT
Definition: v8config.h:793