diff options
author | Ashod Nakashian <ashod.nakashian@collabora.co.uk> | 2022-04-05 07:57:01 -0400 |
---|---|---|
committer | Andras Timar <andras.timar@collabora.com> | 2022-04-14 10:12:53 +0200 |
commit | 3697552d988b791e65cc67ddb4486ac9eacba5b1 (patch) | |
tree | 70396d19fedf5f05d8ef69be497da5002353478c /common | |
parent | wsd: use STATE_ENUM for SessionState (diff) | |
download | online-3697552d988b791e65cc67ddb4486ac9eacba5b1.tar.gz online-3697552d988b791e65cc67ddb4486ac9eacba5b1.zip |
wsd: support serializing STATE_ENUM to ostream
Change-Id: I460e28488184f4f946eec9f5ae4f220af98cca56
Signed-off-by: Ashod Nakashian <ashod.nakashian@collabora.co.uk>
Diffstat (limited to 'common')
-rw-r--r-- | common/StateEnum.hpp | 15 |
1 files changed, 13 insertions, 2 deletions
diff --git a/common/StateEnum.hpp b/common/StateEnum.hpp index 04149016ea..827f777f5e 100644 --- a/common/StateEnum.hpp +++ b/common/StateEnum.hpp @@ -7,6 +7,8 @@ #pragma once +#include <iosfwd> +#include <type_traits> #include <string> /// Enum macro specifically for state-machines. @@ -48,17 +50,26 @@ /// NAME is the name of the state enum followed by the state names. #define STATE_ENUM(NAME, ...) \ enum class NAME : char; \ - static const char* name(NAME e) \ + static inline const char* name(NAME e) \ { \ static const char* const NAME##_names[] = { FOR_EACH(STRINGIFY2, NAME, __VA_ARGS__) }; \ assert(static_cast<unsigned>(e) < sizeof(NAME##_names) / sizeof(NAME##_names[0]) && \ "Enum value is out of range."); \ return NAME##_names[static_cast<int>(e)]; \ } \ - static std::string toString(NAME e) { return name(e); } \ + static inline std::string toString(NAME e) { return name(e); } \ enum class NAME : char \ { \ __VA_ARGS__ \ } +/// Support seemless serialization of STATE_ENUM to ostream. +template <typename T, typename std::enable_if< + std::is_same<decltype(name(T())), const char*>::value>::type* = nullptr> +inline std::ostream& operator<<(std::ostream& os, const T state) +{ + os << name(state); + return os; +} + /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |