Skip to content

Commit

Permalink
Refactor|libdeng2: Added/applied de::applyFlagOperation template
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Apr 30, 2013
1 parent fe94e76 commit 129de82
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 17 deletions.
2 changes: 1 addition & 1 deletion doomsday/libdeng2/include/de/core/system.h
Expand Up @@ -50,7 +50,7 @@ class DENG2_PUBLIC System : DENG2_OBSERVES(Clock, TimeChange)
public:
System(Flags const &behavior = DefaultBehavior);

void setBehavior(Flags const &behavior);
void setBehavior(Flags const &behavior, FlagOp operation = SetFlags);

Flags behavior() const;

Expand Down
9 changes: 9 additions & 0 deletions doomsday/libdeng2/include/de/libdeng2.h
Expand Up @@ -392,6 +392,15 @@ enum FlagOp {
ReplaceFlags = 2 ///< Specified flags become the new set of flags, replacing all previous flags.
};

template <typename FlagsType>
void applyFlagOperation(FlagsType &flags, FlagsType const &newFlags, FlagOp operation) {
switch(operation) {
case SetFlags: flags |= newFlags; break;
case UnsetFlags: flags &= ~newFlags; break;
case ReplaceFlags: flags = newFlags; break;
}
}

/**
* All serialization in all contexts use a common protocol version number.
* Whenever anything changes in serialization, the protocol version needs to be
Expand Down
6 changes: 3 additions & 3 deletions doomsday/libdeng2/include/de/widgets/widget.h
Expand Up @@ -77,8 +77,8 @@ class DENG2_PUBLIC Widget

void show(bool doShow = true);
inline void hide() { show(false); }
void enable(bool yes = true) { setBehavior(Disabled, !yes); }
void disable(bool yes = true) { setBehavior(Disabled, yes); }
void enable(bool yes = true) { setBehavior(Disabled, yes? UnsetFlags : SetFlags); }
void disable(bool yes = true) { setBehavior(Disabled, yes? SetFlags : UnsetFlags); }

bool isHidden() const;
inline bool isEnabled() const { return !behavior().testFlag(Disabled); }
Expand All @@ -90,7 +90,7 @@ class DENG2_PUBLIC Widget
* @param behavior Flags.
* @param set @c true to set, @c false to clear.
*/
void setBehavior(Behaviors behavior, bool set = true);
void setBehavior(Behaviors behavior, FlagOp operation = SetFlags);

Behaviors behavior() const;

Expand Down
4 changes: 2 additions & 2 deletions doomsday/libdeng2/src/core/system.cpp
Expand Up @@ -33,9 +33,9 @@ System::System(Flags const &behavior) : d(new Instance(this))
d->behavior = behavior;
}

void System::setBehavior(Flags const &behavior)
void System::setBehavior(Flags const &behavior, FlagOp operation)
{
d->behavior = behavior;
applyFlagOperation(d->behavior, behavior, operation);
}

System::Flags System::behavior() const
Expand Down
13 changes: 3 additions & 10 deletions doomsday/libdeng2/src/widgets/widget.cpp
Expand Up @@ -140,19 +140,12 @@ bool Widget::isHidden() const

void Widget::show(bool doShow)
{
setBehavior(Hidden, !doShow);
setBehavior(Hidden, doShow? UnsetFlags : SetFlags);
}

void Widget::setBehavior(Behaviors behavior, bool set)
void Widget::setBehavior(Behaviors behavior, FlagOp operation)
{
if(set)
{
d->behavior |= behavior;
}
else
{
d->behavior &= ~behavior;
}
applyFlagOperation(d->behavior, behavior, operation);
}

Widget::Behaviors Widget::behavior() const
Expand Down
2 changes: 1 addition & 1 deletion doomsday/libshell/src/menuwidget.cpp
Expand Up @@ -115,7 +115,7 @@ MenuWidget::MenuWidget(Preset preset, String const &name)
switch(preset)
{
case Popup:
setBehavior(HandleEventsOnlyWhenFocused, true);
setBehavior(HandleEventsOnlyWhenFocused);
setClosable(true);
d->cycleCursor = true;
hide();
Expand Down

0 comments on commit 129de82

Please sign in to comment.