1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
#include "KeyValueObserver.h"
#include "inamespace.h"
#include "ientity.h"
namespace entity {
KeyValueObserver::KeyValueObserver(EntityKeyValue& keyValue, INamespace* ns) :
_keyValue(keyValue),
_namespace(ns),
_observing(false)
{
assert(_namespace != NULL);
_keyValue.attach(*this);
}
KeyValueObserver::~KeyValueObserver()
{
_keyValue.detach(*this);
}
void KeyValueObserver::onKeyValueChanged(const std::string& newValue)
{
assert(_namespace != NULL);
if (_observing) {
// The key value was already observing as a NameObserver,
// so let's unregister first. It's likely that the new value
// will not point to a name anymore
_namespace->removeNameObserver(_observedValue, _keyValue);
_observing = false;
}
// Check if the new value is a name
if (!newValue.empty() && _namespace->nameExists(newValue))
{
// Gotcha, the new value is pointing to a name
_observedValue = newValue;
_observing = true;
// Register the KeyValue as nameobserver
_namespace->addNameObserver(_observedValue, _keyValue);
}
}
} // namespace entity
|