Skip to content

Commit 1858163

Browse files
committed
LibIPC: Add a built-in encoder/decoder for AK::Time
1 parent 8eeedce commit 1858163

File tree

4 files changed

+20
-0
lines changed

4 files changed

+20
-0
lines changed

Userland/Libraries/LibIPC/Decoder.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ ErrorOr<JsonValue> decode(Decoder& decoder)
6262
return JsonValue::from_string(json);
6363
}
6464

65+
template<>
66+
ErrorOr<Time> decode(Decoder& decoder)
67+
{
68+
auto nanoseconds = TRY(decoder.decode<i64>());
69+
return AK::Time::from_nanoseconds(nanoseconds);
70+
}
71+
6572
template<>
6673
ErrorOr<URL> decode(Decoder& decoder)
6774
{

Userland/Libraries/LibIPC/Decoder.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ ErrorOr<ByteBuffer> decode(Decoder&);
8787
template<>
8888
ErrorOr<JsonValue> decode(Decoder&);
8989

90+
template<>
91+
ErrorOr<Time> decode(Decoder&);
92+
9093
template<>
9194
ErrorOr<URL> decode(Decoder&);
9295

Userland/Libraries/LibIPC/Encoder.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <AK/JsonObject.h>
1212
#include <AK/JsonValue.h>
1313
#include <AK/NumericLimits.h>
14+
#include <AK/Time.h>
1415
#include <AK/URL.h>
1516
#include <LibCore/AnonymousBuffer.h>
1617
#include <LibCore/DateTime.h>
@@ -73,6 +74,12 @@ ErrorOr<void> encode(Encoder& encoder, JsonValue const& value)
7374
return encoder.encode(value.serialized<StringBuilder>());
7475
}
7576

77+
template<>
78+
ErrorOr<void> encode(Encoder& encoder, Time const& value)
79+
{
80+
return encoder.encode(value.to_nanoseconds());
81+
}
82+
7683
template<>
7784
ErrorOr<void> encode(Encoder& encoder, URL const& value)
7885
{

Userland/Libraries/LibIPC/Encoder.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ ErrorOr<void> encode(Encoder&, ByteBuffer const&);
118118
template<>
119119
ErrorOr<void> encode(Encoder&, JsonValue const&);
120120

121+
template<>
122+
ErrorOr<void> encode(Encoder&, Time const&);
123+
121124
template<>
122125
ErrorOr<void> encode(Encoder&, URL const&);
123126

0 commit comments

Comments
 (0)