Explorar o código

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>
kleines Filmröllchen %!s(int64=2) %!d(string=hai) anos
pai
achega
a06b277471

+ 6 - 0
Userland/Libraries/LibIPC/Decoder.cpp

@@ -208,4 +208,10 @@ ErrorOr<void> decode(Decoder& decoder, Core::ProxyData& data)
     return {};
 }
 
+// No-op.
+ErrorOr<void> Decoder::decode(AK::Empty&)
+{
+    return {};
+}
+
 }

+ 31 - 0
Userland/Libraries/LibIPC/Decoder.h

@@ -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;
 };

+ 6 - 0
Userland/Libraries/LibIPC/Encoder.cpp

@@ -196,6 +196,12 @@ Encoder& Encoder::operator<<(File const& file)
     return *this;
 }
 
+// No-op.
+Encoder& Encoder::operator<<(AK::Empty const&)
+{
+    return *this;
+}
+
 template<>
 bool encode(Encoder& encoder, Core::AnonymousBuffer const& buffer)
 {

+ 13 - 0
Userland/Libraries/LibIPC/Encoder.h

@@ -9,6 +9,7 @@
 #include <AK/Concepts.h>
 #include <AK/HashMap.h>
 #include <AK/StdLibExtras.h>
+#include <AK/Variant.h>
 #include <LibCore/SharedCircularQueue.h>
 #include <LibIPC/Forward.h>
 #include <LibIPC/Message.h>
@@ -49,6 +50,7 @@ public:
     Encoder& operator<<(URL const&);
     Encoder& operator<<(Dictionary const&);
     Encoder& operator<<(File const&);
+    Encoder& operator<<(AK::Empty const&);
     template<typename K, typename V>
     Encoder& operator<<(HashMap<K, V> const& hashmap)
     {
@@ -87,6 +89,17 @@ public:
         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>
     Encoder& operator<<(T const& enum_value)
     {