mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 06:37:43 +00:00
LibIPC: Support sending Variants over IPC
The format is quite simply the type index followed by the type in its own native encoding; just implementing the receive side with static typing is a bit convoluted. The only limitation of this implementation is that the variant type has to contain an Empty somewhere as it is not default constructible otherwise. Co-authored-by: Ali Mohammad Pur <mpfard@serenityos.org>
This commit is contained in:
parent
5b4818df22
commit
a06b277471
4 changed files with 56 additions and 0 deletions
|
@ -208,4 +208,10 @@ ErrorOr<void> decode(Decoder& decoder, Core::ProxyData& data)
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// No-op.
|
||||||
|
ErrorOr<void> Decoder::decode(AK::Empty&)
|
||||||
|
{
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,6 +12,8 @@
|
||||||
#include <AK/NumericLimits.h>
|
#include <AK/NumericLimits.h>
|
||||||
#include <AK/StdLibExtras.h>
|
#include <AK/StdLibExtras.h>
|
||||||
#include <AK/Try.h>
|
#include <AK/Try.h>
|
||||||
|
#include <AK/TypeList.h>
|
||||||
|
#include <AK/Variant.h>
|
||||||
#include <LibCore/SharedCircularQueue.h>
|
#include <LibCore/SharedCircularQueue.h>
|
||||||
#include <LibCore/Stream.h>
|
#include <LibCore/Stream.h>
|
||||||
#include <LibIPC/File.h>
|
#include <LibIPC/File.h>
|
||||||
|
@ -53,6 +55,7 @@ public:
|
||||||
ErrorOr<void> decode(URL&);
|
ErrorOr<void> decode(URL&);
|
||||||
ErrorOr<void> decode(Dictionary&);
|
ErrorOr<void> decode(Dictionary&);
|
||||||
ErrorOr<void> decode(File&);
|
ErrorOr<void> decode(File&);
|
||||||
|
ErrorOr<void> decode(AK::Empty&);
|
||||||
template<typename K, typename V>
|
template<typename K, typename V>
|
||||||
ErrorOr<void> decode(HashMap<K, V>& hashmap)
|
ErrorOr<void> decode(HashMap<K, V>& hashmap)
|
||||||
{
|
{
|
||||||
|
@ -133,6 +136,18 @@ public:
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename... VariantTypes>
|
||||||
|
ErrorOr<void> decode(Variant<VariantTypes...>& variant)
|
||||||
|
{
|
||||||
|
typename AK::Variant<VariantTypes...>::IndexType type_index;
|
||||||
|
TRY(decode(type_index));
|
||||||
|
if (type_index >= sizeof...(VariantTypes))
|
||||||
|
return Error::from_string_literal("IPC: Invalid variant index");
|
||||||
|
|
||||||
|
TRY((decode_variant<0, sizeof...(VariantTypes), VariantTypes...>(type_index, variant)));
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
ErrorOr<void> decode(Optional<T>& optional)
|
ErrorOr<void> decode(Optional<T>& optional)
|
||||||
{
|
{
|
||||||
|
@ -149,6 +164,22 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
template<size_t CurrentIndex, size_t Max, typename... VariantTypes>
|
||||||
|
ErrorOr<void> decode_variant(size_t index, Variant<VariantTypes...>& variant)
|
||||||
|
{
|
||||||
|
if constexpr (CurrentIndex < Max) {
|
||||||
|
if (index == CurrentIndex) {
|
||||||
|
typename TypeList<VariantTypes...>::template Type<CurrentIndex> element;
|
||||||
|
TRY(decode(element));
|
||||||
|
variant.set(move(element));
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
return decode_variant<CurrentIndex + 1, Max, VariantTypes...>(index, variant);
|
||||||
|
} else {
|
||||||
|
VERIFY_NOT_REACHED();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
InputMemoryStream& m_stream;
|
InputMemoryStream& m_stream;
|
||||||
Core::Stream::LocalSocket& m_socket;
|
Core::Stream::LocalSocket& m_socket;
|
||||||
};
|
};
|
||||||
|
|
|
@ -196,6 +196,12 @@ Encoder& Encoder::operator<<(File const& file)
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// No-op.
|
||||||
|
Encoder& Encoder::operator<<(AK::Empty const&)
|
||||||
|
{
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
bool encode(Encoder& encoder, Core::AnonymousBuffer const& buffer)
|
bool encode(Encoder& encoder, Core::AnonymousBuffer const& buffer)
|
||||||
{
|
{
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
#include <AK/Concepts.h>
|
#include <AK/Concepts.h>
|
||||||
#include <AK/HashMap.h>
|
#include <AK/HashMap.h>
|
||||||
#include <AK/StdLibExtras.h>
|
#include <AK/StdLibExtras.h>
|
||||||
|
#include <AK/Variant.h>
|
||||||
#include <LibCore/SharedCircularQueue.h>
|
#include <LibCore/SharedCircularQueue.h>
|
||||||
#include <LibIPC/Forward.h>
|
#include <LibIPC/Forward.h>
|
||||||
#include <LibIPC/Message.h>
|
#include <LibIPC/Message.h>
|
||||||
|
@ -49,6 +50,7 @@ public:
|
||||||
Encoder& operator<<(URL const&);
|
Encoder& operator<<(URL const&);
|
||||||
Encoder& operator<<(Dictionary const&);
|
Encoder& operator<<(Dictionary const&);
|
||||||
Encoder& operator<<(File const&);
|
Encoder& operator<<(File const&);
|
||||||
|
Encoder& operator<<(AK::Empty const&);
|
||||||
template<typename K, typename V>
|
template<typename K, typename V>
|
||||||
Encoder& operator<<(HashMap<K, V> const& hashmap)
|
Encoder& operator<<(HashMap<K, V> const& hashmap)
|
||||||
{
|
{
|
||||||
|
@ -87,6 +89,17 @@ public:
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Note: We require any encodeable variant to have Empty as its first variant, as only possibly-empty variants can be default constructed.
|
||||||
|
// The default constructability is required by generated IPC message marshalling code.
|
||||||
|
template<typename... VariantTypes>
|
||||||
|
Encoder& operator<<(AK::Variant<AK::Empty, VariantTypes...> const& variant)
|
||||||
|
{
|
||||||
|
// Note: This might be either u8 or size_t depending on the size of the variant; both are encodeable.
|
||||||
|
*this << variant.index();
|
||||||
|
variant.visit([this](auto const& underlying_value) { *this << underlying_value; });
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
template<Enum T>
|
template<Enum T>
|
||||||
Encoder& operator<<(T const& enum_value)
|
Encoder& operator<<(T const& enum_value)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue