From e42484bb656187229c6902c572f406be0fd637cf Mon Sep 17 00:00:00 2001 From: Timothy Date: Sun, 4 Jul 2021 14:44:34 +1000 Subject: [PATCH] AK+LibIPC: Make all enums codable If an enum has a supported underlying type we can provide encoding and decoding for the enum as well. --- AK/Concepts.h | 4 ++++ Userland/Libraries/LibIPC/Decoder.h | 12 ++++++++++++ Userland/Libraries/LibIPC/Encoder.h | 9 +++++++++ 3 files changed, 25 insertions(+) diff --git a/AK/Concepts.h b/AK/Concepts.h index f7e4de9aee..ced4b29142 100644 --- a/AK/Concepts.h +++ b/AK/Concepts.h @@ -26,6 +26,9 @@ concept Signed = IsSigned; template concept Unsigned = IsUnsigned; +template +concept Enum = IsEnum; + template concept SameAs = IsSame; @@ -52,6 +55,7 @@ concept IteratorFunction = requires(Func func, Args... args) } using AK::Concepts::Arithmetic; +using AK::Concepts::Enum; using AK::Concepts::FloatingPoint; using AK::Concepts::Integral; using AK::Concepts::IteratorFunction; diff --git a/Userland/Libraries/LibIPC/Decoder.h b/Userland/Libraries/LibIPC/Decoder.h index 7a288f8448..84f066ae22 100644 --- a/Userland/Libraries/LibIPC/Decoder.h +++ b/Userland/Libraries/LibIPC/Decoder.h @@ -6,6 +6,7 @@ #pragma once +#include #include #include #include @@ -66,6 +67,17 @@ public: return true; } + template + bool decode(T& enum_value) + { + UnderlyingType inner_value; + if (!decode(inner_value)) + return false; + + enum_value = T(inner_value); + return true; + } + template bool decode(T& value) { diff --git a/Userland/Libraries/LibIPC/Encoder.h b/Userland/Libraries/LibIPC/Encoder.h index 8f790c34e3..7812902e46 100644 --- a/Userland/Libraries/LibIPC/Encoder.h +++ b/Userland/Libraries/LibIPC/Encoder.h @@ -6,6 +6,8 @@ #pragma once +#include +#include #include #include @@ -62,6 +64,13 @@ public: return *this; } + template + Encoder& operator<<(T const& enum_value) + { + *this << AK::to_underlying(enum_value); + return *this; + } + template Encoder& operator<<(const T& value) {