From 785ebfad2fff4fde55104258e52218b153c035a0 Mon Sep 17 00:00:00 2001 From: Arnaud Giersch Date: Fri, 5 Feb 2021 15:06:28 +0100 Subject: [PATCH] Add helper macro to declare enums with to_string. --- include/xbt/utility.hpp | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/include/xbt/utility.hpp b/include/xbt/utility.hpp index 2a1541ac8a..9634819ed9 100644 --- a/include/xbt/utility.hpp +++ b/include/xbt/utility.hpp @@ -7,8 +7,23 @@ #ifndef XBT_UTILITY_HPP #define XBT_UTILITY_HPP -#include #include +#include +#include + +/** @brief Helper macro to declare enum class + * + * Declares an enum class EnumType, and a function "const char* to_c_str(EnumType)" to retrieve a C-string description + * for each value. + */ +#define XBT_DECLARE_ENUM_CLASS(EnumType, ...) \ + enum class EnumType; \ + static constexpr char const* to_c_str(EnumType value) \ + { \ + constexpr std::array names{{_XBT_STRINGIFY_ARGS(__VA_ARGS__)}}; \ + return names[static_cast(value)]; \ + } \ + enum class EnumType { __VA_ARGS__ } /* defined here to handle trailing semicolon */ namespace simgrid { namespace xbt { -- 2.20.1