From dfc7212c2332616cb37a3c2f4dfba433fb8a1239 Mon Sep 17 00:00:00 2001 From: sehraf Date: Mon, 10 Feb 2020 18:04:34 +0100 Subject: [PATCH] document how to convert enum class flags to boolean and fix RsFlags << operator --- libretroshare/src/retroshare/rsflags.h | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/libretroshare/src/retroshare/rsflags.h b/libretroshare/src/retroshare/rsflags.h index cdea3f5be..8adcc8e4f 100644 --- a/libretroshare/src/retroshare/rsflags.h +++ b/libretroshare/src/retroshare/rsflags.h @@ -36,7 +36,13 @@ using rs_is_scoped_enum = std::integral_constant< bool, * it as flags type passing it as parameter of this macro. * The result will be type safe flags, that cannot be mixed up with flag of a * different type, but that are very comfortable to operate like plain old - * integers. + * integers. All commom operation like &, | or ! can be used. To convert + * the result of such operation to boolean use !!: +@code{.cpp} +RsConnectModes connect = rsConfig->getConnectModes(); +if (!!(connect & RsConnectModes::OUTGOING_TCP)) +@endcode + * * This macro support flag fields of different lenght depending on what * underlining type (usually from uint8_t up to uint64_t) has been declared for * the enum class. @@ -44,6 +50,7 @@ using rs_is_scoped_enum = std::integral_constant< bool, * underlining type of the enum otherwise different compilers may serialize a * flag variable with different lenght, potentially causing interoperability * issues between differents builds. + * * Usage example: @code{.cpp} enum class RsGrouterItemFlags : uint32_t @@ -129,9 +136,9 @@ operator <<(std::ostream& stream, EFT flags) { using u_t = typename std::underlying_type::type; - for(int i = sizeof(u_t); i>=0; --i) + for(int i = sizeof(u_t) << 3; i>=0; --i) { - stream << (flags & ( 1 << i ) ? "1" : "0"); + stream << ((static_cast(flags) & ( 1 << i )) ? "1" : "0"); if( i % 8 == 0 ) stream << " "; } return stream;