From bee9cb8824c38edebdc8470c0948032796489f33 Mon Sep 17 00:00:00 2001 From: Joseph Mirabel <jmirabel@laas.fr> Date: Tue, 22 Jul 2014 15:10:55 +0200 Subject: [PATCH] Add operator<< for all graph::GraphComponents --- include/hpp/manipulation/graph/edge.hh | 3 +++ include/hpp/manipulation/graph/graph.hh | 16 +++++++++++++++- include/hpp/manipulation/graph/node-selector.hh | 3 +++ include/hpp/manipulation/graph/node.hh | 3 +++ src/graph/edge.cc | 8 ++++++++ src/graph/graph.cc | 16 ++++++++++++++++ src/graph/node-selector.cc | 9 +++++++++ src/graph/node.cc | 9 +++++++++ 8 files changed, 66 insertions(+), 1 deletion(-) diff --git a/include/hpp/manipulation/graph/edge.hh b/include/hpp/manipulation/graph/edge.hh index f6d9ab22..57605c59 100644 --- a/include/hpp/manipulation/graph/edge.hh +++ b/include/hpp/manipulation/graph/edge.hh @@ -47,6 +47,9 @@ namespace hpp { /// \param config Configuration that will initialize the projector. ConfigProjectorPtr_t pathProjector(const Configuration_t config); + /// Print the object in a stream. + std::ostream& print (std::ostream& os) const; + protected: /// Initialization of the object. void init (const EdgeWkPtr_t& weak, const NodeWkPtr_t& from, diff --git a/include/hpp/manipulation/graph/graph.hh b/include/hpp/manipulation/graph/graph.hh index 607893d9..b38cd3e8 100644 --- a/include/hpp/manipulation/graph/graph.hh +++ b/include/hpp/manipulation/graph/graph.hh @@ -18,6 +18,7 @@ # define HPP_MANIPULATION_GRAPH_GRAPH_HH # include <string> +# include <ostream> # include <hpp/util/assertion.hh> # include "hpp/manipulation/robot.hh" @@ -30,7 +31,7 @@ namespace hpp { namespace manipulation { namespace graph { /// Define common methods of the graph components. - class HPP_MANIPULATION_LOCAL GraphComponent + class HPP_MANIPULATION_DLLAPI GraphComponent { public: /// Get the component name. @@ -59,6 +60,12 @@ namespace hpp { return id_; } + /// Print the object in a stream. + virtual std::ostream& print (std::ostream& os) const + { + return os; + } + protected: void init (const GraphComponentWkPtr_t& weak) { @@ -109,6 +116,9 @@ namespace hpp { /// NULL pointer if not found. NodeSelectorPtr_t getNodeSelectorByName (const std::string& name); + /// Print the object in a stream. + std::ostream& print (std::ostream& os) const; + protected: /// Initialization of the object. void init (const GraphWkPtr_t& weak, RobotPtr_t robot); @@ -133,6 +143,10 @@ namespace hpp { }; // Class Graph } // namespace graph } // namespace manipulation + + HPP_MANIPULATION_DLLAPI std::ostream& operator<< (std::ostream& os, + const hpp::manipulation::graph::GraphComponent& graphComp); + } // namespace hpp #endif // HPP_MANIPULATION_GRAPH_GRAPH_HH diff --git a/include/hpp/manipulation/graph/node-selector.hh b/include/hpp/manipulation/graph/node-selector.hh index dc6834a9..e042e4a3 100644 --- a/include/hpp/manipulation/graph/node-selector.hh +++ b/include/hpp/manipulation/graph/node-selector.hh @@ -45,6 +45,9 @@ namespace hpp { /// Select randomly an outgoing edge of the given node. virtual EdgePtr_t chooseEdge(const NodePtr_t& node); + /// Print the object in a stream. + std::ostream& print (std::ostream& os) const; + protected: /// Initialization of the object. void init (const NodeSelectorPtr_t& weak); diff --git a/include/hpp/manipulation/graph/node.hh b/include/hpp/manipulation/graph/node.hh index 9766a3c3..d7c073f8 100644 --- a/include/hpp/manipulation/graph/node.hh +++ b/include/hpp/manipulation/graph/node.hh @@ -80,6 +80,9 @@ namespace hpp { return neighbors_; } + /// Print the object in a stream. + std::ostream& print (std::ostream& os) const; + protected: /// Initialize the object. void init (const NodeWkPtr_t& self); diff --git a/src/graph/edge.cc b/src/graph/edge.cc index 2f0e8fb3..bb20a015 100644 --- a/src/graph/edge.cc +++ b/src/graph/edge.cc @@ -37,6 +37,14 @@ namespace hpp { to_ = to; constraints_ = constraints; } + + std::ostream& Edge::print (std::ostream& os) const + { + os << " | | |__ " << name () << " --> " + << to_.lock ()->name (); + return os; + } + } // namespace graph } // namespace manipulation } // namespace hpp diff --git a/src/graph/graph.cc b/src/graph/graph.cc index 094a392f..786a35c5 100644 --- a/src/graph/graph.cc +++ b/src/graph/graph.cc @@ -73,6 +73,22 @@ namespace hpp { } return NodeSelectorPtr_t(); } + + std::ostream& Graph::print (std::ostream& os) const + { + os << name () << std::endl; + for (NodeSelectors_t::const_iterator it = nodeSelectors_.begin(); + it != nodeSelectors_.end(); it++) + os << *(*it); + return os; + } } // namespace graph } // namespace manipulation + + std::ostream& operator<< (std::ostream& os, + const hpp::manipulation::graph::GraphComponent& graphComp) + { + return graphComp.print (os); + } + } // namespace hpp diff --git a/src/graph/node-selector.cc b/src/graph/node-selector.cc index 55b2a277..dc647502 100644 --- a/src/graph/node-selector.cc +++ b/src/graph/node-selector.cc @@ -66,6 +66,15 @@ namespace hpp { size_t n = rand() % neighbors.size(); return neighbors[n]; } + + std::ostream& NodeSelector::print (std::ostream& os) const + { + os << " |__ " << name() << std::endl; + for (Nodes_t::const_iterator it = orderedStates_.begin(); + orderedStates_.end() != it; it++) + os << *(*it); + return os; + } } // namespace graph } // namespace manipulation } // namespace hpp diff --git a/src/graph/node.cc b/src/graph/node.cc index 2a9bd205..01fe00ed 100644 --- a/src/graph/node.cc +++ b/src/graph/node.cc @@ -62,6 +62,15 @@ namespace hpp { Configuration_t cfg = config; return constraints_->apply(cfg) && ( cfg == config ); } + + std::ostream& Node::print (std::ostream& os) const + { + os << " | |_ " << name() << std::endl; + for (Edges_t::const_iterator it = neighbors_.begin(); + it != neighbors_.end(); it++) + os << *(*it); + return os; + } } // namespace graph } // namespace manipulation } // namespace hpp -- GitLab