summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorTim Schumacher <timschumi@gmx.de>2023-01-19 10:27:19 +0100
committerLinus Groh <mail@linusgroh.de>2023-01-24 12:47:39 +0000
commit6ccda76a7161a705aa83d35da970f10807728cd6 (patch)
tree20440f6b9a73db389823fbde9748fcb6a384b3ba /Userland
parent8b5b767465defd5326f92a0364252f80aa78bcfe (diff)
downloadserenity-6ccda76a7161a705aa83d35da970f10807728cd6.zip
LibCore: Add support for non-trivial types to `Stream::*_value`
At the moment, there is no immediate advantage compared to just calling the underlying functions directly, but having a common interface feels more ergonomic (users don't have to care about how a type serializes) and maybe we'll find a way to hide the actual implementation from direct access some time in the future.
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibCore/Stream.h14
1 files changed, 14 insertions, 0 deletions
diff --git a/Userland/Libraries/LibCore/Stream.h b/Userland/Libraries/LibCore/Stream.h
index 7e3cf6f23a..1351a41a8a 100644
--- a/Userland/Libraries/LibCore/Stream.h
+++ b/Userland/Libraries/LibCore/Stream.h
@@ -102,6 +102,13 @@ public:
virtual ErrorOr<void> write_entire_buffer(ReadonlyBytes);
template<typename T>
+ requires(requires(Stream& stream) { { T::read_from_stream(stream) } -> SameAs<ErrorOr<T>>; })
+ ErrorOr<T> read_value()
+ {
+ return T::read_from_stream(*this);
+ }
+
+ template<typename T>
requires(Traits<T>::is_trivially_serializable())
ErrorOr<T> read_value()
{
@@ -111,6 +118,13 @@ public:
}
template<typename T>
+ requires(requires(T t, Stream& stream) { { t.write_to_stream(stream) } -> SameAs<ErrorOr<void>>; })
+ ErrorOr<void> write_value(T const& value)
+ {
+ return value.write_to_stream(*this);
+ }
+
+ template<typename T>
requires(Traits<T>::is_trivially_serializable())
ErrorOr<void> write_value(T const& value)
{