Skip to content

Commit

Permalink
libcore: Added more audiences in Record and Variable
Browse files Browse the repository at this point in the history
Observing added/removed members and getting value changes with
old/new values.
  • Loading branch information
skyjake committed May 18, 2014
1 parent 6e959cd commit c74160d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 0 deletions.
4 changes: 4 additions & 0 deletions doomsday/libcore/include/de/data/record.h
Expand Up @@ -71,6 +71,10 @@ class DENG2_PUBLIC Record : public ISerializable, public LogEntry::Arg::Base,
IgnoreDoubleUnderscoreMembers
};

DENG2_DEFINE_AUDIENCE2(Addition, void recordMemberAdded(Record &record, Variable &member))

DENG2_DEFINE_AUDIENCE2(Removal, void recordMemberRemoved(Record &record, Variable &member))

DENG2_DEFINE_AUDIENCE2(Deletion, void recordBeingDeleted(Record &record))

public:
Expand Down
3 changes: 3 additions & 0 deletions doomsday/libcore/include/de/data/variable.h
Expand Up @@ -278,6 +278,9 @@ class DENG2_PUBLIC Variable : public ISerializable
*/
DENG2_DEFINE_AUDIENCE2(Change, void variableValueChanged(Variable &variable, Value const &newValue))

DENG2_DEFINE_AUDIENCE2(ChangeFrom, void variableValueChangedFrom(Variable &variable, Value const &oldValue,
Value const &newValue))

private:
DENG2_PRIVATE(d)
};
Expand Down
15 changes: 15 additions & 0 deletions doomsday/libcore/src/data/record.cpp
Expand Up @@ -172,9 +172,13 @@ DENG2_PIMPL(Record)
}

DENG2_PIMPL_AUDIENCE(Deletion)
DENG2_PIMPL_AUDIENCE(Addition)
DENG2_PIMPL_AUDIENCE(Removal)
};

DENG2_AUDIENCE_METHOD(Record, Deletion)
DENG2_AUDIENCE_METHOD(Record, Addition)
DENG2_AUDIENCE_METHOD(Record, Removal)

Record::Record() : d(new Instance(*this))
{}
Expand All @@ -188,7 +192,10 @@ Record::Record(Record const &other, CopyBehavior behavior)

Record::~Record()
{
// Notify before deleting members so that observers have full visibility
// to the record prior to deletion.
DENG2_FOR_AUDIENCE2(Deletion, i) i->recordBeingDeleted(*this);

clear();
}

Expand All @@ -198,6 +205,8 @@ void Record::clear()
{
DENG2_FOR_EACH(Members, i, d->members)
{
DENG2_FOR_AUDIENCE2(Removal, o) o->recordMemberRemoved(*this, **i);

i.value()->audienceForDeletion() -= this;
delete i.value();
}
Expand Down Expand Up @@ -277,13 +286,19 @@ Variable &Record::add(Variable *variable)
}
var->audienceForDeletion() += this;
d->members[variable->name()] = var.release();

DENG2_FOR_AUDIENCE2(Addition, i) i->recordMemberAdded(*this, *variable);

return *variable;
}

Variable *Record::remove(Variable &variable)
{
variable.audienceForDeletion() -= this;
d->members.remove(variable.name());

DENG2_FOR_AUDIENCE2(Removal, i) i->recordMemberRemoved(*this, variable);

return &variable;
}

Expand Down
3 changes: 3 additions & 0 deletions doomsday/libcore/src/data/variable.cpp
Expand Up @@ -59,10 +59,12 @@ DENG2_PIMPL_NOREF(Variable)

DENG2_PIMPL_AUDIENCE(Deletion)
DENG2_PIMPL_AUDIENCE(Change)
DENG2_PIMPL_AUDIENCE(ChangeFrom)
};

DENG2_AUDIENCE_METHOD(Variable, Deletion)
DENG2_AUDIENCE_METHOD(Variable, Change)
DENG2_AUDIENCE_METHOD(Variable, ChangeFrom)

Variable::Variable(String const &name, Value *initial, Flags const &m)
: d(new Instance)
Expand Down Expand Up @@ -131,6 +133,7 @@ Variable &Variable::set(Value *v)
if(notify)
{
DENG2_FOR_AUDIENCE2(Change, i) i->variableValueChanged(*this, *d->value);
DENG2_FOR_AUDIENCE2(ChangeFrom, i) i->variableValueChangedFrom(*this, *oldValue, *d->value);
}
}
return *this;
Expand Down

0 comments on commit c74160d

Please sign in to comment.