task_manager: check duplicated node 72/312072/2
authorInki Dae <inki.dae@samsung.com>
Tue, 4 Jun 2024 01:51:24 +0000 (10:51 +0900)
committerInki Dae <inki.dae@samsung.com>
Tue, 4 Jun 2024 01:55:03 +0000 (10:55 +0900)
Check duplicated node by introduing isNodeDuplicated function.

Change-Id: Id7eadd1daaa8d8c87ba91df5003c2cd004f09c60
Signed-off-by: Inki Dae <inki.dae@samsung.com>
services/task_manager/include/CallbackNode.h
services/task_manager/include/TaskManager.h
services/task_manager/include/TaskNode.h
services/task_manager/src/CallbackNode.cpp
services/task_manager/src/TaskManager.cpp
services/task_manager/src/TaskNode.cpp

index c343255ac02612e929cb7a52fb1c38a287f6c8e1..8834ce724a0dea67bf9d3e99998776fc911720c5 100644 (file)
@@ -42,6 +42,8 @@ protected:
        std::condition_variable _event;
        std::mutex _mutex;
 
+       bool isNodeDuplicated(const std::vector<std::shared_ptr<INode> > &target, const std::shared_ptr<INode> &newNode);
+
 public:
        CallbackNode() = default;
        virtual ~CallbackNode() = default;
index 09b3490a6573af0594c3a7e9dfd43053321d58f2..92308c2bddac7ad927b3cc9544cc9b95d935857b 100644 (file)
@@ -38,6 +38,7 @@ private:
 
        void threadCb(std::shared_ptr<INode> &node);
        void verifyGraph();
+       bool isNodeDuplicated(const std::vector<std::shared_ptr<INode> > &target, const std::shared_ptr<INode> &newNode);
 
 public:
        TaskManager() = default;
index a63b38906e74e6a4380758770447247af3cf70a4..8bc13d6f6fd8370db72d2cbf15848fdaa3e33405 100644 (file)
@@ -40,6 +40,8 @@ protected:
        std::condition_variable _event;
        std::mutex _mutex;
 
+       bool isNodeDuplicated(const std::vector<std::shared_ptr<INode> > &target, const std::shared_ptr<INode> &newNode);
+
 public:
        TaskNode() = default;
        virtual ~TaskNode() = default;
index ab9664f92cda0c538535916f4cc9e4b87198d765..6e8def5fc5e291635594a55f9aa1a7514b2131c1 100644 (file)
@@ -15,6 +15,7 @@
  */
 
 #include <cstring>
+#include <algorithm>
 #include "SingleoLog.h"
 #include "SingleoException.h"
 #include "CallbackNode.h"
@@ -47,8 +48,19 @@ void CallbackNode::setCb(const NodeCb &cb)
        _cb = cb;
 }
 
+bool CallbackNode::isNodeDuplicated(const vector<shared_ptr<INode> > &target, const shared_ptr<INode> &newNode)
+{
+       return find_if(target.begin(), target.end(),
+                                  [&newNode](const shared_ptr<INode> &node) { return node == newNode; }) != target.end();
+}
+
 void CallbackNode::addDependency(std::shared_ptr<INode> node)
 {
+       if (isNodeDuplicated(_dependencies, node)) {
+               SINGLEO_LOGE("A given node has already been registered.");
+               throw InvalidOperation("A given node has already been registered.");
+       }
+
        _dependencies.push_back(node);
 }
 
index 2a46402a92c00a1a08d2f256851033c35695f133..f3558df6c848c9de9ac3ae7f202d968604e25274 100644 (file)
@@ -17,6 +17,7 @@
 #include <cstring>
 #include <queue>
 #include <map>
+#include <algorithm>
 
 #include "SingleoException.h"
 #include "SingleoLog.h"
@@ -90,8 +91,19 @@ vector<shared_ptr<BaseDataType> > &TaskManager::getInputs()
        return _inputs;
 }
 
+bool TaskManager::isNodeDuplicated(const vector<shared_ptr<INode> > &target, const shared_ptr<INode> &newNode)
+{
+       return find_if(target.begin(), target.end(),
+                                  [&newNode](const shared_ptr<INode> &node) { return node == newNode; }) != target.end();
+}
+
 void TaskManager::addNode(std::shared_ptr<INode> node)
 {
+       if (isNodeDuplicated(_nodes, node)) {
+               SINGLEO_LOGE("A given node has already been registered.");
+               throw InvalidOperation("A given node has already been registered.");
+       }
+
        _nodes.push_back(node);
        node->configure();
 }
index d11dfc96961781039fef5d12fc71a9f83c6976f1..6ee98bef5de38cb10c1c4f3d69d8dd198210f0c0 100644 (file)
  * limitations under the License.
  */
 
+#include <algorithm>
+#include "SingleoLog.h"
+#include "SingleoException.h"
 #include "TaskNode.h"
 
 using namespace std;
+using namespace singleo::exception;
 
 namespace singleo
 {
@@ -38,8 +42,19 @@ shared_ptr<SharedBuffer> &TaskNode::getInputBuffer()
        return _inputBuffer;
 }
 
+bool TaskNode::isNodeDuplicated(const vector<shared_ptr<INode> > &target, const shared_ptr<INode> &newNode)
+{
+       return find_if(target.begin(), target.end(),
+                                  [&newNode](const shared_ptr<INode> &node) { return node == newNode; }) != target.end();
+}
+
 void TaskNode::addDependency(std::shared_ptr<INode> node)
 {
+       if (isNodeDuplicated(_dependencies, node)) {
+               SINGLEO_LOGE("A given node has already been registered.");
+               throw InvalidOperation("A given node has already been registered.");
+       }
+
        _dependencies.push_back(node);
 }