ladybird/AK/BufferStream.h
Andreas Kling 27f699ef0c AK: Rename the common integer typedefs to make it obvious what they are.
These types can be picked up by including <AK/Types.h>:

* u8, u16, u32, u64 (unsigned)
* i8, i16, i32, i64 (signed)
2019-07-03 21:20:13 +02:00

77 lines
1.5 KiB
C++

#pragma once
#include <AK/AKString.h>
#include <AK/ByteBuffer.h>
namespace AK {
class BufferStream {
public:
explicit BufferStream(ByteBuffer& buffer)
: m_buffer(buffer)
{
}
void operator<<(u8 value)
{
m_buffer[m_offset++] = value & 0xffu;
}
void operator<<(char value)
{
m_buffer[m_offset++] = (u8)value;
}
void operator<<(u16 value)
{
m_buffer[m_offset++] = value & 0xffu;
m_buffer[m_offset++] = (u8)(value >> 8) & 0xffu;
}
void operator<<(u32 value)
{
m_buffer[m_offset++] = value & 0xffu;
m_buffer[m_offset++] = (u8)(value >> 8) & 0xffu;
m_buffer[m_offset++] = (u8)(value >> 16) & 0xffu;
m_buffer[m_offset++] = (u8)(value >> 24) & 0xffu;
}
void operator<<(const StringView& value)
{
for (ssize_t i = 0; i < value.length(); ++i)
m_buffer[m_offset++] = value[i];
}
void operator<<(const ByteBuffer& value)
{
for (ssize_t i = 0; i < value.size(); ++i)
m_buffer[m_offset++] = value[i];
}
bool at_end() const
{
return m_offset == m_buffer.size();
}
void fill_to_end(u8 ch)
{
while (!at_end())
m_buffer[m_offset++] = ch;
}
ssize_t offset() const { return m_offset; }
void snip()
{
m_buffer.trim(m_offset);
}
private:
ByteBuffer& m_buffer;
ssize_t m_offset { 0 };
};
}
using AK::BufferStream;