diff --git a/src/definition/DefinitionNode.cpp b/src/definition/DefinitionNode.cpp index de95336..e994903 100644 --- a/src/definition/DefinitionNode.cpp +++ b/src/definition/DefinitionNode.cpp @@ -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 { int children_count = (int)children.size(); diff --git a/src/definition/DefinitionNode.h b/src/definition/DefinitionNode.h index 5fb3307..514631f 100644 --- a/src/definition/DefinitionNode.h +++ b/src/definition/DefinitionNode.h @@ -77,6 +77,11 @@ public: */ void addWatcher(DefinitionWatcher *watcher, bool init_diff=false); + /** + * Get the current number of watchers. + */ + int getWatcherCount() const; + protected: void addChild(DefinitionNode* child); void removeChild(DefinitionNode* child); diff --git a/src/definition/DiffManager.cpp b/src/definition/DiffManager.cpp index 9cf1c0c..af98faf 100644 --- a/src/definition/DiffManager.cpp +++ b/src/definition/DiffManager.cpp @@ -21,7 +21,15 @@ DiffManager::~DiffManager() void DiffManager::addWatcher(const DefinitionNode *node, DefinitionWatcher *watcher) { - watchers[node].push_back(watcher); + if (std::find(watchers[node].begin(), watchers[node].end(), watcher) == watchers[node].end()) + { + watchers[node].push_back(watcher); + } +} + +int DiffManager::getWatcherCount(const DefinitionNode *node) +{ + return watchers[node].size(); } void DiffManager::addDiff(DefinitionNode *node, const DefinitionDiff *diff) diff --git a/src/definition/DiffManager.h b/src/definition/DiffManager.h index f0998ce..bf8c0ab 100644 --- a/src/definition/DiffManager.h +++ b/src/definition/DiffManager.h @@ -27,6 +27,11 @@ public: */ 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. */ diff --git a/src/tests/DefinitionNode_Test.cpp b/src/tests/DefinitionNode_Test.cpp index a4e08fe..174c2db 100644 --- a/src/tests/DefinitionNode_Test.cpp +++ b/src/tests/DefinitionNode_Test.cpp @@ -3,6 +3,7 @@ #include "DefinitionNode.h" #include "FloatNode.h" #include "PackStream.h" +#include "DefinitionWatcher.h" TEST(DefinitionNode, toString) { @@ -26,6 +27,25 @@ TEST(DefinitionNode, 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) { DefinitionNode root(NULL, "root");