From 6019ea0296a86827155f5546587086218f7031fe Mon Sep 17 00:00:00 2001 From: bergmann Date: Fri, 23 Mar 2018 17:17:43 +0100 Subject: [PATCH] * refactored/improved utl::flags --- include/cpputils/misc/flags.h | 68 +++++++++++++++++++---------------- 1 file changed, 38 insertions(+), 30 deletions(-) diff --git a/include/cpputils/misc/flags.h b/include/cpputils/misc/flags.h index 39ada59..9b471b5 100644 --- a/include/cpputils/misc/flags.h +++ b/include/cpputils/misc/flags.h @@ -6,50 +6,58 @@ namespace utl { - template + template struct op_flag_convert_none { - static inline TBase to_base(const TEnum e) - { return static_cast(e); } + static inline T_base to_base(const T_enum e) + { return static_cast(e); } }; - template + template struct op_flag_convert_shift { - static inline TBase to_base(const TEnum e) - { return static_cast(1 << static_cast(e)); } + static inline T_base to_base(const T_enum e) + { return static_cast(1 << static_cast(e)); } }; template< - class TEnum, - class TBase = typename std::underlying_type::type, - class Op = op_flag_convert_none> + class T_enum, + class T_base = typename std::underlying_type::type, + class T_op = op_flag_convert_none> struct flags { public: - TBase value; + using enum_type = T_enum; + using base_type = T_base; + using op_type = T_op; public: - inline bool is_set(TEnum e) const - { return static_cast(value & Op::to_base(e)); } + base_type value; - inline void set(TEnum e) - { value = static_cast(value | Op::to_base(e)); } + public: + inline bool is_set(enum_type e) const + { return static_cast(value & op_type::to_base(e)); } + + inline void set(enum_type e) + { value = static_cast(value | op_type::to_base(e)); } - inline void clear(TEnum e) - { value = static_cast(value & ~Op::to_base(e)); } + inline void clear(enum_type e) + { value = static_cast(value & ~op_type::to_base(e)); } inline void reset() { value = 0; } public: - TBase operator()() const + base_type operator()() const { return value; } - operator bool() const + operator base_type() const + { return static_cast(value); } + + explicit operator bool() const { return static_cast(value); } - bool operator[](TEnum e) const + bool operator[](enum_type e) const { return is_set(e); } public: @@ -57,19 +65,19 @@ namespace utl value(0) { } - explicit flags(TBase v) : + explicit flags(base_type v) : value(v) { } - flags(TEnum e) : - value(Op::to_base(e)) + flags(enum_type e) : + value(T_op::to_base(e)) { } flags(const flags& other) : value(other.value) { } - flags(std::initializer_list list) : + flags(std::initializer_list list) : flags() { for (auto& e : list) @@ -85,19 +93,19 @@ namespace utl static inline const flags& all() { - static const flags value(std::numeric_limits::max()); + static const flags value(std::numeric_limits::max()); return value; } }; template< - class TEnum, - class TBase = typename std::underlying_type::type> - using shifted_flags = flags>; + class T_enum, + class T_base = typename std::underlying_type::type> + using shifted_flags = flags>; template< - class TEnum, - class TBase = typename std::underlying_type::type> - using simple_flags = flags>; + class T_enum, + class T_base = typename std::underlying_type::type> + using simple_flags = flags>; } \ No newline at end of file