2018-01-28 03:29:42 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <type_traits>
|
|
|
|
|
|
|
|
namespace chatterino {
|
|
|
|
|
2018-01-28 15:28:02 +01:00
|
|
|
// = std::enable_if<std::is_enum<T>::value>::type
|
|
|
|
|
|
|
|
template <typename T, typename Q = typename std::underlying_type<T>::type>
|
2018-01-28 03:29:42 +01:00
|
|
|
class FlagsEnum
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
FlagsEnum()
|
2018-03-31 11:23:07 +02:00
|
|
|
: value(static_cast<T>(0))
|
2018-01-28 03:29:42 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
FlagsEnum(T _value)
|
|
|
|
: value(_value)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
inline T operator~() const
|
|
|
|
{
|
|
|
|
return (T) ~(Q)this->value;
|
|
|
|
}
|
|
|
|
inline T operator|(Q a) const
|
|
|
|
{
|
|
|
|
return (T)((Q)a | (Q)this->value);
|
|
|
|
}
|
|
|
|
inline T operator&(Q a) const
|
|
|
|
{
|
|
|
|
return (T)((Q)a & (Q)this->value);
|
|
|
|
}
|
|
|
|
inline T operator^(Q a) const
|
|
|
|
{
|
|
|
|
return (T)((Q)a ^ (Q)this->value);
|
|
|
|
}
|
|
|
|
inline T &operator|=(const Q &a)
|
|
|
|
{
|
2018-01-28 14:45:39 +01:00
|
|
|
return (T &)((Q &)this->value |= (Q)a);
|
2018-01-28 03:29:42 +01:00
|
|
|
}
|
|
|
|
inline T &operator&=(const Q &a)
|
|
|
|
{
|
2018-01-28 14:45:39 +01:00
|
|
|
return (T &)((Q &)this->value &= (Q)a);
|
2018-01-28 03:29:42 +01:00
|
|
|
}
|
|
|
|
inline T &operator^=(const Q &a)
|
|
|
|
{
|
2018-01-28 14:45:39 +01:00
|
|
|
return (T &)((Q &)this->value ^= (Q)a);
|
2018-01-28 03:29:42 +01:00
|
|
|
}
|
|
|
|
|
2018-03-31 11:23:07 +02:00
|
|
|
void EnableFlag(T flag)
|
|
|
|
{
|
|
|
|
reinterpret_cast<Q &>(this->value) |= static_cast<Q>(flag);
|
|
|
|
}
|
|
|
|
|
2018-08-07 01:35:24 +02:00
|
|
|
void set(T flag)
|
|
|
|
{
|
|
|
|
reinterpret_cast<Q &>(this->value) |= static_cast<Q>(flag);
|
|
|
|
}
|
|
|
|
|
|
|
|
void unset(T flag)
|
|
|
|
{
|
|
|
|
reinterpret_cast<Q &>(this->value) &= ~static_cast<Q>(flag);
|
|
|
|
}
|
|
|
|
|
|
|
|
void set(T flag, bool value)
|
|
|
|
{
|
|
|
|
if (value)
|
|
|
|
this->set(flag);
|
|
|
|
else
|
|
|
|
this->unset(flag);
|
|
|
|
}
|
|
|
|
|
2018-03-31 11:23:07 +02:00
|
|
|
bool HasFlag(Q flag) const
|
|
|
|
{
|
|
|
|
return (this->value & flag) == flag;
|
|
|
|
}
|
|
|
|
|
2018-01-28 03:29:42 +01:00
|
|
|
T value;
|
|
|
|
};
|
2018-03-31 11:23:07 +02:00
|
|
|
|
2018-01-28 03:29:42 +01:00
|
|
|
} // namespace chatterino
|