mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 08:37:46 +00:00
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. :^)
This commit is contained in:
parent
cba4880301
commit
3775983dfe
10 changed files with 83 additions and 45 deletions
|
@ -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;
|
||||
|
|
|
@ -179,5 +179,6 @@ const LogStream& operator<<(const LogStream&, const Point&);
|
|||
}
|
||||
|
||||
namespace IPC {
|
||||
bool encode(Encoder&, const Gfx::Point&);
|
||||
bool decode(Decoder&, Gfx::Point&);
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -339,4 +339,5 @@ const LogStream& operator<<(const LogStream&, const Rect&);
|
|||
|
||||
namespace IPC {
|
||||
bool decode(Decoder&, Gfx::Rect&);
|
||||
bool encode(Encoder&, const Gfx::Rect&);
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -114,5 +114,6 @@ const LogStream& operator<<(const LogStream&, const Size&);
|
|||
}
|
||||
|
||||
namespace IPC {
|
||||
bool encode(Encoder&, const Gfx::Size&);
|
||||
bool decode(Decoder&, Gfx::Size&);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue