Fixed watchers that could be added twice to the same definition node

This commit is contained in:
Michaël Lemaire 2015-09-21 23:10:43 +02:00
parent c51d6d2d35
commit 63eb7b53eb
5 changed files with 51 additions and 1 deletions

View file

@ -181,6 +181,18 @@ void DefinitionNode::addWatcher(DefinitionWatcher *watcher, bool init_diff)
} }
} }
int DefinitionNode::getWatcherCount() const
{
if (root && root->diffs)
{
return root->diffs->getWatcherCount(this);
}
else
{
return 0;
}
}
void DefinitionNode::save(PackStream* stream) const void DefinitionNode::save(PackStream* stream) const
{ {
int children_count = (int)children.size(); int children_count = (int)children.size();

View file

@ -77,6 +77,11 @@ public:
*/ */
void addWatcher(DefinitionWatcher *watcher, bool init_diff=false); void addWatcher(DefinitionWatcher *watcher, bool init_diff=false);
/**
* Get the current number of watchers.
*/
int getWatcherCount() const;
protected: protected:
void addChild(DefinitionNode* child); void addChild(DefinitionNode* child);
void removeChild(DefinitionNode* child); void removeChild(DefinitionNode* child);

View file

@ -21,7 +21,15 @@ DiffManager::~DiffManager()
void DiffManager::addWatcher(const DefinitionNode *node, DefinitionWatcher *watcher) void DiffManager::addWatcher(const DefinitionNode *node, DefinitionWatcher *watcher)
{ {
if (std::find(watchers[node].begin(), watchers[node].end(), watcher) == watchers[node].end())
{
watchers[node].push_back(watcher); watchers[node].push_back(watcher);
}
}
int DiffManager::getWatcherCount(const DefinitionNode *node)
{
return watchers[node].size();
} }
void DiffManager::addDiff(DefinitionNode *node, const DefinitionDiff *diff) void DiffManager::addDiff(DefinitionNode *node, const DefinitionDiff *diff)

View file

@ -27,6 +27,11 @@ public:
*/ */
void addWatcher(const DefinitionNode *node, DefinitionWatcher *watcher); void addWatcher(const DefinitionNode *node, DefinitionWatcher *watcher);
/**
* Get the number of watchers registered for a given node.
*/
int getWatcherCount(const DefinitionNode *node);
/** /**
* Add a new diff of a node to the change flow. * Add a new diff of a node to the change flow.
*/ */

View file

@ -3,6 +3,7 @@
#include "DefinitionNode.h" #include "DefinitionNode.h"
#include "FloatNode.h" #include "FloatNode.h"
#include "PackStream.h" #include "PackStream.h"
#include "DefinitionWatcher.h"
TEST(DefinitionNode, toString) TEST(DefinitionNode, toString)
{ {
@ -26,6 +27,25 @@ TEST(DefinitionNode, getPath)
EXPECT_EQ("/branch/leaf", leaf.getPath()); EXPECT_EQ("/branch/leaf", leaf.getPath());
} }
class FakeWatcher: public DefinitionWatcher
{
virtual void nodeChanged(const DefinitionNode *, const DefinitionDiff *) override
{
}
};
TEST(DefinitionNode, addWatcher)
{
DefinitionNode root(NULL, "root");
FakeWatcher watcher;
EXPECT_EQ(0, root.getWatcherCount());
root.addWatcher(&watcher);
EXPECT_EQ(1, root.getWatcherCount());
root.addWatcher(&watcher);
EXPECT_EQ(1, root.getWatcherCount());
}
TEST(DefinitionNode, findByPath) TEST(DefinitionNode, findByPath)
{ {
DefinitionNode root(NULL, "root"); DefinitionNode root(NULL, "root");