2424#include < string>
2525#include < sstream>
2626#include < limits>
27+ #include < bit>
2728#include < bitset>
2829#include < initializer_list>
2930#include < cstdint>
3435#include < iostream>
3536#include < iomanip>
3637
38+ #if !defined(__CUDACC__) && !defined(__HIPCC__)
39+ #define O2_ENUMFLAGS_ENABLE_REFLECTION 1
40+ #else
41+ #define O2_ENUMFLAGS_ENABLE_REFLECTION 0
42+ #endif
43+
44+ #if O2_ENUMFLAGS_ENABLE_REFLECTION
3745#include " CommonUtils/StringUtils.h"
46+ #endif
3847
3948namespace o2 ::utils
4049{
@@ -55,6 +64,7 @@ concept EnumFlagHelper = requires {
5564// This is very much inspired by much more extensive libraries like magic_enum.
5665// Inspiration by its c++20 version (https://github.com/fix8mt/conjure_enum).
5766// NOTE: Cannot detect if bit values past the underlying type are defined.
67+ #if O2_ENUMFLAGS_ENABLE_REFLECTION
5868template <EnumFlagHelper E>
5969struct FlagsHelper final {
6070 using U = std::underlying_type_t <E>;
@@ -317,10 +327,12 @@ struct FlagsHelper final {
317327 return false ;
318328 }
319329};
330+ #endif
320331
321332} // namespace details::enum_flags
322333
323334// Require an enum to fullfil what one would except from a bitset.
335+ #if O2_ENUMFLAGS_ENABLE_REFLECTION
324336template <typename E>
325337concept EnumFlag = requires {
326338 // range checks
@@ -332,6 +344,10 @@ concept EnumFlag = requires {
332344 requires !details::enum_flags::FlagsHelper<E>::hasNone (); // added automatically
333345 requires !details::enum_flags::FlagsHelper<E>::hasAll (); // added automatically
334346};
347+ #else
348+ template <typename E>
349+ concept EnumFlag = details::enum_flags::EnumFlagHelper<E>;
350+ #endif
335351
336352/* *
337353 * \brief Class to aggregate and manage enum-based on-off flags.
@@ -358,7 +374,9 @@ template <EnumFlag E>
358374class EnumFlags
359375{
360376 static constexpr int DefaultBase{2 };
377+ #if O2_ENUMFLAGS_ENABLE_REFLECTION
361378 using H = details::enum_flags::FlagsHelper<E>;
379+ #endif
362380 using U = std::underlying_type_t <E>;
363381 U mBits {0 };
364382
@@ -388,18 +406,21 @@ class EnumFlags
388406 // Initialize with a list of flags.
389407 constexpr EnumFlags (std::initializer_list<E> flags) noexcept
390408 {
391- std::for_each (flags.begin (), flags.end (), [this ](const E f) noexcept { mBits |= to_bit (f); });
409+ for (const E f : flags) {
410+ mBits |= to_bit (f);
411+ }
392412 }
413+ #if O2_ENUMFLAGS_ENABLE_REFLECTION
393414 // Init from a string.
394415 //
395416 explicit EnumFlags (const std::string& str, int base = DefaultBase)
396417 {
397418 set (str, base);
398419 }
399- // Destructor.
400- constexpr ~EnumFlags () = default ;
420+ #endif
401421
402- static constexpr U None{0 }; // Represents no flags set.
422+ static constexpr U None{0 }; // Represents no flags set.
423+ #if O2_ENUMFLAGS_ENABLE_REFLECTION
403424 static constexpr U All{H::MaxRep}; // Represents all flags set.
404425
405426 // Return list of all enum values
@@ -432,6 +453,7 @@ class EnumFlags
432453 throw ;
433454 }
434455 }
456+ #endif
435457 // Returns the raw bitset value.
436458 [[nodiscard]] constexpr auto value () const noexcept
437459 {
@@ -493,6 +515,7 @@ class EnumFlags
493515 }
494516
495517 // Checks if all flags are set.
518+ #if O2_ENUMFLAGS_ENABLE_REFLECTION
496519 [[nodiscard]] constexpr bool all () const noexcept
497520 {
498521 return mBits == All;
@@ -537,6 +560,7 @@ class EnumFlags
537560 }
538561 return oss.str ();
539562 }
563+ #endif
540564
541565 // Checks if any flag is set (Boolean context).
542566 [[nodiscard]] constexpr explicit operator bool () const noexcept
@@ -645,6 +669,7 @@ class EnumFlags
645669 }
646670
647671 // Serializes the flag set to a string.
672+ #if O2_ENUMFLAGS_ENABLE_REFLECTION
648673 [[nodiscard]] std::string serialize () const
649674 {
650675 return std::to_string (mBits );
@@ -659,6 +684,7 @@ class EnumFlags
659684 }
660685 mBits = static_cast <U>(v);
661686 }
687+ #endif
662688
663689 // Counts the number of set bits (active flags).
664690 [[nodiscard]] constexpr size_t count () const noexcept
@@ -686,6 +712,7 @@ class EnumFlags
686712
687713 private:
688714 // Set implementation, bits was zeroed before.
715+ #if O2_ENUMFLAGS_ENABLE_REFLECTION
689716 void setImpl (const std::string& s, int base = 2 )
690717 {
691718 // Helper to check if character is valid for given base
@@ -782,15 +809,20 @@ class EnumFlags
782809 throw std::invalid_argument (" Cannot parse string!" );
783810 }
784811 }
812+ #endif
785813};
786814
815+ #if O2_ENUMFLAGS_ENABLE_REFLECTION
787816template <EnumFlag E>
788817std::ostream& operator <<(std::ostream& os, const EnumFlags<E>& f)
789818{
790819 os << f.pstring (true );
791820 return os;
792821}
822+ #endif
793823
794824} // namespace o2::utils
795825
826+ #undef O2_ENUMFLAGS_ENABLE_REFLECTION
827+
796828#endif
0 commit comments