From 91730cec9609a4d62c5f387ca3627d2233685982 Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Wed, 3 Apr 2024 23:01:05 -0400 Subject: [PATCH] cpp-common/bt2c: add format_as() template functions for wise enums Add some format_as() functions in the bt2 and bt2c namespaces that will apply to all wise enums defined in that namespace. Change-Id: I4952f062fc36e94925d2f0b093ac09d0fae6fdc0 Signed-off-by: Simon Marchi Reviewed-on: https://review.lttng.org/c/babeltrace/+/12248 Reviewed-by: Philippe Proulx Tested-by: jenkins --- src/cpp-common/bt2c/fmt.hpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/cpp-common/bt2c/fmt.hpp b/src/cpp-common/bt2c/fmt.hpp index 0121806d..4633ebca 100644 --- a/src/cpp-common/bt2c/fmt.hpp +++ b/src/cpp-common/bt2c/fmt.hpp @@ -7,11 +7,26 @@ #include "common/common.h" #include "cpp-common/bt2/message.hpp" #include "cpp-common/vendor/fmt/format.h" /* IWYU pragma: keep */ +#include "cpp-common/vendor/wise-enum/wise_enum.h" #include "uuid.hpp" +namespace internal { + +template +using EnableIfIsWiseEnum = + typename std::enable_if::value, const char *>::type; + +} /* namespace internal */ + namespace bt2 { +template +::internal::EnableIfIsWiseEnum format_as(const T val) noexcept +{ + return wise_enum::to_string(val); +} + inline const char *format_as(const MessageType type) { return bt_common_message_type_string(static_cast(type)); @@ -21,6 +36,12 @@ inline const char *format_as(const MessageType type) namespace bt2c { +template +::internal::EnableIfIsWiseEnum format_as(const T val) noexcept +{ + return wise_enum::to_string(val); +} + inline std::string format_as(const UuidView uuid) { return uuid.str(); -- 2.34.1