refactored EnumFlags
This commit is contained in:
+14
-11
@@ -104,21 +104,24 @@ void Channel::addOrReplaceTimeout(MessagePtr message)
|
||||
break;
|
||||
}
|
||||
|
||||
if (s->flags.HasFlag(Message::Untimeout) &&
|
||||
if (s->flags.has(MessageFlag::Untimeout) &&
|
||||
s->timeoutUser == message->timeoutUser) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (s->flags.HasFlag(Message::Timeout) &&
|
||||
s->timeoutUser == message->timeoutUser) {
|
||||
if (message->flags.HasFlag(Message::PubSub) &&
|
||||
!s->flags.HasFlag(Message::PubSub)) {
|
||||
if (s->flags.has(MessageFlag::Timeout) &&
|
||||
s->timeoutUser == message->timeoutUser) //
|
||||
{
|
||||
if (message->flags.has(MessageFlag::PubSub) &&
|
||||
!s->flags.has(MessageFlag::PubSub)) //
|
||||
{
|
||||
this->replaceMessage(s, message);
|
||||
addMessage = false;
|
||||
break;
|
||||
}
|
||||
if (!message->flags.HasFlag(Message::PubSub) &&
|
||||
s->flags.HasFlag(Message::PubSub)) {
|
||||
if (!message->flags.has(MessageFlag::PubSub) &&
|
||||
s->flags.has(MessageFlag::PubSub)) //
|
||||
{
|
||||
addMessage = false;
|
||||
break;
|
||||
}
|
||||
@@ -142,10 +145,10 @@ void Channel::addOrReplaceTimeout(MessagePtr message)
|
||||
// disable the messages from the user
|
||||
for (int i = 0; i < snapshotLength; i++) {
|
||||
auto &s = snapshot[i];
|
||||
if ((s->flags & (Message::Timeout | Message::Untimeout)) == 0 &&
|
||||
if (s->flags.hasNone({MessageFlag::Timeout, MessageFlag::Untimeout}) &&
|
||||
s->loginName == message->timeoutUser) {
|
||||
// FOURTF: disabled for now
|
||||
// s->flags.EnableFlag(Message::Disabled);
|
||||
// s->flags.EnableFlag(MessageFlag::Disabled);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -163,12 +166,12 @@ void Channel::disableAllMessages()
|
||||
int snapshotLength = snapshot.getLength();
|
||||
for (int i = 0; i < snapshotLength; i++) {
|
||||
auto &s = snapshot[i];
|
||||
if (s->flags & Message::System || s->flags & Message::Timeout) {
|
||||
if (s->flags.hasAny({MessageFlag::System, MessageFlag::Timeout})) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// FOURTF: disabled for now
|
||||
// s->flags.EnableFlag(Message::Disabled);
|
||||
// s->flags.EnableFlag(MessageFlag::Disabled);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+48
-32
@@ -15,43 +15,26 @@ public:
|
||||
{
|
||||
}
|
||||
|
||||
FlagsEnum(T _value)
|
||||
: value(_value)
|
||||
FlagsEnum(T value)
|
||||
: value(value)
|
||||
{
|
||||
}
|
||||
|
||||
inline T operator~() const
|
||||
FlagsEnum(std::initializer_list<T> flags)
|
||||
{
|
||||
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)
|
||||
{
|
||||
return (T &)((Q &)this->value |= (Q)a);
|
||||
}
|
||||
inline T &operator&=(const Q &a)
|
||||
{
|
||||
return (T &)((Q &)this->value &= (Q)a);
|
||||
}
|
||||
inline T &operator^=(const Q &a)
|
||||
{
|
||||
return (T &)((Q &)this->value ^= (Q)a);
|
||||
for (auto flag : flags) {
|
||||
this->set(flag);
|
||||
}
|
||||
}
|
||||
|
||||
void EnableFlag(T flag)
|
||||
bool operator==(const FlagsEnum<T> &other)
|
||||
{
|
||||
reinterpret_cast<Q &>(this->value) |= static_cast<Q>(flag);
|
||||
return this->value == other.value;
|
||||
}
|
||||
|
||||
bool operator!=(const FlagsEnum &other)
|
||||
{
|
||||
return this->value != other.value;
|
||||
}
|
||||
|
||||
void set(T flag)
|
||||
@@ -72,11 +55,44 @@ public:
|
||||
this->unset(flag);
|
||||
}
|
||||
|
||||
bool HasFlag(Q flag) const
|
||||
bool has(T flag) const
|
||||
{
|
||||
return (this->value & flag) == flag;
|
||||
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
|
||||
{
|
||||
return !this->hasAny(flags);
|
||||
}
|
||||
|
||||
private:
|
||||
T value;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user