ladybird/AK/BufferStream.h
Andreas Kling 85b886c2e0 Make it possible to build the Kernel on a macOS host.
It still requires an ELF compiler and linker, but at least it builds.
I need to get rid of the "Unix" namespace. This does a lot of that.
2018-12-02 23:34:50 +01:00

61 lines
1.2 KiB
C++

#pragma once
#include "ByteBuffer.h"
namespace AK {
class BufferStream {
public:
explicit BufferStream(ByteBuffer& buffer)
: m_buffer(buffer)
{
}
void operator<<(byte value)
{
m_buffer[m_offset++] = value & 0xffu;
}
void operator<<(word value)
{
m_buffer[m_offset++] = value & 0xffu;
m_buffer[m_offset++] = (byte)(value >> 8) & 0xffu;
}
void operator<<(dword value)
{
m_buffer[m_offset++] = value & 0xffu;
m_buffer[m_offset++] = (byte)(value >> 8) & 0xffu;
m_buffer[m_offset++] = (byte)(value >> 16) & 0xffu;
m_buffer[m_offset++] = (byte)(value >> 24) & 0xffu;
}
void operator<<(const char* str)
{
size_t len = strlen(str);
for (unsigned i = 0; i < len; ++i)
m_buffer[m_offset++] = str[i];
}
void operator<<(const String& value)
{
for (unsigned i = 0; i < value.length(); ++i)
m_buffer[m_offset++] = value[i];
}
void fillToEnd(byte ch)
{
while (m_offset < m_buffer.size())
m_buffer[m_offset++] = ch;
}
size_t offset() const { return m_offset; }
private:
ByteBuffer& m_buffer;
size_t m_offset { 0 };
};
}
using AK::BufferStream;