mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 04:47:35 +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
|
@ -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)
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue