mirror of
https://github.com/SerenityOS/serenity
synced 2024-11-03 02:19:45 +00:00
LibIPC: Add support for encoding and decoding Array<T, N>
Also add a note to the Concepts header that the reason we have all the strange concepts in place for container types is to work around the language limitation that we cannot partially specialize function templates.
This commit is contained in:
parent
e09bfc1a8c
commit
7e6918e14a
3 changed files with 33 additions and 0 deletions
|
@ -21,6 +21,8 @@
|
|||
//
|
||||
// Then decode<int>() would be ambiguous because either declaration could work (the compiler would
|
||||
// not be able to distinguish if you wanted to decode an int or a Vector of int).
|
||||
//
|
||||
// They also serve to work around the inability to do partial function specialization in C++.
|
||||
namespace IPC::Concepts {
|
||||
|
||||
namespace Detail {
|
||||
|
@ -50,6 +52,11 @@ constexpr inline bool IsVector = false;
|
|||
template<typename T>
|
||||
constexpr inline bool IsVector<Vector<T>> = true;
|
||||
|
||||
template<typename T>
|
||||
constexpr inline bool IsArray = false;
|
||||
template<typename T, size_t N>
|
||||
constexpr inline bool IsArray<Array<T, N>> = true;
|
||||
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
|
@ -67,4 +74,7 @@ concept Variant = Detail::IsVariant<T>;
|
|||
template<typename T>
|
||||
concept Vector = Detail::IsVector<T>;
|
||||
|
||||
template<typename T>
|
||||
concept Array = Detail::IsArray<T>;
|
||||
|
||||
}
|
||||
|
|
|
@ -108,6 +108,18 @@ ErrorOr<File> decode(Decoder&);
|
|||
template<>
|
||||
ErrorOr<Empty> decode(Decoder&);
|
||||
|
||||
template<Concepts::Array T>
|
||||
ErrorOr<T> decode(Decoder& decoder)
|
||||
{
|
||||
T array {};
|
||||
auto size = TRY(decoder.decode_size());
|
||||
if (size != array.size())
|
||||
return Error::from_string_literal("Array size mismatch");
|
||||
for (size_t i = 0; i < array.size(); ++i)
|
||||
array[i] = TRY(decoder.decode<typename T::ValueType>());
|
||||
return array;
|
||||
}
|
||||
|
||||
template<Concepts::Vector T>
|
||||
ErrorOr<T> decode(Decoder& decoder)
|
||||
{
|
||||
|
|
|
@ -108,6 +108,17 @@ 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)
|
||||
{
|
||||
TRY(encoder.encode_size(array.size()));
|
||||
|
||||
for (auto const& value : array)
|
||||
TRY(encoder.encode(value));
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
template<Concepts::Vector T>
|
||||
ErrorOr<void> encode(Encoder& encoder, T const& vector)
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue