Pārlūkot izejas kodu

* refactored/improved utl::flags

master
bergmann pirms 6 gadiem
vecāks
revīzija
6019ea0296
1 mainītis faili ar 38 papildinājumiem un 30 dzēšanām
  1. +38
    -30
      include/cpputils/misc/flags.h

+ 38
- 30
include/cpputils/misc/flags.h Parādīt failu

@@ -6,50 +6,58 @@


namespace utl namespace utl
{ {
template<class TEnum, class TBase>
template<class T_enum, class T_base>
struct op_flag_convert_none struct op_flag_convert_none
{ {
static inline TBase to_base(const TEnum e)
{ return static_cast<TBase>(e); }
static inline T_base to_base(const T_enum e)
{ return static_cast<T_base>(e); }
}; };


template<class TEnum, class TBase>
template<class T_enum, class T_base>
struct op_flag_convert_shift struct op_flag_convert_shift
{ {
static inline TBase to_base(const TEnum e)
{ return static_cast<TBase>(1 << static_cast<int>(e)); }
static inline T_base to_base(const T_enum e)
{ return static_cast<T_base>(1 << static_cast<int>(e)); }
}; };


template< template<
class TEnum,
class TBase = typename std::underlying_type<TEnum>::type,
class Op = op_flag_convert_none<TEnum, TBase>>
class T_enum,
class T_base = typename std::underlying_type<T_enum>::type,
class T_op = op_flag_convert_none<T_enum, T_base>>
struct flags struct flags
{ {
public: public:
TBase value;
using enum_type = T_enum;
using base_type = T_base;
using op_type = T_op;


public: public:
inline bool is_set(TEnum e) const
{ return static_cast<bool>(value & Op::to_base(e)); }
base_type value;


inline void set(TEnum e)
{ value = static_cast<TBase>(value | Op::to_base(e)); }
public:
inline bool is_set(enum_type e) const
{ return static_cast<bool>(value & op_type::to_base(e)); }

inline void set(enum_type e)
{ value = static_cast<base_type>(value | op_type::to_base(e)); }


inline void clear(TEnum e)
{ value = static_cast<TBase>(value & ~Op::to_base(e)); }
inline void clear(enum_type e)
{ value = static_cast<base_type>(value & ~op_type::to_base(e)); }


inline void reset() inline void reset()
{ value = 0; } { value = 0; }


public: public:
TBase operator()() const
base_type operator()() const
{ return value; } { return value; }


operator bool() const
operator base_type() const
{ return static_cast<bool>(value); }

explicit operator bool() const
{ return static_cast<bool>(value); } { return static_cast<bool>(value); }


bool operator[](TEnum e) const
bool operator[](enum_type e) const
{ return is_set(e); } { return is_set(e); }


public: public:
@@ -57,19 +65,19 @@ namespace utl
value(0) value(0)
{ } { }


explicit flags(TBase v) :
explicit flags(base_type v) :
value(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) : flags(const flags& other) :
value(other.value) value(other.value)
{ } { }


flags(std::initializer_list<TEnum> list) :
flags(std::initializer_list<enum_type> list) :
flags() flags()
{ {
for (auto& e : list) for (auto& e : list)
@@ -85,19 +93,19 @@ namespace utl


static inline const flags& all() static inline const flags& all()
{ {
static const flags value(std::numeric_limits<TBase>::max());
static const flags value(std::numeric_limits<base_type>::max());
return value; return value;
} }
}; };


template< template<
class TEnum,
class TBase = typename std::underlying_type<TEnum>::type>
using shifted_flags = flags<TEnum, TBase, op_flag_convert_shift<TEnum, TBase>>;
class T_enum,
class T_base = typename std::underlying_type<T_enum>::type>
using shifted_flags = flags<T_enum, T_base, op_flag_convert_shift<T_enum, T_base>>;


template< template<
class TEnum,
class TBase = typename std::underlying_type<TEnum>::type>
using simple_flags = flags<TEnum, TBase, op_flag_convert_none<TEnum, TBase>>;
class T_enum,
class T_base = typename std::underlying_type<T_enum>::type>
using simple_flags = flags<T_enum, T_base, op_flag_convert_none<T_enum, T_base>>;


} }

Notiek ielāde…
Atcelt
Saglabāt