summaryrefslogtreecommitdiff
path: root/Libraries/LibIPC
diff options
context:
space:
mode:
Diffstat (limited to 'Libraries/LibIPC')
-rw-r--r--Libraries/LibIPC/IEncoder.h95
-rw-r--r--Libraries/LibIPC/IMessage.h5
2 files changed, 98 insertions, 2 deletions
diff --git a/Libraries/LibIPC/IEncoder.h b/Libraries/LibIPC/IEncoder.h
new file mode 100644
index 0000000000..11dc713a19
--- /dev/null
+++ b/Libraries/LibIPC/IEncoder.h
@@ -0,0 +1,95 @@
+#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;
+};
diff --git a/Libraries/LibIPC/IMessage.h b/Libraries/LibIPC/IMessage.h
index a6e37bdd90..96ff01125d 100644
--- a/Libraries/LibIPC/IMessage.h
+++ b/Libraries/LibIPC/IMessage.h
@@ -1,7 +1,8 @@
#pragma once
#include <AK/String.h>
-#include <AK/ByteBuffer.h>
+
+typedef Vector<u8, 1024> IMessageBuffer;
class IMessage {
public:
@@ -10,7 +11,7 @@ public:
virtual int endpoint_magic() const = 0;
virtual int message_id() const = 0;
virtual String message_name() const = 0;
- virtual ByteBuffer encode() const = 0;
+ virtual IMessageBuffer encode() const = 0;
protected:
IMessage();