diff options
author | Andreas Kling <awesomekling@gmail.com> | 2018-10-10 11:53:07 +0200 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2018-10-10 11:53:07 +0200 |
commit | 5a300551574451fbf509685d11095bda4fcb20be (patch) | |
tree | 7bd68b5b0bc9daab6899be52dc694b7162dc6b89 /AK/ByteBuffer.h | |
download | serenity-5a300551574451fbf509685d11095bda4fcb20be.zip |
Import all this stuff into a single repo called Serenity.
Diffstat (limited to 'AK/ByteBuffer.h')
-rw-r--r-- | AK/ByteBuffer.h | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/AK/ByteBuffer.h b/AK/ByteBuffer.h new file mode 100644 index 0000000000..ee84e2091f --- /dev/null +++ b/AK/ByteBuffer.h @@ -0,0 +1,85 @@ +#pragma once + +#include "Buffer.h" +#include "Types.h" +#include <cstdlib> +#include <cstring> +#include <cstdio> + +namespace AK { + +class ByteBuffer { +public: + ByteBuffer() { } + ByteBuffer(std::nullptr_t) { } + ByteBuffer(const ByteBuffer& other) + : m_impl(other.m_impl.copyRef()) + { + } + ByteBuffer(ByteBuffer&& other) + : m_impl(std::move(other.m_impl)) + { + } + ByteBuffer& operator=(ByteBuffer&& other) + { + if (this != &other) + m_impl = std::move(other.m_impl); + return *this; + } + + static ByteBuffer createUninitialized(size_t size) { return ByteBuffer(Buffer<byte>::createUninitialized(size)); } + static ByteBuffer copy(const byte* data, size_t size) { return ByteBuffer(Buffer<byte>::copy(data, size)); } + static ByteBuffer wrap(byte* data, size_t size) { return ByteBuffer(Buffer<byte>::wrap(data, size)); } + static ByteBuffer adopt(byte* data, size_t size) { return ByteBuffer(Buffer<byte>::adopt(data, size)); } + + ~ByteBuffer() { clear(); } + void clear() { m_impl = nullptr; } + + operator bool() const { return !isNull(); } + bool operator!() const { return isNull(); } + bool isNull() const { return m_impl == nullptr; } + + byte& operator[](size_t i) { ASSERT(m_impl); return (*m_impl)[i]; } + byte operator[](size_t i) const { ASSERT(m_impl); return (*m_impl)[i]; } + bool isEmpty() const { return !m_impl || m_impl->isEmpty(); } + size_t size() const { return m_impl ? m_impl->size() : 0; } + + byte* pointer() { return m_impl ? m_impl->pointer() : nullptr; } + const byte* pointer() const { return m_impl ? m_impl->pointer() : nullptr; } + + byte* offsetPointer(size_t offset) { return m_impl ? m_impl->offsetPointer(offset) : nullptr; } + const byte* offsetPointer(size_t offset) const { return m_impl ? m_impl->offsetPointer(offset) : nullptr; } + + const void* endPointer() const { return m_impl ? m_impl->endPointer() : nullptr; } + + // NOTE: trim() does not reallocate. + void trim(size_t size) + { + if (m_impl) + m_impl->trim(size); + } + + ByteBuffer slice(size_t offset, size_t size) const + { + if (isNull()) + return { }; + if (offset >= this->size()) + return { }; + if (offset + size >= this->size()) + size = this->size() - offset; + return copy(offsetPointer(offset), size); + } + +private: + explicit ByteBuffer(RetainPtr<Buffer<byte>>&& impl) + : m_impl(std::move(impl)) + { + } + + RetainPtr<Buffer<byte>> m_impl; +}; + +} + +using AK::ByteBuffer; + |