Header buffer.h

namespace clu

Typedefs

using mutable_buffer = basic_buffer<std::byte>

Mutable buffer using std::byte as the element type.

using const_buffer = basic_buffer<const std::byte>

Constant buffer using std::byte as the element type.

Functions

template<alias_safe T>
constexpr void memmove(T *dst, const T *src, const size_t size) noexcept

Copies data between addresses, safe even when the destination overlaps the source.

Parameters
  • dst – Destination address.

  • src – Source address.

  • size – The size (counting T’s, not bytes) to copy.

template<buffer_safe T>
mutable_buffer trivial_buffer(T &value) noexcept
template<buffer_safe T>
const_buffer trivial_buffer(const T &value) noexcept
template<typename T>
void trivial_buffer(const T&&) = delete

Variables

template<typename T> concept alias_safe   = same_as_any_of<T, unsigned char, char, std::byte>

Specifies that a type is safe to alias.

An alias-safe type could be used as buffer elements.

template<typename T> concept buffer_safe  =trivially_copyable<T> ||(std::is_array_v<T> && trivially_copyable<std::remove_all_extents_t<T>>)

Specifies that a type is safe to be seen through by a buffer.

Buffer-safe types can be read from/written into buffers.

template<typename T> concept trivial_range  =std::ranges::contiguous_range<T> &&std::ranges::sized_range<T> &&trivially_copyable<std::ranges::range_value_t<T>>
template<typename T> concept mutable_trivial_range   = trivial_range<T> && (!std::is_const_v<std::ranges::range_value_t<T>>)
template<typename T>
class basic_buffer
#include <buffer.h>

Non-owning buffer view, suitable for raw byte manipulations.

Template Parameters

T – The buffer element type, must be alias-safe.

Public Types

using value_type = T
using mutable_type = basic_buffer<std::remove_const_t<T>>
using const_type = basic_buffer<std::add_const_t<T>>

Public Functions

constexpr basic_buffer() noexcept = default

Creates an empty buffer.

constexpr basic_buffer(const basic_buffer&) noexcept = default
constexpr basic_buffer(basic_buffer&&) noexcept = default
constexpr basic_buffer &operator=(const basic_buffer&) noexcept = default
constexpr basic_buffer &operator=(basic_buffer&&) noexcept = default
inline constexpr basic_buffer(T *ptr, const size_t size) noexcept

Constructs a buffer from a start pointer and a size.

Parameters
  • ptr – Start of the buffer region.

  • size – Size of the buffer.

template<typename R> inline  requires (std::is_const_v< T > &&trivial_range< R >)||(!std
template<typename = int> inline requires std::is_const_v< T > constexpr explicit (false) basic_buffer(const mutable_type &buffer) noexcept
inline constexpr T *data() const noexcept

Gets a pointer to the buffer data.

inline constexpr size_t size() const noexcept

Gets the size of the buffer.

inline constexpr bool empty() const noexcept

Checks if the buffer is empty.

inline constexpr void remove_prefix(const size_t size) noexcept
inline constexpr void remove_suffix(const size_t size) noexcept
inline constexpr T &operator[](const size_t index) const noexcept
inline constexpr basic_buffer first(const size_t size) const noexcept
inline constexpr basic_buffer last(const size_t size) const noexcept
inline constexpr basic_buffer &operator+=(const size_t size) noexcept
inline constexpr std::span<T> as_span() const noexcept

Converts the buffer into a std::span of the element type.

template<trivially_copyable U>
inline constexpr U as() const noexcept
template<trivially_copyable U>
inline constexpr U consume_as() noexcept
inline constexpr size_t copy_to(const mutable_type dest) const noexcept
inline constexpr size_t copy_to_consume(const mutable_type dest) noexcept

Private Members

T *ptr_ = nullptr
size_t size_ = 0

Private Static Functions

template<typename U>
static inline constexpr T *conditional_reinterpret_cast(U *ptr) noexcept

Friends

inline friend constexpr friend basic_buffer operator+ (basic_buffer buffer, const size_t size) noexcept