mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-10 13:00:29 +03:00
ef658594e4
Instead of using ByteBuffer (which always malloc() their storage) for IPC message encoding, we now use a Vector<u8, 1024>, which means that messages smaller than 1 KB avoid heap allocation entirely.
96 lines
2.2 KiB
C++
96 lines
2.2 KiB
C++
#pragma once
|
|
|
|
#include <LibIPC/IMessage.h>
|
|
|
|
class IEncoder {
|
|
public:
|
|
explicit IEncoder(IMessageBuffer& buffer)
|
|
: m_buffer(buffer)
|
|
{
|
|
}
|
|
|
|
IEncoder& operator<<(bool value)
|
|
{
|
|
return *this << (u8)value;
|
|
}
|
|
|
|
IEncoder& operator<<(u8 value)
|
|
{
|
|
m_buffer.append(value);
|
|
return *this;
|
|
}
|
|
|
|
IEncoder& operator<<(u16 value)
|
|
{
|
|
m_buffer.ensure_capacity(m_buffer.size() + 2);
|
|
m_buffer.unchecked_append((u8)value);
|
|
m_buffer.unchecked_append((u8)(value >> 8));
|
|
return *this;
|
|
}
|
|
|
|
IEncoder& operator<<(u32 value)
|
|
{
|
|
m_buffer.ensure_capacity(m_buffer.size() + 4);
|
|
m_buffer.unchecked_append((u8)value);
|
|
m_buffer.unchecked_append((u8)(value >> 8));
|
|
m_buffer.unchecked_append((u8)(value >> 16));
|
|
m_buffer.unchecked_append((u8)(value >> 24));
|
|
return *this;
|
|
}
|
|
|
|
IEncoder& operator<<(i8 value)
|
|
{
|
|
m_buffer.append((u8)value);
|
|
return *this;
|
|
}
|
|
|
|
IEncoder& operator<<(i16 value)
|
|
{
|
|
m_buffer.ensure_capacity(m_buffer.size() + 2);
|
|
m_buffer.unchecked_append((u8)value);
|
|
m_buffer.unchecked_append((u8)(value >> 8));
|
|
return *this;
|
|
}
|
|
|
|
IEncoder& operator<<(i32 value)
|
|
{
|
|
m_buffer.ensure_capacity(m_buffer.size() + 4);
|
|
m_buffer.unchecked_append((u8)value);
|
|
m_buffer.unchecked_append((u8)(value >> 8));
|
|
m_buffer.unchecked_append((u8)(value >> 16));
|
|
m_buffer.unchecked_append((u8)(value >> 24));
|
|
return *this;
|
|
}
|
|
|
|
#ifdef __serenity__
|
|
IEncoder& operator<<(size_t value)
|
|
{
|
|
return *this << (u32)value;
|
|
}
|
|
#endif
|
|
|
|
IEncoder& operator<<(float value)
|
|
{
|
|
union bits {
|
|
float as_float;
|
|
u32 as_u32;
|
|
} u;
|
|
u.as_float = value;
|
|
return *this << u.as_u32;
|
|
}
|
|
|
|
IEncoder& operator<<(const char* value)
|
|
{
|
|
return *this << StringView(value);
|
|
}
|
|
|
|
IEncoder& operator<<(const StringView& value)
|
|
{
|
|
m_buffer.append((const u8*)value.characters_without_null_termination(), value.length());
|
|
return *this;
|
|
}
|
|
|
|
private:
|
|
IMessageBuffer& m_buffer;
|
|
};
|