14#include <AUI/Traits/serializable.h>
24class API_AUI_CORE AByteBufferView {
30 AByteBufferView()
noexcept: mBuffer(
nullptr), mSize(0) {}
31 AByteBufferView(
const char* buffer,
size_t size)
noexcept: mBuffer(buffer), mSize(size) {}
32 explicit AByteBufferView(
const std::string&
string)
noexcept: mBuffer(
string.data()), mSize(
string.size()) {}
33 explicit AByteBufferView(std::string_view
string)
noexcept: mBuffer(
string.data()), mSize(
string.size()) {}
35 static AByteBufferView fromRange(
const char* begin,
const char* end)
noexcept {
36 return AByteBufferView(begin, std::distance(begin, end));
46 const T&
at(
size_t byteIndex)
const noexcept
48 return *
reinterpret_cast<const T*
>(mBuffer + byteIndex);
52 bool empty() const noexcept {
57 AByteBufferView slice(std::size_t offset )
const noexcept {
58 return slice(offset, size() - offset);
62 AByteBufferView slice(std::size_t offset, std::size_t size)
const noexcept {
63 AUI_ASSERTX(offset + size <= mSize,
"out of bounds");
64 return { mBuffer + offset, size };
68 const char* data() const noexcept {
73 size_t size() const noexcept {
78 auto begin() const noexcept {
82 auto end() const noexcept {
83 return data() + size();
87 AString toHexString()
const;
90 AString toBase64String()
const;
95 if (mSize !=
sizeof(T)) {
96 throw AException(
"as<T>(): invalid size");
98 return *
reinterpret_cast<const T*
>(mBuffer);
103 static AByteBufferView fromRaw(
const T& data)
noexcept {
104 return {
reinterpret_cast<const char*
>(&data),
sizeof(data) };
108 _unique<IInputStream> toStream()
const;
111inline std::ostream& operator<<(std::ostream& lhs,
const AByteBufferView& rhs) {
113 for (
const auto b : rhs) {
115 lhs.write(buf, std::distance(std::begin(buf), fmt::format_to(buf,
" {:02x}", b)));
125 os.
write(view.data(), view.size());
Acts like std::string_view but for AByteBuffer.
Definition AByteBufferView.h:24
const T & at(size_t byteIndex) const noexcept
Gets value of specified type by byte index relative to the beginning of internal buffer.
Definition AByteBufferView.h:46
Definition IOutputStream.h:18
virtual void write(const char *src, size_t size)=0
Writes exact size bytes to stream. Blocking (waiting for write all data) is allowed.
#define AUI_ASSERTX(condition, what)
Asserts that the passed condition evaluates to true. Adds extra message string.
Definition Assert.h:74
Definition serializable.h:26