diff options
author | Andreas Kling <kling@serenityos.org> | 2020-05-12 18:05:43 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-05-12 18:49:24 +0200 |
commit | 3775983dfe840ede58bafd8ee237fe4b258f4bd7 (patch) | |
tree | a333e56fd685e25bccf91377ede73e7376f2d8ae | |
parent | cba48803014f8248b7f29747204bc757cdd5d63d (diff) | |
download | serenity-3775983dfe840ede58bafd8ee237fe4b258f4bd7.zip |
LibIPC+LibGfx: Templatize IPC encoding as well as decoding
Now most classes dictate how they are serialized and deserialized when
transmitted across LibIPC sockets. This also makes the IPC compiler
a bit simpler. :^)
-rw-r--r-- | DevTools/IPCCompiler/main.cpp | 45 | ||||
-rw-r--r-- | Libraries/LibGfx/Point.cpp | 7 | ||||
-rw-r--r-- | Libraries/LibGfx/Point.h | 1 | ||||
-rw-r--r-- | Libraries/LibGfx/Rect.cpp | 7 | ||||
-rw-r--r-- | Libraries/LibGfx/Rect.h | 1 | ||||
-rw-r--r-- | Libraries/LibGfx/Size.cpp | 7 | ||||
-rw-r--r-- | Libraries/LibGfx/Size.h | 1 | ||||
-rw-r--r-- | Libraries/LibIPC/Decoder.cpp | 9 | ||||
-rw-r--r-- | Libraries/LibIPC/Decoder.h | 21 | ||||
-rw-r--r-- | Libraries/LibIPC/Encoder.h | 29 |
10 files changed, 83 insertions, 45 deletions
diff --git a/DevTools/IPCCompiler/main.cpp b/DevTools/IPCCompiler/main.cpp index 1eacc6b177..7af89e9a5e 100644 --- a/DevTools/IPCCompiler/main.cpp +++ b/DevTools/IPCCompiler/main.cpp @@ -311,26 +311,7 @@ int main(int argc, char** argv) initial_value = "false"; out() << " " << parameter.type << " " << parameter.name << " = " << initial_value << ";"; - if (parameter.type == "Vector<Gfx::Rect>") { - out() << " u64 " << parameter.name << "_size = 0;"; - out() << " stream >> " << parameter.name << "_size;"; - out() << " for (size_t i = 0; i < " << parameter.name << "_size; ++i) {"; - out() << " Gfx::Rect rect;"; - out() << " if (!decoder.decode(rect))"; - out() << " return nullptr;"; - out() << " " << parameter.name << ".append(move(rect));"; - out() << " }"; - } else if (parameter.type == "Vector<i32>") { - out() << " u64 " << parameter.name << "_size = 0;"; - out() << " stream >> " << parameter.name << "_size;"; - out() << " for (size_t i = 0; i < " << parameter.name << "_size; ++i) {"; - out() << " i32 value;"; - out() << " stream >> value;"; - out() << " if (stream.handle_read_failure())"; - out() << " return nullptr;"; - out() << " " << parameter.name << ".append(value);"; - out() << " }"; - } else if (parameter.type.starts_with("Optional<")) { + if (parameter.type.starts_with("Optional<")) { out() << " bool has_value = false;"; out() << " stream >> has_value;"; out() << " if (has_value) {"; @@ -364,30 +345,6 @@ int main(int argc, char** argv) for (auto& parameter : parameters) { if (parameter.type == "Gfx::Color") { out() << " stream << m_" << parameter.name << ".value();"; - } else if (parameter.type == "Gfx::Size") { - out() << " stream << m_" << parameter.name << ".width();"; - out() << " stream << m_" << parameter.name << ".height();"; - } else if (parameter.type == "Gfx::Point") { - out() << " stream << m_" << parameter.name << ".x();"; - out() << " stream << m_" << parameter.name << ".y();"; - } else if (parameter.type == "Gfx::Rect") { - out() << " stream << m_" << parameter.name << ".x();"; - out() << " stream << m_" << parameter.name << ".y();"; - out() << " stream << m_" << parameter.name << ".width();"; - out() << " stream << m_" << parameter.name << ".height();"; - } else if (parameter.type == "Vector<Gfx::Rect>") { - out() << " stream << (u64)m_" << parameter.name << ".size();"; - out() << " for (auto& rect : m_" << parameter.name << ") {"; - out() << " stream << rect.x();"; - out() << " stream << rect.y();"; - out() << " stream << rect.width();"; - out() << " stream << rect.height();"; - out() << " }"; - } else if (parameter.type == "Vector<i32>") { - out() << " stream << (u64)m_" << parameter.name << ".size();"; - out() << " for (auto value : m_" << parameter.name << ") {"; - out() << " stream << value;"; - out() << " }"; } else if (parameter.type == "Gfx::ShareableBitmap") { out() << " stream << m_" << parameter.name << ".shbuf_id();"; out() << " stream << m_" << parameter.name << ".width();"; diff --git a/Libraries/LibGfx/Point.cpp b/Libraries/LibGfx/Point.cpp index 32bd115a48..10efa90f08 100644 --- a/Libraries/LibGfx/Point.cpp +++ b/Libraries/LibGfx/Point.cpp @@ -28,6 +28,7 @@ #include <AK/String.h> #include <LibGfx/Point.h> #include <LibIPC/Decoder.h> +#include <LibIPC/Encoder.h> namespace Gfx { @@ -45,6 +46,12 @@ const LogStream& operator<<(const LogStream& stream, const Point& value) namespace IPC { +bool encode(Encoder& encoder, const Gfx::Point& point) +{ + encoder << point.x() << point.y(); + return true; +} + bool decode(Decoder& decoder, Gfx::Point& point) { int x = 0; diff --git a/Libraries/LibGfx/Point.h b/Libraries/LibGfx/Point.h index daf466d6a3..7b97b51a5d 100644 --- a/Libraries/LibGfx/Point.h +++ b/Libraries/LibGfx/Point.h @@ -179,5 +179,6 @@ const LogStream& operator<<(const LogStream&, const Point&); } namespace IPC { +bool encode(Encoder&, const Gfx::Point&); bool decode(Decoder&, Gfx::Point&); } diff --git a/Libraries/LibGfx/Rect.cpp b/Libraries/LibGfx/Rect.cpp index e3ec19b37b..32d1f30c3c 100644 --- a/Libraries/LibGfx/Rect.cpp +++ b/Libraries/LibGfx/Rect.cpp @@ -29,6 +29,7 @@ #include <AK/Vector.h> #include <LibGfx/Rect.h> #include <LibIPC/Decoder.h> +#include <LibIPC/Encoder.h> namespace Gfx { @@ -146,6 +147,12 @@ const LogStream& operator<<(const LogStream& stream, const Rect& value) namespace IPC { +bool encode(Encoder& encoder, const Gfx::Rect& rect) +{ + encoder << rect.location() << rect.size(); + return true; +} + bool decode(Decoder& decoder, Gfx::Rect& rect) { Gfx::Point point; diff --git a/Libraries/LibGfx/Rect.h b/Libraries/LibGfx/Rect.h index 5a025cb282..6baedfe092 100644 --- a/Libraries/LibGfx/Rect.h +++ b/Libraries/LibGfx/Rect.h @@ -339,4 +339,5 @@ const LogStream& operator<<(const LogStream&, const Rect&); namespace IPC { bool decode(Decoder&, Gfx::Rect&); +bool encode(Encoder&, const Gfx::Rect&); } diff --git a/Libraries/LibGfx/Size.cpp b/Libraries/LibGfx/Size.cpp index ce49aa678d..a6bf384fa5 100644 --- a/Libraries/LibGfx/Size.cpp +++ b/Libraries/LibGfx/Size.cpp @@ -28,6 +28,7 @@ #include <AK/String.h> #include <LibGfx/Size.h> #include <LibIPC/Decoder.h> +#include <LibIPC/Encoder.h> namespace Gfx { @@ -45,6 +46,12 @@ const LogStream& operator<<(const LogStream& stream, const Size& value) namespace IPC { +bool encode(Encoder& encoder, const Gfx::Size& size) +{ + encoder << size.width() << size.height(); + return true; +} + bool decode(Decoder& decoder, Gfx::Size& size) { int width = 0; diff --git a/Libraries/LibGfx/Size.h b/Libraries/LibGfx/Size.h index 4ca7f0addb..c48ebac2ec 100644 --- a/Libraries/LibGfx/Size.h +++ b/Libraries/LibGfx/Size.h @@ -114,5 +114,6 @@ const LogStream& operator<<(const LogStream&, const Size&); } namespace IPC { +bool encode(Encoder&, const Gfx::Size&); bool decode(Decoder&, Gfx::Size&); } diff --git a/Libraries/LibIPC/Decoder.cpp b/Libraries/LibIPC/Decoder.cpp index 0d9ed08318..372fd8dd1c 100644 --- a/Libraries/LibIPC/Decoder.cpp +++ b/Libraries/LibIPC/Decoder.cpp @@ -136,4 +136,13 @@ bool Decoder::decode(Dictionary& dictionary) return true; } +void dongle() { + ByteBuffer buffer; + BufferStream stream(buffer); + Decoder d(stream); + Vector<String> x; + d.decode(x); +} + + } diff --git a/Libraries/LibIPC/Decoder.h b/Libraries/LibIPC/Decoder.h index dfc68f6f56..d79abb9c06 100644 --- a/Libraries/LibIPC/Decoder.h +++ b/Libraries/LibIPC/Decoder.h @@ -26,15 +26,19 @@ #pragma once +#include <AK/BufferStream.h> #include <AK/Forward.h> +#include <AK/NumericLimits.h> +#include <AK/String.h> #include <LibIPC/Forward.h> #include <LibIPC/Message.h> namespace IPC { template<typename T> -bool decode(BufferStream&, T&) +inline bool decode(Decoder&, T&) { + ASSERT_NOT_REACHED(); return false; } @@ -64,6 +68,21 @@ public: return IPC::decode(*this, value); } + template<typename T> + bool decode(Vector<T>& vector) + { + u64 size; + if (!decode(size) || size > NumericLimits<i32>::max()) + return false; + for (size_t i = 0; i < size; ++i) { + T value; + if (!decode(value)) + return false; + vector.append(move(value)); + } + return true; + } + private: BufferStream& m_stream; }; diff --git a/Libraries/LibIPC/Encoder.h b/Libraries/LibIPC/Encoder.h index 742dc6bf69..96141cc0ec 100644 --- a/Libraries/LibIPC/Encoder.h +++ b/Libraries/LibIPC/Encoder.h @@ -31,6 +31,13 @@ namespace IPC { +template<typename T> +bool encode(BufferStream&, T&) +{ + ASSERT_NOT_REACHED(); + return false; +} + class Encoder { public: explicit Encoder(MessageBuffer& buffer) @@ -53,6 +60,28 @@ public: Encoder& operator<<(const String&); Encoder& operator<<(const Dictionary&); + template<typename T> + Encoder& operator<<(const Vector<T>& vector) + { + *this << (u64)vector.size(); + for (auto& value : vector) + *this << value; + return *this; + } + + template<typename T> + Encoder& operator<<(const T& value) + { + encode(value); + return *this; + } + + template<typename T> + void encode(const T& value) + { + IPC::encode(*this, value); + } + private: MessageBuffer& m_buffer; }; |