AUI Framework  master
Cross-platform module-based framework for developing C++20 desktop applications
AByteBufferView.h
1/*
2 * AUI Framework - Declarative UI toolkit for modern C++20
3 * Copyright (C) 2020-2024 Alex2772 and Contributors
4 *
5 * SPDX-License-Identifier: MPL-2.0
6 *
7 * This Source Code Form is subject to the terms of the Mozilla Public
8 * License, v. 2.0. If a copy of the MPL was not distributed with this
9 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 */
11
12#pragma once
13
14#include <AUI/Traits/serializable.h>
15
24class API_AUI_CORE AByteBufferView {
25private:
26 const char* mBuffer;
27 size_t mSize;
28
29public:
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()) {}
34
35 static AByteBufferView fromRange(const char* begin, const char* end) noexcept {
36 return AByteBufferView(begin, std::distance(begin, end));
37 }
38
45 template <typename T>
46 const T& at(size_t byteIndex) const noexcept
47 {
48 return *reinterpret_cast<const T*>(mBuffer + byteIndex);
49 }
50
51 [[nodiscard]]
52 bool empty() const noexcept {
53 return mSize == 0;
54 }
55
56 [[nodiscard]]
57 AByteBufferView slice(std::size_t offset /* to end */) const noexcept {
58 return slice(offset, size() - offset);
59 }
60
61 [[nodiscard]]
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 };
65 }
66
67 [[nodiscard]]
68 const char* data() const noexcept {
69 return mBuffer;
70 }
71
72 [[nodiscard]]
73 size_t size() const noexcept {
74 return mSize;
75 }
76
77 [[nodiscard]]
78 auto begin() const noexcept {
79 return data();
80 }
81 [[nodiscard]]
82 auto end() const noexcept {
83 return data() + size();
84 }
85
86 [[nodiscard]]
87 AString toHexString() const;
88
89 [[nodiscard]]
90 AString toBase64String() const;
91
92 template<typename T>
93 [[nodiscard]]
94 const T& as() const {
95 if (mSize != sizeof(T)) {
96 throw AException("as<T>(): invalid size");
97 }
98 return *reinterpret_cast<const T*>(mBuffer);
99 }
100
101 template<typename T>
102 [[nodiscard]]
103 static AByteBufferView fromRaw(const T& data) noexcept {
104 return { reinterpret_cast<const char*>(&data), sizeof(data) };
105 }
106
107
108 _unique<IInputStream> toStream() const;
109};
110
111inline std::ostream& operator<<(std::ostream& lhs, const AByteBufferView& rhs) {
112 lhs << "[";
113 for (const auto b : rhs) {
114 char buf[8];
115 lhs.write(buf, std::distance(std::begin(buf), fmt::format_to(buf, " {:02x}", b)));
116 }
117 lhs << " ]";
118
119 return lhs;
120}
121
122template<>
124 static void write(IOutputStream& os, AByteBufferView view) {
125 os.write(view.data(), view.size());
126 }
127};
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
Abstract AUI exception.
Definition: AException.h:29
Represents a Unicode character string.
Definition: AString.h:37
Definition: IOutputStream.h:20
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
Definition: Empty.h:19