14#include "AUI/Util/Assert.h"
23#include <AUI/Traits/serializable.h>
24#include "AByteBufferView.h"
33 char* mBuffer =
nullptr;
38 using iterator =
char*;
41 AByteBuffer(
const char* buffer,
size_t size);
42 explicit AByteBuffer(
size_t initialCapacity);
43 AByteBuffer(
const unsigned char* buffer,
size_t size);
46 memcpy(mBuffer, other.data(), other.size());
50 AByteBuffer(
const AByteBuffer& other): AByteBuffer(
AByteBufferView(other)) {}
51 AByteBuffer(AByteBuffer&& other)
noexcept;
55 void write(
const char* src,
size_t size)
override;
76 return { mBuffer, mSize };
82 mSize = mCapacity = 0;
103 if (availableToWrite <
size) {
130 template <
typename T>
131 T&
at(
size_t byteIndex)
133 return *
reinterpret_cast<T*
>(mBuffer + byteIndex);
143 template <
typename T>
144 const T&
at(
size_t byteIndex)
const
146 return *
reinterpret_cast<T*
>(mBuffer + byteIndex);
161 AUI_ASSERTX(s <= mCapacity,
"size cannot be greater than reserved buffer size; did you mean AByteBuffer::resize?");
177 AUI_ASSERTX(mSize <= mCapacity,
"size cannot be greater than reserved buffer size; did you mean AByteBuffer::resize?");
198 if (mCapacity != s) {
203 mBuffer =
new char[s];
246 if (&other ==
this) {
251 mBuffer = other.mBuffer;
252 mCapacity = other.mCapacity;
255 other.mBuffer =
nullptr;
261 AByteBuffer& operator=(
const AByteBuffer& other) {
262 if (&other ==
this) {
266 if (mCapacity < other.
size()) {
267 reallocate(other.
size());
269 std::memcpy(mBuffer, other.
data(), other.
size());
275 bool operator==(
const AByteBuffer& r)
const;
276 bool operator!=(
const AByteBuffer& r)
const;
284 return mBuffer + mSize;
288 return mBuffer + mCapacity;
290 const char* begin()
const
294 const char* end()
const
296 return mBuffer + mSize;
302 return AByteBufferView(*this).template as<T>();
306 AString toHexString()
const {
307 return AByteBufferView(*this).toHexString();
311 AString toBase64String()
const {
312 return AByteBufferView(*this).toBase64String();
315 iterator erase(iterator begin, iterator end)
noexcept {
318 std::memmove(begin, end, std::distance(end, AByteBuffer::end()));
319 setSize(size() - std::distance(begin, end));
324 bool ownsIterator(iterator i)
const noexcept {
325 return i >= begin() && i <= end();
328 static AByteBuffer fromStream(aui::no_escape<IInputStream> is);
329 static AByteBuffer fromStream(aui::no_escape<IInputStream> is,
size_t sizeRestriction);
331 static AByteBuffer fromString(
const AString&
string);
332 static AByteBuffer fromHexString(
const AString&
string);
333 static AByteBuffer fromBase64String(
const AString& encodedString);
336API_AUI_CORE std::ostream& operator<<(std::ostream& o,
AByteBufferView buffer);
341 os.
write(value.data(), value.size());
Acts like std::string_view but for AByteBuffer.
Definition AByteBufferView.h:24
std::vector-like growing array for byte storage.
Definition AByteBuffer.h:31
void reallocate(size_t s)
Definition AByteBuffer.h:197
void increaseSize(size_t s)
Definition AByteBuffer.h:175
size_t getAvailableToWrite() const
Definition AByteBuffer.h:111
void increaseInternalBuffer(size_t size)
Increases internal buffer.
Definition AByteBuffer.h:93
T & at(size_t byteIndex)
Gets value of specified type by byte index relative to the beginning of internal buffer.
Definition AByteBuffer.h:131
size_t size() const noexcept
Definition AByteBuffer.h:227
size_t capacity() const noexcept
Definition AByteBuffer.h:234
const T & at(size_t byteIndex) const
Gets value of specified type by byte index relative to the beginning of internal buffer.
Definition AByteBuffer.h:144
void grow(size_t size)
If getReserved() - getSize() is less than size increases internal buffer size enough to store size by...
Definition AByteBuffer.h:101
void reserve(size_t size)
Resizes internal buffer.
Definition AByteBuffer.cpp:45
void resize(size_t s)
Definition AByteBuffer.h:185
void setSize(size_t s)
Definition AByteBuffer.h:160
size_t getReserved() const noexcept
Definition AByteBuffer.h:241
size_t getSize() const noexcept
Definition AByteBuffer.h:213
bool empty() const noexcept
Definition AByteBuffer.h:220
char * data() const
Definition AByteBuffer.h:119
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_ASSERT(condition)
Asserts that the passed condition evaluates to true.
Definition Assert.h:55
#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