From e46347d4409eff982fdc3b1ccfaaa99f1acddfbb Mon Sep 17 00:00:00 2001
From: Joseph Mirabel <jmirabel@laas.fr>
Date: Tue, 22 Jul 2014 10:24:05 +0200
Subject: [PATCH] Add static vector to retrieve GraphComponents

---
 include/hpp/manipulation/graph/fwd.hh   |  1 +
 include/hpp/manipulation/graph/graph.hh | 31 +++++++++++++++++++++++++
 src/graph/edge.cc                       |  1 +
 src/graph/graph.cc                      |  3 +++
 src/graph/node-selector.cc              |  1 +
 src/graph/node.cc                       |  1 +
 6 files changed, 38 insertions(+)

diff --git a/include/hpp/manipulation/graph/fwd.hh b/include/hpp/manipulation/graph/fwd.hh
index 0dd47547..719e7d3b 100644
--- a/include/hpp/manipulation/graph/fwd.hh
+++ b/include/hpp/manipulation/graph/fwd.hh
@@ -27,6 +27,7 @@ namespace hpp {
       HPP_PREDEF_CLASS (Node);
       HPP_PREDEF_CLASS (Edge);
       HPP_PREDEF_CLASS (NodeSelector);
+      HPP_PREDEF_CLASS (GraphComponent);
       typedef boost::shared_ptr < Graph > GraphPtr_t;
       typedef boost::shared_ptr < Node > NodePtr_t;
       typedef boost::shared_ptr < Edge > EdgePtr_t;
diff --git a/include/hpp/manipulation/graph/graph.hh b/include/hpp/manipulation/graph/graph.hh
index 047d281d..ef6abc44 100644
--- a/include/hpp/manipulation/graph/graph.hh
+++ b/include/hpp/manipulation/graph/graph.hh
@@ -45,8 +45,39 @@ namespace hpp {
             name_ = name;
           }
 
+          /// Keep track of the create components in order to retrieve them
+          /// easily.
+          static std::vector < GraphComponentWkPtr_t > components;
+
+          /// Get the component by its ID. The validity of the GraphComponent
+          /// is not checked.
+          static GraphComponentWkPtr_t get(int id)
+          {
+            HPP_ASSERT (id >= 0 && id < (int)components.size());
+            return components[id];
+          };
+
+          /// Return the component id.
+          int id () const
+          {
+            return id_;
+          }
+
+        protected:
+          void init (const GraphComponentWkPtr_t& weak)
+          {
+            wkPtr_ = weak;
+            components.push_back (wkPtr_);
+            id_ = components.size();
+          }
+
+          GraphComponent() : id_(-1)
+          {}
+
         private:
           std::string name_;
+          GraphComponentWkPtr_t wkPtr_;
+          int id_;
       };
 
 
diff --git a/src/graph/edge.cc b/src/graph/edge.cc
index eb31d71d..2f0e8fb3 100644
--- a/src/graph/edge.cc
+++ b/src/graph/edge.cc
@@ -31,6 +31,7 @@ namespace hpp {
       void Edge::init (const EdgeWkPtr_t& weak, const NodeWkPtr_t& from,
           const NodeWkPtr_t& to, const ConstraintPtr_t& constraints)
       {
+        GraphComponent::init (weak);
         wkPtr_ = weak;
         from_ = from;
         to_ = to;
diff --git a/src/graph/graph.cc b/src/graph/graph.cc
index 1ac73fc2..e56b582e 100644
--- a/src/graph/graph.cc
+++ b/src/graph/graph.cc
@@ -22,6 +22,8 @@
 namespace hpp {
   namespace manipulation {
     namespace graph {
+      std::vector < GraphComponentWkPtr_t > GraphComponent::components = std::vector < GraphComponentWkPtr_t >();
+
       GraphPtr_t Graph::create(RobotPtr_t robot)
       {
         Graph* ptr = new Graph;
@@ -32,6 +34,7 @@ namespace hpp {
 
       void Graph::init (const GraphWkPtr_t& weak, RobotPtr_t robot)
       {
+        GraphComponent::init (weak);
         robot_ = robot;
         wkPtr_ = weak;
       }
diff --git a/src/graph/node-selector.cc b/src/graph/node-selector.cc
index 17cff49a..be1efc01 100644
--- a/src/graph/node-selector.cc
+++ b/src/graph/node-selector.cc
@@ -31,6 +31,7 @@ namespace hpp {
 
       void NodeSelector::init (const NodeSelectorPtr_t& weak)
       {
+        GraphComponent::init (weak);
         wkPtr_ = weak;
       }
 
diff --git a/src/graph/node.cc b/src/graph/node.cc
index ad0ebd53..2a9bd205 100644
--- a/src/graph/node.cc
+++ b/src/graph/node.cc
@@ -37,6 +37,7 @@ namespace hpp {
 
       void Node::init (const NodeWkPtr_t& weak)
       {
+        GraphComponent::init (weak);
         wkPtr_ = weak;
       }
 
-- 
GitLab