Преглед изворни кода

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.
Andrew Kaster пре 1 година
родитељ
комит
7e6918e14a

+ 10 - 0
Userland/Libraries/LibIPC/Concepts.h

@@ -21,6 +21,8 @@
 //
 //
 // Then decode<int>() would be ambiguous because either declaration could work (the compiler would
 // 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).
 // 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 IPC::Concepts {
 
 
 namespace Detail {
 namespace Detail {
@@ -50,6 +52,11 @@ constexpr inline bool IsVector = false;
 template<typename T>
 template<typename T>
 constexpr inline bool IsVector<Vector<T>> = true;
 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>
 template<typename T>
@@ -67,4 +74,7 @@ concept Variant = Detail::IsVariant<T>;
 template<typename T>
 template<typename T>
 concept Vector = Detail::IsVector<T>;
 concept Vector = Detail::IsVector<T>;
 
 
+template<typename T>
+concept Array = Detail::IsArray<T>;
+
 }
 }

+ 12 - 0
Userland/Libraries/LibIPC/Decoder.h

@@ -108,6 +108,18 @@ ErrorOr<File> decode(Decoder&);
 template<>
 template<>
 ErrorOr<Empty> decode(Decoder&);
 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>
 template<Concepts::Vector T>
 ErrorOr<T> decode(Decoder& decoder)
 ErrorOr<T> decode(Decoder& decoder)
 {
 {

+ 11 - 0
Userland/Libraries/LibIPC/Encoder.h

@@ -108,6 +108,17 @@ ErrorOr<void> encode(Encoder&, File const&);
 template<>
 template<>
 ErrorOr<void> encode(Encoder&, Empty const&);
 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>
 template<Concepts::Vector T>
 ErrorOr<void> encode(Encoder& encoder, T const& vector)
 ErrorOr<void> encode(Encoder& encoder, T const& vector)
 {
 {