1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 04:27:45 +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:
kleines Filmröllchen 2022-11-09 17:05:04 +01:00 committed by Tim Flynn
parent 5b4818df22
commit a06b277471
4 changed files with 56 additions and 0 deletions

View file

@ -12,6 +12,8 @@
#include <AK/NumericLimits.h>
#include <AK/StdLibExtras.h>
#include <AK/Try.h>
#include <AK/TypeList.h>
#include <AK/Variant.h>
#include <LibCore/SharedCircularQueue.h>
#include <LibCore/Stream.h>
#include <LibIPC/File.h>
@ -53,6 +55,7 @@ public:
ErrorOr<void> decode(URL&);
ErrorOr<void> decode(Dictionary&);
ErrorOr<void> decode(File&);
ErrorOr<void> decode(AK::Empty&);
template<typename K, typename V>
ErrorOr<void> decode(HashMap<K, V>& hashmap)
{
@ -133,6 +136,18 @@ public:
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>
ErrorOr<void> decode(Optional<T>& optional)
{
@ -149,6 +164,22 @@ public:
}
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;
Core::Stream::LocalSocket& m_socket;
};