2020-01-18 05:38:21 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
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
|
|
|
|
|
|
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);
|
|
|
|
|
Encoder& operator<<(const char*);
|
|
|
|
|
Encoder& operator<<(const StringView&);
|
|
|
|
|
Encoder& operator<<(const String&);
|
2020-11-07 16:39:45 -03:00
|
|
|
Encoder& operator<<(const ByteBuffer&);
|
2020-06-07 17:54:27 -03:00
|
|
|
Encoder& operator<<(const URL&);
|
2020-05-03 17:15:27 -03:00
|
|
|
Encoder& operator<<(const Dictionary&);
|
2020-11-21 15:59:12 -03:00
|
|
|
Encoder& operator<<(const File&);
|
2020-11-07 16:39:45 -03:00
|
|
|
template<typename K, typename V>
|
|
|
|
|
Encoder& operator<<(const HashMap<K, V>& hashmap)
|
|
|
|
|
{
|
|
|
|
|
*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>
|
|
|
|
|
Encoder& operator<<(const Vector<T>& vector)
|
|
|
|
|
{
|
|
|
|
|
*this << (u64)vector.size();
|
|
|
|
|
for (auto& value : vector)
|
|
|
|
|
*this << value;
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
|
Encoder& operator<<(const T& value)
|
|
|
|
|
{
|
|
|
|
|
encode(value);
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-12 14:02:44 -03:00
|
|
|
template<typename T>
|
|
|
|
|
Encoder& operator<<(const Optional<T>& optional)
|
|
|
|
|
{
|
|
|
|
|
*this << optional.has_value();
|
|
|
|
|
if (optional.has_value())
|
|
|
|
|
*this << optional.value();
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-12 13:05:43 -03:00
|
|
|
template<typename T>
|
|
|
|
|
void encode(const T& value)
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
|
|
|
|
}
|