2020-01-18 09:38:21 +01:00
|
|
|
/*
|
2021-09-03 12:54:51 +02:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2019-12-30 02:41:45 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
2021-07-04 14:44:34 +10:00
|
|
|
#include <AK/Concepts.h>
|
2022-09-13 17:42:39 +02:00
|
|
|
#include <AK/HashMap.h>
|
2021-07-04 14:44:34 +10:00
|
|
|
#include <AK/StdLibExtras.h>
|
2022-02-01 20:49:29 +01:00
|
|
|
#include <LibCore/SharedCircularQueue.h>
|
2020-05-03 22:15:27 +02:00
|
|
|
#include <LibIPC/Forward.h>
|
2020-02-06 14:54:09 +01:00
|
|
|
#include <LibIPC/Message.h>
|
2019-12-30 02:41:45 +01:00
|
|
|
|
2020-02-05 19:57:18 +01:00
|
|
|
namespace IPC {
|
|
|
|
|
|
2020-05-12 18:05:43 +02:00
|
|
|
template<typename T>
|
2020-09-20 13:00:54 +02:00
|
|
|
bool encode(Encoder&, T&)
|
2020-05-12 18:05:43 +02:00
|
|
|
{
|
2020-10-03 16:26:09 +03:30
|
|
|
static_assert(DependentFalse<T>, "Base IPC::encode() was instantiated");
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-05-12 18:05:43 +02:00
|
|
|
}
|
|
|
|
|
|
2020-02-05 19:57:18 +01:00
|
|
|
class Encoder {
|
2019-12-30 02:41:45 +01:00
|
|
|
public:
|
2020-02-05 19:57:18 +01:00
|
|
|
explicit Encoder(MessageBuffer& buffer)
|
2019-12-30 02:41:45 +01:00
|
|
|
: m_buffer(buffer)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-15 11:27:47 +01:00
|
|
|
Encoder& operator<<(bool);
|
|
|
|
|
Encoder& operator<<(u8);
|
|
|
|
|
Encoder& operator<<(u16);
|
2021-11-29 02:18:58 +01:00
|
|
|
Encoder& operator<<(unsigned);
|
|
|
|
|
Encoder& operator<<(unsigned long);
|
|
|
|
|
Encoder& operator<<(unsigned long long);
|
2020-02-15 11:27:47 +01:00
|
|
|
Encoder& operator<<(i8);
|
|
|
|
|
Encoder& operator<<(i16);
|
|
|
|
|
Encoder& operator<<(i32);
|
|
|
|
|
Encoder& operator<<(i64);
|
|
|
|
|
Encoder& operator<<(float);
|
2021-08-26 03:05:01 +02:00
|
|
|
Encoder& operator<<(double);
|
2021-09-03 12:54:51 +02:00
|
|
|
Encoder& operator<<(char const*);
|
2021-11-11 00:55:02 +01:00
|
|
|
Encoder& operator<<(StringView);
|
2021-09-03 12:54:51 +02:00
|
|
|
Encoder& operator<<(String const&);
|
|
|
|
|
Encoder& operator<<(ByteBuffer const&);
|
2022-11-07 16:30:55 -05:00
|
|
|
Encoder& operator<<(JsonValue const&);
|
2021-09-03 12:54:51 +02:00
|
|
|
Encoder& operator<<(URL const&);
|
|
|
|
|
Encoder& operator<<(Dictionary const&);
|
|
|
|
|
Encoder& operator<<(File const&);
|
2020-11-07 23:09:45 +03:30
|
|
|
template<typename K, typename V>
|
2021-09-03 12:54:51 +02:00
|
|
|
Encoder& operator<<(HashMap<K, V> const& hashmap)
|
2022-04-03 00:43:22 +03:00
|
|
|
{
|
|
|
|
|
*this << (u32)hashmap.size();
|
|
|
|
|
for (auto it : hashmap) {
|
|
|
|
|
*this << it.key;
|
|
|
|
|
*this << it.value;
|
|
|
|
|
}
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename K, typename V>
|
|
|
|
|
Encoder& operator<<(OrderedHashMap<K, V> const& hashmap)
|
2020-11-07 23:09:45 +03:30
|
|
|
{
|
|
|
|
|
*this << (u32)hashmap.size();
|
|
|
|
|
for (auto it : hashmap) {
|
|
|
|
|
*this << it.key;
|
|
|
|
|
*this << it.value;
|
|
|
|
|
}
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
2019-12-30 02:41:45 +01:00
|
|
|
|
2020-05-12 18:05:43 +02:00
|
|
|
template<typename T>
|
2021-09-03 12:54:51 +02:00
|
|
|
Encoder& operator<<(Vector<T> const& vector)
|
2020-05-12 18:05:43 +02:00
|
|
|
{
|
|
|
|
|
*this << (u64)vector.size();
|
|
|
|
|
for (auto& value : vector)
|
|
|
|
|
*this << value;
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-01 20:49:29 +01:00
|
|
|
template<typename T, size_t Size>
|
|
|
|
|
Encoder& operator<<(Core::SharedSingleProducerCircularQueue<T, Size> const& queue)
|
|
|
|
|
{
|
|
|
|
|
*this << IPC::File(queue.fd());
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-04 14:44:34 +10:00
|
|
|
template<Enum T>
|
|
|
|
|
Encoder& operator<<(T const& enum_value)
|
|
|
|
|
{
|
|
|
|
|
*this << AK::to_underlying(enum_value);
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-12 18:05:43 +02:00
|
|
|
template<typename T>
|
2021-09-03 12:54:51 +02:00
|
|
|
Encoder& operator<<(T const& value)
|
2020-05-12 18:05:43 +02:00
|
|
|
{
|
|
|
|
|
encode(value);
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-12 19:02:44 +02:00
|
|
|
template<typename T>
|
2021-09-03 12:54:51 +02:00
|
|
|
Encoder& operator<<(Optional<T> const& optional)
|
2020-05-12 19:02:44 +02:00
|
|
|
{
|
|
|
|
|
*this << optional.has_value();
|
|
|
|
|
if (optional.has_value())
|
|
|
|
|
*this << optional.value();
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-12 18:05:43 +02:00
|
|
|
template<typename T>
|
2021-09-03 12:54:51 +02:00
|
|
|
void encode(T const& value)
|
2020-05-12 18:05:43 +02:00
|
|
|
{
|
|
|
|
|
IPC::encode(*this, value);
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-30 02:41:45 +01:00
|
|
|
private:
|
2021-11-29 02:18:58 +01:00
|
|
|
void encode_u32(u32);
|
|
|
|
|
void encode_u64(u64);
|
|
|
|
|
|
2020-02-05 19:57:18 +01:00
|
|
|
MessageBuffer& m_buffer;
|
2019-12-30 02:41:45 +01:00
|
|
|
};
|
2020-02-05 19:57:18 +01:00
|
|
|
|
|
|
|
|
}
|