mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-09-04 08:36:12 +00:00
LibIPC: Add a generic encoder for spans
This commit is contained in:
parent
b090952274
commit
a5b996c079
Notes:
github-actions[bot]
2025-03-09 15:16:14 +00:00
Author: https://github.com/trflynn89
Commit: a5b996c079
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/3865
2 changed files with 17 additions and 11 deletions
|
@ -6,6 +6,7 @@
|
|||
|
||||
#include <AK/HashMap.h>
|
||||
#include <AK/Optional.h>
|
||||
#include <AK/Span.h>
|
||||
#include <AK/Variant.h>
|
||||
#include <AK/Vector.h>
|
||||
#include <LibCore/SharedCircularQueue.h>
|
||||
|
@ -27,7 +28,8 @@ namespace IPC::Concepts {
|
|||
|
||||
namespace Detail {
|
||||
|
||||
// Cannot use SpecializationOf with these templates because they have non-type parameters. See https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p1985r3.pdf
|
||||
// Cannot use SpecializationOf with these templates because they have non-type parameters. See:
|
||||
// https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p1985r3.pdf
|
||||
template<typename T>
|
||||
constexpr inline bool IsArray = false;
|
||||
template<typename T, size_t N>
|
||||
|
@ -56,6 +58,9 @@ concept Array = Detail::IsArray<T>;
|
|||
template<typename T>
|
||||
concept Vector = Detail::IsVector<T>;
|
||||
|
||||
template<typename T>
|
||||
concept Span = SpecializationOf<T, AK::Span>;
|
||||
|
||||
template<typename T>
|
||||
concept HashMap = Detail::IsHashMap<T>;
|
||||
|
||||
|
|
|
@ -116,26 +116,27 @@ ErrorOr<void> encode(Encoder&, File const&);
|
|||
template<>
|
||||
ErrorOr<void> encode(Encoder&, Empty const&);
|
||||
|
||||
template<typename T, size_t N>
|
||||
ErrorOr<void> encode(Encoder& encoder, Array<T, N> const& array)
|
||||
template<Concepts::Span T>
|
||||
ErrorOr<void> encode(Encoder& encoder, T const& span)
|
||||
{
|
||||
TRY(encoder.encode_size(array.size()));
|
||||
TRY(encoder.encode_size(span.size()));
|
||||
|
||||
for (auto const& value : array)
|
||||
for (auto const& value : span)
|
||||
TRY(encoder.encode(value));
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
template<typename T, size_t N>
|
||||
ErrorOr<void> encode(Encoder& encoder, Array<T, N> const& array)
|
||||
{
|
||||
return encoder.encode(array.span());
|
||||
}
|
||||
|
||||
template<Concepts::Vector T>
|
||||
ErrorOr<void> encode(Encoder& encoder, T const& vector)
|
||||
{
|
||||
TRY(encoder.encode_size(vector.size()));
|
||||
|
||||
for (auto const& value : vector)
|
||||
TRY(encoder.encode(value));
|
||||
|
||||
return {};
|
||||
return encoder.encode(vector.span());
|
||||
}
|
||||
|
||||
template<Concepts::HashMap T>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue