summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibIPC
diff options
context:
space:
mode:
authorkleines Filmröllchen <filmroellchen@serenityos.org>2022-11-09 17:05:04 +0100
committerTim Flynn <trflynn89@pm.me>2022-12-13 10:24:59 -0500
commita06b2774712922a2c2a149b92864689be9ed24f8 (patch)
tree660dd096f4a271e47896206f97bd3a024b5b7efd /Userland/Libraries/LibIPC
parent5b4818df22769bc3353c33b1f1a8d299d96e6efc (diff)
downloadserenity-a06b2774712922a2c2a149b92864689be9ed24f8.zip
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>
Diffstat (limited to 'Userland/Libraries/LibIPC')
-rw-r--r--Userland/Libraries/LibIPC/Decoder.cpp6
-rw-r--r--Userland/Libraries/LibIPC/Decoder.h31
-rw-r--r--Userland/Libraries/LibIPC/Encoder.cpp6
-rw-r--r--Userland/Libraries/LibIPC/Encoder.h13
4 files changed, 56 insertions, 0 deletions
diff --git a/Userland/Libraries/LibIPC/Decoder.cpp b/Userland/Libraries/LibIPC/Decoder.cpp
index 3337aa012d..04ef566e99 100644
--- a/Userland/Libraries/LibIPC/Decoder.cpp
+++ b/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 {};
+}
+
}
diff --git a/Userland/Libraries/LibIPC/Decoder.h b/Userland/Libraries/LibIPC/Decoder.h
index a20efa38d6..1cc3902a58 100644
--- a/Userland/Libraries/LibIPC/Decoder.h
+++ b/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;
};
diff --git a/Userland/Libraries/LibIPC/Encoder.cpp b/Userland/Libraries/LibIPC/Encoder.cpp
index 1794915cdb..28121ef519 100644
--- a/Userland/Libraries/LibIPC/Encoder.cpp
+++ b/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)
{
diff --git a/Userland/Libraries/LibIPC/Encoder.h b/Userland/Libraries/LibIPC/Encoder.h
index e1531c33fa..1d1696a4a0 100644
--- a/Userland/Libraries/LibIPC/Encoder.h
+++ b/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)
{