5#ifndef INCLUDE_V8_MEMORY_SPAN_H_
6#define INCLUDE_V8_MEMORY_SPAN_H_
18#if __has_include(<version>)
21#if defined(__cpp_lib_three_way_comparison) && \
22 __cpp_lib_three_way_comparison >= 201711L
23#define V8_HAVE_SPACESHIP_OPERATOR 1
25#define V8_HAVE_SPACESHIP_OPERATOR 0
30#if __has_include(<ranges>)
44inline constexpr bool std::ranges::enable_view<v8::MemorySpan<T>> =
true;
46inline constexpr bool std::ranges::enable_borrowed_range<v8::MemorySpan<T>> =
66 template <
typename From,
typename To>
67 using is_array_convertible = std::is_convertible<From (*)[], To (*)[]>;
68 template <
typename From,
typename To>
69 static constexpr bool is_array_convertible_v =
70 is_array_convertible<From, To>::value;
72 template <
typename It>
73 using iter_reference_t =
decltype(*std::declval<It&>());
75 template <
typename It,
typename =
void>
76 struct is_compatible_iterator : std::false_type {};
77 template <
typename It>
78 struct is_compatible_iterator<
81 std::is_base_of<std::random_access_iterator_tag,
82 typename std::iterator_traits<It>::iterator_category>,
83 is_array_convertible<std::remove_reference_t<iter_reference_t<It>>,
84 T>>> : std::true_type {};
85 template <
typename It>
86 static constexpr bool is_compatible_iterator_v =
87 is_compatible_iterator<It>::value;
90 [[nodiscard]]
static constexpr U* to_address(U* p)
noexcept {
94 template <
typename It,
95 typename = std::void_t<decltype(std::declval<It&>().operator->())>>
96 [[nodiscard]]
static constexpr auto to_address(It it)
noexcept {
97 return it.operator->();
110 template <
typename Iterator,
111 std::enable_if_t<is_compatible_iterator_v<Iterator>,
bool> =
true>
114 : data_(to_address(first)), size_(count) {}
117 template <
typename Iterator,
118 std::enable_if_t<is_compatible_iterator_v<Iterator> &&
119 !std::is_convertible_v<Iterator, size_t>,
123 : data_(to_address(first)), size_(last - first) {}
128 : data_(a), size_(N) {}
131 template <
typename U,
size_t N,
132 std::enable_if_t<is_array_convertible_v<U, T>,
bool> =
true>
134 std::array<U, N>& a)
noexcept
135 : data_(a.data()), size_{N} {}
138 template <
typename U,
size_t N,
139 std::enable_if_t<is_array_convertible_v<const U, T>,
bool> =
true>
141 const std::array<U, N>& a)
noexcept
142 : data_(a.data()), size_{N} {}
145 [[nodiscard]]
constexpr T*
data()
const {
return data_; }
147 [[nodiscard]]
constexpr size_t size()
const {
return size_; }
149 [[nodiscard]]
constexpr T&
operator[](
size_t i)
const {
return data_[i]; }
152 [[nodiscard]]
constexpr bool empty()
const {
return size() == 0; }
166#if __has_include(<ranges>)
167 using iterator_concept = std::contiguous_iterator_tag;
177 return a.ptr_ == b.ptr_;
179#if V8_HAVE_SPACESHIP_OPERATOR
180 [[nodiscard]]
friend constexpr auto operator<=>(
const Iterator&,
187 return a.ptr_ != b.ptr_;
192 return a.ptr_ < b.ptr_;
196 return a.ptr_ <= b.ptr_;
200 return a.ptr_ > b.ptr_;
204 return a.ptr_ >= b.ptr_;
259 return lhs.ptr_ - rhs.ptr_;
264 [[nodiscard]]
constexpr reference operator[](
size_t offset)
const {
271 constexpr explicit Iterator(T* ptr) : ptr_(ptr) {}
296template <
class T, std::size_t N, std::size_t... I>
298 T (&a)[N], std::index_sequence<I...>) {
302template <
class T, std::size_t N, std::size_t... I>
304 T (&&a)[N], std::index_sequence<I...>) {
305 return {{std::move(a[I])...}};
309template <
class T, std::
size_t N>
310[[nodiscard]]
constexpr std::array<std::remove_cv_t<T>, N>
to_array(T (&a)[N]) {
314template <
class T, std::
size_t N>
315[[nodiscard]]
constexpr std::array<std::remove_cv_t<T>, N>
to_array(
318 std::make_index_sequence<N>{});
Definition: v8-memory-span.h:154
std::ptrdiff_t difference_type
Definition: v8-memory-span.h:156
friend constexpr bool operator>(const Iterator &a, const Iterator &b)
Definition: v8-memory-span.h:198
friend constexpr difference_type operator-(const Iterator &lhs, const Iterator &rhs)
Definition: v8-memory-span.h:257
constexpr Iterator & operator+=(difference_type rhs)
Definition: v8-memory-span.h:230
friend constexpr Iterator operator+(Iterator lhs, difference_type rhs)
Definition: v8-memory-span.h:235
constexpr Iterator & operator++()
Definition: v8-memory-span.h:208
friend constexpr bool operator>=(const Iterator &a, const Iterator &b)
Definition: v8-memory-span.h:202
friend constexpr bool operator==(const Iterator &a, const Iterator &b)
Definition: v8-memory-span.h:173
std::random_access_iterator_tag iterator_category
Definition: v8-memory-span.h:160
T value_type
Definition: v8-memory-span.h:157
constexpr Iterator()=default
friend constexpr bool operator!=(const Iterator &a, const Iterator &b)
Definition: v8-memory-span.h:185
value_type & reference
Definition: v8-memory-span.h:159
constexpr reference operator*() const
Definition: v8-memory-span.h:262
friend constexpr Iterator operator+(difference_type lhs, const Iterator &rhs)
Definition: v8-memory-span.h:241
constexpr Iterator operator--(int)
Definition: v8-memory-span.h:224
constexpr Iterator operator++(int)
Definition: v8-memory-span.h:213
value_type * pointer
Definition: v8-memory-span.h:158
friend constexpr bool operator<(const Iterator &a, const Iterator &b)
Definition: v8-memory-span.h:190
constexpr Iterator & operator-=(difference_type rhs)
Definition: v8-memory-span.h:246
friend constexpr Iterator operator-(Iterator lhs, difference_type rhs)
Definition: v8-memory-span.h:251
friend constexpr bool operator<=(const Iterator &a, const Iterator &b)
Definition: v8-memory-span.h:194
constexpr Iterator & operator--()
Definition: v8-memory-span.h:219
constexpr pointer operator->() const
Definition: v8-memory-span.h:263
Definition: v8-memory-span.h:63
constexpr MemorySpan(std::nullptr_t, size_t)
Definition: v8-memory-span.h:107
Iterator end() const
Definition: v8-memory-span.h:277
constexpr MemorySpan(std::array< U, N > &a) noexcept
Definition: v8-memory-span.h:133
constexpr MemorySpan()=default
constexpr T & operator[](size_t i) const
Definition: v8-memory-span.h:149
constexpr MemorySpan(const std::array< U, N > &a) noexcept
Definition: v8-memory-span.h:140
constexpr MemorySpan(Iterator first, size_t count)
Definition: v8-memory-span.h:112
constexpr MemorySpan(T(&a)[N]) noexcept
Definition: v8-memory-span.h:127
constexpr T * data() const
Definition: v8-memory-span.h:145
constexpr MemorySpan(Iterator first, Iterator last)
Definition: v8-memory-span.h:121
constexpr bool empty() const
Definition: v8-memory-span.h:152
Iterator begin() const
Definition: v8-memory-span.h:276
constexpr size_t size() const
Definition: v8-memory-span.h:147
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:303
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:297
Definition: libplatform.h:15
constexpr std::array< std::remove_cv_t< T >, N > to_array(T(&a)[N])
Definition: v8-memory-span.h:310
#define V8_EXPORT
Definition: v8config.h:793