Skip to content

Commit

Permalink
libdeng2|Refactor: Replaced Qt signals with de::Observers in Rules an…
Browse files Browse the repository at this point in the history
…d Clock

de::Observers has less overhead and does not require an additional
base class (QObject).
  • Loading branch information
skyjake committed Jan 30, 2013
1 parent 9be8446 commit 840bdea
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 107 deletions.
10 changes: 4 additions & 6 deletions doomsday/libdeng2/include/de/core/clock.h
Expand Up @@ -20,18 +20,19 @@
#ifndef LIBDENG2_CLOCK_H
#define LIBDENG2_CLOCK_H

#include <QObject>
#include "../Time"
#include "../Observers"

namespace de {

/**
* Time source.
* @ingroup core
*/
class Clock : public QObject
class Clock
{
Q_OBJECT
public:
DENG2_DEFINE_AUDIENCE(TimeChange, void timeChanged(Clock const &))

public:
Clock();
Expand All @@ -58,9 +59,6 @@ class Clock : public QObject
static void setAppClock(Clock *c);
static Clock &appClock();

signals:
void timeChanged();

private:
Time _startedAt;
Time _time;
Expand Down
4 changes: 1 addition & 3 deletions doomsday/libdeng2/include/de/widgets/constantrule.h
Expand Up @@ -32,10 +32,8 @@ namespace de {
*/
class DENG2_PUBLIC ConstantRule : public Rule
{
Q_OBJECT

public:
explicit ConstantRule(float constantValue);
ConstantRule(float constantValue);

/**
* Changes the value of the constant in the rule.
Expand Down
4 changes: 1 addition & 3 deletions doomsday/libdeng2/include/de/widgets/delegaterule.h
Expand Up @@ -36,8 +36,6 @@ namespace de {
*/
class DENG2_PUBLIC DelegateRule : public ConstantRule
{
Q_OBJECT

public:
/**
* Source of the delegate, responsible for setting the delegate's value.
Expand Down Expand Up @@ -65,7 +63,7 @@ class DENG2_PUBLIC DelegateRule : public ConstantRule
* @param source Source.
* @param delegateId Id passed to the source to identify this delegate.
*/
explicit DelegateRule(ISource &source, int delegateId = 0);
DelegateRule(ISource &source, int delegateId = 0);

/**
* Public destructor. The source rule/owner is expected to directly call
Expand Down
6 changes: 2 additions & 4 deletions doomsday/libdeng2/include/de/widgets/operatorrule.h
Expand Up @@ -31,8 +31,6 @@ namespace de {
*/
class DENG2_PUBLIC OperatorRule : public Rule
{
Q_OBJECT

public:
enum Operator {
Equals,
Expand All @@ -48,9 +46,9 @@ class DENG2_PUBLIC OperatorRule : public Rule
};

public:
explicit OperatorRule(Operator op, Rule const *unary);
OperatorRule(Operator op, Rule const *unary);

explicit OperatorRule(Operator op, Rule const *left, Rule const *right);
OperatorRule(Operator op, Rule const *left, Rule const *right);

protected:
~OperatorRule();
Expand Down
16 changes: 6 additions & 10 deletions doomsday/libdeng2/include/de/widgets/rectanglerule.h
Expand Up @@ -35,10 +35,8 @@ namespace de {
*
* @ingroup widgets
*/
class DENG2_PUBLIC RectangleRule : public Rule
class DENG2_PUBLIC RectangleRule : public Rule, DENG2_OBSERVES(Clock, TimeChange)
{
Q_OBJECT

public:
enum InputRule {
Left,
Expand All @@ -49,11 +47,11 @@ class DENG2_PUBLIC RectangleRule : public Rule
Height,
AnchorX,
AnchorY,
MAX_RULES
MAX_INPUT_RULES
};

public:
explicit RectangleRule();
RectangleRule();

/**
* Constructs a rectangle rule with individual rules defining the placement
Expand All @@ -64,9 +62,9 @@ class DENG2_PUBLIC RectangleRule : public Rule
* @param right Rule for the right coordinate.
* @param bottom Rule for the bottom coordinate.
*/
explicit RectangleRule(Rule const *left, Rule const *top, Rule const *right, Rule const *bottom);
RectangleRule(Rule const *left, Rule const *top, Rule const *right, Rule const *bottom);

explicit RectangleRule(RectangleRule const *rect);
RectangleRule(RectangleRule const *rect);

// Output rules.
Rule const *left() const;
Expand Down Expand Up @@ -117,12 +115,10 @@ class DENG2_PUBLIC RectangleRule : public Rule
*/
Rectanglei recti() const;

public slots:
void timeChanged();

protected:
~RectangleRule();
void update();
void timeChanged(Clock const &);

private:
struct Instance;
Expand Down
39 changes: 23 additions & 16 deletions doomsday/libdeng2/include/de/widgets/rule.h
Expand Up @@ -20,13 +20,15 @@
#ifndef LIBDENG2_RULE_H
#define LIBDENG2_RULE_H

#include <QObject>
#include <QSet>
#include "../libdeng2.h"
#include "../Counted"
#include "../Observers"

namespace de {

// Declared outside Rule because Rule itself implements the interface.
DENG2_DECLARE_AUDIENCE(RuleInvalidation, void ruleInvalidated())

/**
* Rules are used together to evaluate formulas dependent on other rules.
*
Expand All @@ -35,23 +37,28 @@ namespace de {
* - Every rule knows where its value comes from / how it's generated.
* - When the value changes, all dependent rules are notified and marked as invalid.
* - When a rule is invalid, its current value will be updated (i.e., validated).
* - Rules can be replaced dynamically with other rules, see Rule::replace().
* - Reference counting is used for lifetime management.
*
* @ingroup widgets
*/
class DENG2_PUBLIC Rule : public QObject, public Counted
class DENG2_PUBLIC Rule : public Counted, public DENG2_AUDIENCE_INTERFACE(RuleInvalidation)
{
Q_OBJECT
public:
DENG2_AUDIENCE(RuleInvalidation)

public:
explicit Rule(float initialValue = 0);
Rule(float initialValue = 0);

/**
* Determines the rule's current value. If it has been marked invalid,
* the value is updated first (see update()).
*/
float value() const;
float value() const;

/**
* Marks the rule invalid, causing all dependent rules to be invalid, too.
*/
void invalidate();

/**
* Updates the rule with a valid value. Derived classes must call
Expand All @@ -62,6 +69,11 @@ class DENG2_PUBLIC Rule : public QObject, public Counted
*/
virtual void update();

/**
* Determines if the rule's value is currently valid. A rule becomes
* invalid if any of its dependencies are invalidated, or invalidate() is
* called directly on the rule.
*/
bool isValid() const;

/**
Expand All @@ -77,8 +89,9 @@ class DENG2_PUBLIC Rule : public QObject, public Counted
*/
void independentOf(Rule const *dependency);

public:
/**
* Clearss the flag that determines whether there are any invalid rules.
* Clears the flag that determines whether there are any invalid rules.
* This could, for example, be called after drawing a frame.
*/
static void markRulesValid();
Expand All @@ -92,12 +105,6 @@ class DENG2_PUBLIC Rule : public QObject, public Counted
*/
static bool invalidRulesExist();

public slots:
/**
* Marks the rule invalid, causing all dependent rules to be invalid, too.
*/
void invalidate();

protected:
~Rule(); // Counted

Expand All @@ -110,8 +117,8 @@ public slots:

float cachedValue() const;

signals:
void valueInvalidated();
// Implements IRuleInvalidationObserver.
void ruleInvalidated();

private:
struct Instance;
Expand Down
9 changes: 3 additions & 6 deletions doomsday/libdeng2/include/de/widgets/scalarrule.h
Expand Up @@ -31,12 +31,10 @@ namespace de {
* Rule with a scalar value. The value is animated over time.
* @ingroup widgets
*/
class DENG2_PUBLIC ScalarRule : public Rule
class DENG2_PUBLIC ScalarRule : public Rule, DENG2_OBSERVES(Clock, TimeChange)
{
Q_OBJECT

public:
explicit ScalarRule(float initialValue);
ScalarRule(float initialValue);

void set(float target, TimeDelta transition = 0);

Expand All @@ -53,8 +51,7 @@ class DENG2_PUBLIC ScalarRule : public Rule
~ScalarRule();
void update();

protected slots:
void timeChanged();
void timeChanged(Clock const &);

private:
Animation _animation;
Expand Down
2 changes: 1 addition & 1 deletion doomsday/libdeng2/src/core/clock.cpp
Expand Up @@ -36,7 +36,7 @@ void Clock::setTime(Time const &currentTime)

if(changed)
{
emit timeChanged();
DENG2_FOR_AUDIENCE(TimeChange, i) i->timeChanged(*this);
}
}

Expand Down

0 comments on commit 840bdea

Please sign in to comment.