From 0a0b3c4b9ff55020ef490106ff8f1d12c8f122e4 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 20 Jul 2018 14:49:13 -0400 Subject: [PATCH] ipc_helpers: Add PushEnum() member function to ResponseBuilder Allows pushing strongly-typed enum members without the need to always cast them at the call sites. Note that we *only* allow strongly-typed enums in this case. The reason for this is that strongly typed enums have a guaranteed defined size, so the size of the data being pushed is always deterministic. With regular enums this can be a little more error-prone, so we disallow them. This function simply uses the underlying type of the enum to determine the size of the data. For example, if an enum is defined as: enum class SomeEnum : u16 { SomeEntry }; if PushEnum(SomeEnum::SomeEntry); is called, then it will push a u16-size amount of data. --- src/core/hle/ipc_helpers.h | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/core/hle/ipc_helpers.h b/src/core/hle/ipc_helpers.h index 24605a2739..8b5b06f31d 100644 --- a/src/core/hle/ipc_helpers.h +++ b/src/core/hle/ipc_helpers.h @@ -174,6 +174,25 @@ public: template void Push(const First& first_value, const Other&... other_values); + /** + * Helper function for pushing strongly-typed enumeration values. + * + * @tparam Enum The enumeration type to be pushed + * + * @param value The value to push. + * + * @note The underlying size of the enumeration type is the size of the + * data that gets pushed. e.g. "enum class SomeEnum : u16" will + * push a u16-sized amount of data. + */ + template + void PushEnum(Enum value) { + static_assert(std::is_enum_v, "T must be an enum type within a PushEnum call."); + static_assert(!std::is_convertible_v, + "enum type in PushEnum must be a strongly typed enum."); + Push(static_cast>(value)); + } + /** * @brief Copies the content of the given trivially copyable class to the buffer as a normal * param