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