2018-10-10 09:53:07 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "Buffer.h"
|
|
|
|
#include "Types.h"
|
2018-12-03 23:27:16 +00:00
|
|
|
#include "StdLibExtras.h"
|
2018-10-10 09:53:07 +00:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
class ByteBuffer {
|
|
|
|
public:
|
|
|
|
ByteBuffer() { }
|
|
|
|
ByteBuffer(std::nullptr_t) { }
|
|
|
|
ByteBuffer(const ByteBuffer& other)
|
|
|
|
: m_impl(other.m_impl.copyRef())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
ByteBuffer(ByteBuffer&& other)
|
2018-10-16 10:10:01 +00:00
|
|
|
: m_impl(move(other.m_impl))
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
ByteBuffer& operator=(ByteBuffer&& other)
|
|
|
|
{
|
|
|
|
if (this != &other)
|
2018-10-16 10:10:01 +00:00
|
|
|
m_impl = move(other.m_impl);
|
2018-10-10 09:53:07 +00:00
|
|
|
return *this;
|
|
|
|
}
|
2018-10-26 22:14:24 +00:00
|
|
|
ByteBuffer& operator=(const ByteBuffer& other)
|
|
|
|
{
|
|
|
|
m_impl = other.m_impl.copyRef();
|
|
|
|
return *this;
|
|
|
|
}
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2018-12-21 01:10:45 +00:00
|
|
|
static ByteBuffer create_uninitialized(size_t size) { return ByteBuffer(Buffer<byte>::create_uninitialized(size)); }
|
2018-10-10 09:53:07 +00:00
|
|
|
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; }
|
|
|
|
|
2018-12-21 01:10:45 +00:00
|
|
|
operator bool() const { return !is_null(); }
|
|
|
|
bool operator!() const { return is_null(); }
|
|
|
|
bool is_null() const { return m_impl == nullptr; }
|
2018-10-10 09:53:07 +00:00
|
|
|
|
|
|
|
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]; }
|
2018-12-21 01:10:45 +00:00
|
|
|
bool is_empty() const { return !m_impl || m_impl->is_empty(); }
|
2018-10-10 09:53:07 +00:00
|
|
|
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; }
|
|
|
|
|
2018-12-21 01:10:45 +00:00
|
|
|
byte* offset_pointer(size_t offset) { return m_impl ? m_impl->offset_pointer(offset) : nullptr; }
|
|
|
|
const byte* offset_pointer(size_t offset) const { return m_impl ? m_impl->offset_pointer(offset) : nullptr; }
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2018-12-21 01:10:45 +00:00
|
|
|
const void* end_pointer() const { return m_impl ? m_impl->end_pointer() : nullptr; }
|
2018-10-10 09:53:07 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
{
|
2018-12-21 01:10:45 +00:00
|
|
|
if (is_null())
|
2018-10-10 09:53:07 +00:00
|
|
|
return { };
|
|
|
|
if (offset >= this->size())
|
|
|
|
return { };
|
|
|
|
if (offset + size >= this->size())
|
|
|
|
size = this->size() - offset;
|
2018-12-21 01:10:45 +00:00
|
|
|
return copy(offset_pointer(offset), size);
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
explicit ByteBuffer(RetainPtr<Buffer<byte>>&& impl)
|
2018-10-16 10:10:01 +00:00
|
|
|
: m_impl(move(impl))
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
RetainPtr<Buffer<byte>> m_impl;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
using AK::ByteBuffer;
|
|
|
|
|