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