24 using pointer =
void *;
25 using const_pointer =
void const *;
27 constexpr static std::size_t extent = std::dynamic_extent;
35 constexpr void_span(
void *pointer,
size_t size) noexcept : _pointer(pointer), _size(size)
37 hi_assert(pointer !=
nullptr or size == 0);
40 template<
typename T, std::
size_t N>
45 template<
typename T, std::
size_t N>
50 [[nodiscard]]
constexpr pointer data()
const noexcept
55 [[nodiscard]]
constexpr size_type size()
const noexcept
60 [[nodiscard]]
constexpr size_type size_bytes()
const noexcept
65 [[nodiscard]]
constexpr bool empty()
const noexcept
70 template<std::
size_t N>
71 [[nodiscard]]
constexpr void_span first()
const noexcept
83 template<std::
size_t N>
84 [[nodiscard]]
constexpr void_span last()
const noexcept
96 template<std::
size_t Offset, std::
size_t Count = std::dynamic_extent>
97 [[nodiscard]]
constexpr void_span subspan()
const noexcept
99 if constexpr (Count == std::dynamic_extent) {
100 hi_axiom(Offset <= _size);
103 hi_axiom((Offset + Count) <= _size);
110 hi_axiom(offset <= _size);
111 if (count == std::dynamic_extent) {
112 count = _size - offset;
115 hi_axiom(offset + count <= _size);
119 template<
typename T,
size_t E = std::dynamic_extent>
120 [[nodiscard]]
constexpr friend std::span<T, E> as_span(
void_span const& rhs)
noexcept
122 if constexpr (E == std::dynamic_extent) {
123 hi_axiom(std::bit_cast<std::uintptr_t>(rhs._pointer) % std::alignment_of_v<T> == 0);
124 hi_axiom((rhs._size /
sizeof(T)) *
sizeof(T) == rhs._size);
125 return {
static_cast<T *
>(rhs._pointer), rhs._size /
sizeof(T)};
128 hi_axiom(std::bit_cast<std::uintptr_t>(rhs._pointer) % std::alignment_of_v<T> == 0);
129 hi_axiom(E *
sizeof(T) <= rhs._size);
130 return {
static_cast<T *
>(rhs._pointer), E};
134 template<
typename CharT,
typename Traits = std::
char_traits<CharT>>
135 [[nodiscard]]
constexpr friend std::basic_string_view<CharT, Traits> as_basic_string_view(
void_span const& rhs)
noexcept
137 hi_axiom(std::bit_cast<std::uintptr_t>(rhs._pointer) % std::alignment_of_v<CharT> == 0);
138 hi_axiom((rhs._size /
sizeof(CharT)) *
sizeof(CharT) == rhs._size);
139 return {
static_cast<CharT
const *
>(rhs._pointer), rhs._size /
sizeof(CharT)};
142 [[nodiscard]]
constexpr friend std::string_view as_string_view(
void_span const& rhs)
noexcept
144 return as_basic_string_view<char>(rhs);
147 [[nodiscard]]
constexpr friend bstring_view as_bstring_view(
void_span const& rhs)
noexcept
149 return as_basic_string_view<std::byte, byte_char_traits>(rhs);
153 pointer _pointer =
nullptr;
161 using pointer =
void const *;
162 using const_pointer =
void const *;
164 constexpr static std::size_t extent = std::dynamic_extent;
172 constexpr const_void_span(
void const *pointer,
size_t size) noexcept : _pointer(pointer), _size(size)
174 hi_assert(pointer !=
nullptr or size == 0);
179 template<
typename T, std::
size_t N>
184 template<
typename T, std::
size_t N>
189 [[nodiscard]]
constexpr pointer data()
const noexcept
194 [[nodiscard]]
constexpr size_type size()
const noexcept
199 [[nodiscard]]
constexpr size_type size_bytes()
const noexcept
204 [[nodiscard]]
constexpr bool empty()
const noexcept
209 template<std::
size_t N>
212 hi_axiom(N <= _size);
213 return {_pointer, N};
218 hi_axiom(n <= _size);
219 return {_pointer, n};
222 template<std::
size_t N>
225 hi_axiom(N <= _size);
231 hi_axiom(n <= _size);
235 template<std::
size_t Offset, std::
size_t Count = std::dynamic_extent>
238 if constexpr (Count == std::dynamic_extent) {
239 hi_axiom(Offset <= _size);
242 hi_axiom((Offset + Count) <= _size);
249 hi_axiom(offset <= _size);
250 if (count == std::dynamic_extent) {
251 count = _size - offset;
254 hi_axiom(offset + count <= _size);
258 template<
typename T,
size_t E = std::dynamic_extent>
259 [[nodiscard]]
constexpr friend std::span<T, E> as_span(
const_void_span const& rhs)
noexcept requires(std::is_const_v<T>)
261 if constexpr (E == std::dynamic_extent) {
262 hi_axiom(std::bit_cast<std::uintptr_t>(rhs._pointer) % std::alignment_of_v<T> == 0);
263 hi_axiom((rhs._size /
sizeof(T)) *
sizeof(T) == rhs._size);
264 return {
static_cast<T *
>(rhs._pointer), rhs._size /
sizeof(T)};
267 hi_axiom(std::bit_cast<std::uintptr_t>(rhs._pointer) % std::alignment_of_v<T> == 0);
268 hi_axiom(E *
sizeof(T) <= rhs._size);
269 return {
static_cast<T *
>(rhs._pointer), E};
273 template<
typename CharT,
typename Traits = std::
char_traits<CharT>>
274 [[nodiscard]]
constexpr friend std::basic_string_view<CharT, Traits> as_basic_string_view(
const_void_span const& rhs)
noexcept
276 hi_axiom(std::bit_cast<std::uintptr_t>(rhs._pointer) % std::alignment_of_v<CharT> == 0);
277 hi_axiom((rhs._size /
sizeof(CharT)) *
sizeof(CharT) == rhs._size);
278 return {
static_cast<CharT
const *
>(rhs._pointer), rhs._size /
sizeof(CharT)};
281 [[nodiscard]]
constexpr friend std::string_view as_string_view(
const_void_span const& rhs)
noexcept
283 return as_basic_string_view<char>(rhs);
286 [[nodiscard]]
constexpr friend bstring_view as_bstring_view(
const_void_span const& rhs)
noexcept
288 return as_basic_string_view<std::byte, byte_char_traits>(rhs);
292 pointer _pointer =
nullptr;
void * advance_bytes(void *ptr, std::ptrdiff_t distance) noexcept
Advance a pointer by a number of bytes.
Definition memory.hpp:224