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
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-08-07 07:55:31 +02:00
|
|
|
FlagsEnum(T value)
|
|
|
|
: value(value)
|
2018-01-28 03:29:42 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-08-07 07:55:31 +02:00
|
|
|
FlagsEnum(std::initializer_list<T> flags)
|
2018-01-28 03:29:42 +01:00
|
|
|
{
|
2018-08-07 07:55:31 +02:00
|
|
|
for (auto flag : flags) {
|
|
|
|
this->set(flag);
|
|
|
|
}
|
2018-01-28 03:29:42 +01:00
|
|
|
}
|
2018-08-07 07:55:31 +02:00
|
|
|
|
|
|
|
bool operator==(const FlagsEnum<T> &other)
|
2018-01-28 03:29:42 +01:00
|
|
|
{
|
2018-08-07 07:55:31 +02:00
|
|
|
return this->value == other.value;
|
2018-01-28 03:29:42 +01:00
|
|
|
}
|
|
|
|
|
2018-08-07 07:55:31 +02:00
|
|
|
bool operator!=(const FlagsEnum &other)
|
2018-03-31 11:23:07 +02:00
|
|
|
{
|
2018-08-07 07:55:31 +02:00
|
|
|
return this->value != other.value;
|
2018-03-31 11:23:07 +02:00
|
|
|
}
|
|
|
|
|
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-08-07 07:55:31 +02:00
|
|
|
bool has(T flag) const
|
|
|
|
{
|
|
|
|
return static_cast<Q>(this->value) & static_cast<Q>(flag);
|
|
|
|
}
|
|
|
|
|
|
|
|
// bool hasAny(std::initializer_list<T> flags) const
|
|
|
|
//{
|
|
|
|
// for (auto flag : flags) {
|
|
|
|
// if (this->has(flag)) return true;
|
|
|
|
// }
|
|
|
|
// return false;
|
|
|
|
//}
|
|
|
|
|
|
|
|
bool hasAny(FlagsEnum flags) const
|
|
|
|
{
|
|
|
|
return static_cast<Q>(this->value) & static_cast<Q>(flags.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
// bool hasAll(std::initializer_list<T> flags) const
|
|
|
|
//{
|
|
|
|
// for (auto flag : flags) {
|
|
|
|
// if (!this->has(flag)) return false;
|
|
|
|
// }
|
|
|
|
// return true;
|
|
|
|
//}
|
|
|
|
|
|
|
|
bool hasAll(FlagsEnum<T> flags) const
|
|
|
|
{
|
|
|
|
return (static_cast<Q>(this->value) & static_cast<Q>(flags.value)) &&
|
|
|
|
static_cast<Q>(flags->value);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool hasNone(std::initializer_list<T> flags) const
|
2018-03-31 11:23:07 +02:00
|
|
|
{
|
2018-08-07 07:55:31 +02:00
|
|
|
return !this->hasAny(flags);
|
2018-03-31 11:23:07 +02:00
|
|
|
}
|
|
|
|
|
2018-08-07 07:55:31 +02:00
|
|
|
private:
|
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
|