summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibIPC/Encoder.h
blob: b6a97e2c9d512fd9ee190637f41a88a210200f9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
 * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/Concepts.h>
#include <AK/HashMap.h>
#include <AK/StdLibExtras.h>
#include <AK/Variant.h>
#include <LibCore/SharedCircularQueue.h>
#include <LibIPC/Concepts.h>
#include <LibIPC/Forward.h>
#include <LibIPC/Message.h>

namespace IPC {

template<typename T>
ErrorOr<void> encode(Encoder&, T const&)
{
    static_assert(DependentFalse<T>, "Base IPC::encode() was instantiated");
    VERIFY_NOT_REACHED();
}

class Encoder {
public:
    explicit Encoder(MessageBuffer& buffer)
        : m_buffer(buffer)
    {
    }

    template<typename T>
    ErrorOr<void> encode(T const& value);

    ErrorOr<void> extend_capacity(size_t capacity)
    {
        return m_buffer.data.try_ensure_capacity(m_buffer.data.size() + capacity);
    }

    void append(u8 value)
    {
        m_buffer.data.unchecked_append(value);
    }

    ErrorOr<void> append(u8 const* values, size_t count)
    {
        TRY(extend_capacity(count));
        m_buffer.data.unchecked_append(values, count);
        return {};
    }

    ErrorOr<void> append_file_descriptor(int fd)
    {
        auto auto_fd = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) AutoCloseFileDescriptor(fd)));
        return m_buffer.fds.try_append(move(auto_fd));
    }

    ErrorOr<void> encode_size(size_t size);

private:
    MessageBuffer& m_buffer;
};

template<Arithmetic T>
ErrorOr<void> encode(Encoder& encoder, T const& value)
{
    TRY(encoder.extend_capacity(sizeof(T)));

    if constexpr (sizeof(T) == 1) {
        encoder.append(static_cast<u8>(value));
    } else if constexpr (sizeof(T) == 2) {
        encoder.append(static_cast<u8>(value));
        encoder.append(static_cast<u8>(value >> 8));
    } else if constexpr (sizeof(T) == 4) {
        encoder.append(static_cast<u8>(value));
        encoder.append(static_cast<u8>(value >> 8));
        encoder.append(static_cast<u8>(value >> 16));
        encoder.append(static_cast<u8>(value >> 24));
    } else if constexpr (sizeof(T) == 8) {
        encoder.append(static_cast<u8>(value));
        encoder.append(static_cast<u8>(value >> 8));
        encoder.append(static_cast<u8>(value >> 16));
        encoder.append(static_cast<u8>(value >> 24));
        encoder.append(static_cast<u8>(value >> 32));
        encoder.append(static_cast<u8>(value >> 40));
        encoder.append(static_cast<u8>(value >> 48));
        encoder.append(static_cast<u8>(value >> 56));
    } else {
        static_assert(DependentFalse<T>);
    }

    return {};
}

template<Enum T>
ErrorOr<void> encode(Encoder& encoder, T const& value)
{
    return encoder.encode(to_underlying(value));
}

template<>
ErrorOr<void> encode(Encoder&, float const&);

template<>
ErrorOr<void> encode(Encoder&, double const&);

template<>
ErrorOr<void> encode(Encoder&, StringView const&);

template<>
ErrorOr<void> encode(Encoder&, DeprecatedString const&);

template<>
ErrorOr<void> encode(Encoder&, ByteBuffer const&);

template<>
ErrorOr<void> encode(Encoder&, JsonValue const&);

template<>
ErrorOr<void> encode(Encoder&, URL const&);

template<>
ErrorOr<void> encode(Encoder&, Dictionary const&);

template<>
ErrorOr<void> encode(Encoder&, File const&);

template<>
ErrorOr<void> encode(Encoder&, Empty const&);

template<Concepts::Vector T>
ErrorOr<void> encode(Encoder& encoder, T const& vector)
{
    // NOTE: Do not change this encoding without also updating LibC/netdb.cpp.
    TRY(encoder.encode_size(vector.size()));

    for (auto const& value : vector)
        TRY(encoder.encode(value));

    return {};
}

template<Concepts::HashMap T>
ErrorOr<void> encode(Encoder& encoder, T const& hashmap)
{
    TRY(encoder.encode_size(hashmap.size()));

    for (auto it : hashmap) {
        TRY(encoder.encode(it.key));
        TRY(encoder.encode(it.value));
    }

    return {};
}

template<Concepts::SharedSingleProducerCircularQueue T>
ErrorOr<void> encode(Encoder& encoder, T const& queue)
{
    return encoder.encode(IPC::File { queue.fd() });
}

template<Concepts::Optional T>
ErrorOr<void> encode(Encoder& encoder, T const& optional)
{
    TRY(encoder.encode(optional.has_value()));

    if (optional.has_value())
        TRY(encoder.encode(optional.value()));

    return {};
}

template<Concepts::Variant T>
ErrorOr<void> encode(Encoder& encoder, T const& variant)
{
    TRY(encoder.encode(variant.index()));

    return variant.visit([&](auto const& value) {
        return encoder.encode(value);
    });
}

// This must be last so that it knows about the above specializations.
template<typename T>
ErrorOr<void> Encoder::encode(T const& value)
{
    return IPC::encode(*this, value);
}

}