cpp-common/bt2c: add format_as() template functions for wise enums
authorSimon Marchi <simon.marchi@efficios.com>
Thu, 4 Apr 2024 03:01:05 +0000 (23:01 -0400)
committerSimon Marchi <simon.marchi@efficios.com>
Wed, 17 Apr 2024 17:57:53 +0000 (13:57 -0400)
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 <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/12248
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
Tested-by: jenkins <jenkins@lttng.org>
src/cpp-common/bt2c/fmt.hpp

index 0121806da584e17138e4c40a730056a43f0f5489..4633ebca73b03233e1cc2c9ec7e9df120c66d463 100644 (file)
@@ -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 <typename T>
+using EnableIfIsWiseEnum =
+    typename std::enable_if<wise_enum::is_wise_enum<T>::value, const char *>::type;
+
+} /* namespace internal */
+
 namespace bt2 {
 
+template <typename T>
+::internal::EnableIfIsWiseEnum<T> format_as(const T val) noexcept
+{
+    return wise_enum::to_string<T>(val);
+}
+
 inline const char *format_as(const MessageType type)
 {
     return bt_common_message_type_string(static_cast<bt_message_type>(type));
@@ -21,6 +36,12 @@ inline const char *format_as(const MessageType type)
 
 namespace bt2c {
 
+template <typename T>
+::internal::EnableIfIsWiseEnum<T> format_as(const T val) noexcept
+{
+    return wise_enum::to_string<T>(val);
+}
+
 inline std::string format_as(const UuidView uuid)
 {
     return uuid.str();
This page took 0.026148 seconds and 4 git commands to generate.