From 8eb39ab9548f6d18249c07f8ccf6f290f1819dd8 Mon Sep 17 00:00:00 2001
From: Joseph Mirabel <jmirabel@laas.fr>
Date: Mon, 8 Feb 2021 14:26:55 +0100
Subject: [PATCH] Clean RosPublish commands.

---
 src/ros_publish-python-module-py.cc |  7 +++-
 src/ros_publish.cpp                 | 61 ++---------------------------
 src/ros_publish.hh                  |  5 +--
 3 files changed, 11 insertions(+), 62 deletions(-)

diff --git a/src/ros_publish-python-module-py.cc b/src/ros_publish-python-module-py.cc
index 1d95259..a50e445 100644
--- a/src/ros_publish-python-module-py.cc
+++ b/src/ros_publish-python-module-py.cc
@@ -8,5 +8,10 @@ BOOST_PYTHON_MODULE(wrap)
 {
   bp::import("dynamic_graph");
 
-  dg::python::exposeEntity<dg::RosPublish, bp::bases<dg::Entity>, dg::python::AddCommands>() ;
+  dg::python::exposeEntity<dg::RosPublish, bp::bases<dg::Entity>, dg::python::AddCommands>()
+    .def("clear", &dg::RosPublish::clear, "Remove all signals writing data to a ROS topic")
+    .def("rm", &dg::RosPublish::rm, "Remove a signal writing data to a ROS topic",
+        bp::args("signal_name"))
+    .def("list", &dg::RosPublish::list, "List signals writing data to a ROS topic")
+    ;
 }
diff --git a/src/ros_publish.cpp b/src/ros_publish.cpp
index dd5a134..407d714 100644
--- a/src/ros_publish.cpp
+++ b/src/ros_publish.cpp
@@ -28,23 +28,6 @@ const double RosPublish::ROS_JOINT_STATE_PUBLISHER_RATE = 0.01;
 
 namespace command {
 namespace rosPublish {
-Clear::Clear(RosPublish& entity, const std::string& docstring)
-    : Command(entity, std::vector<Value::Type>(), docstring) {}
-
-Value Clear::doExecute() {
-  RosPublish& entity = static_cast<RosPublish&>(owner());
-
-  entity.clear();
-  return Value();
-}
-
-List::List(RosPublish& entity, const std::string& docstring)
-    : Command(entity, std::vector<Value::Type>(), docstring) {}
-
-Value List::doExecute() {
-  RosPublish& entity = static_cast<RosPublish&>(owner());
-  return Value(entity.list());
-}
 
 Add::Add(RosPublish& entity, const std::string& docstring)
     : Command(entity, boost::assign::list_of(Value::STRING)(Value::STRING)(Value::STRING), docstring) {}
@@ -88,16 +71,6 @@ Value Add::doExecute() {
   return Value();
 }
 
-Rm::Rm(RosPublish& entity, const std::string& docstring)
-    : Command(entity, boost::assign::list_of(Value::STRING), docstring) {}
-
-Value Rm::doExecute() {
-  RosPublish& entity = static_cast<RosPublish&>(owner());
-  std::vector<Value> values = getParameterValues();
-  const std::string& signal = values[0].value();
-  entity.rm(signal);
-  return Value();
-}
 }  // namespace rosPublish
 }  // end of namespace command.
 
@@ -143,29 +116,6 @@ RosPublish::RosPublish(const std::string& n)
       "    - topic:  the topic name in ROS.\n"
       "\n";
   addCommand("add", new command::rosPublish::Add(*this, docstring));
-  docstring =
-      "\n"
-      "  Remove a signal writing data to a ROS topic\n"
-      "\n"
-      "  Input:\n"
-      "    - name of the signal to remove (see method list for the list of "
-      "signals).\n"
-      "\n";
-  addCommand("rm", new command::rosPublish::Rm(*this, docstring));
-  docstring =
-      "\n"
-      "  Remove all signals writing data to a ROS topic\n"
-      "\n"
-      "  No input:\n"
-      "\n";
-  addCommand("clear", new command::rosPublish::Clear(*this, docstring));
-  docstring =
-      "\n"
-      "  List signals writing data to a ROS topic\n"
-      "\n"
-      "  No input:\n"
-      "\n";
-  addCommand("list", new command::rosPublish::List(*this, docstring));
 }
 
 RosPublish::~RosPublish() { aofs_.close(); }
@@ -187,13 +137,10 @@ void RosPublish::rm(const std::string& signal) {
   bindedSignal_.erase(signal);
 }
 
-std::string RosPublish::list() const {
-  std::string result("[");
-  for (std::map<std::string, bindedSignal_t>::const_iterator it = bindedSignal_.begin(); it != bindedSignal_.end();
-       it++) {
-    result += "'" + it->first + "',";
-  }
-  result += "]";
+std::vector<std::string> RosPublish::list() const {
+  std::vector<std::string> result(bindedSignal_.size());
+  std::transform(bindedSignal_.begin(), bindedSignal_.end(),
+      result.begin(), [](const auto& pair) { return pair.first; });
   return result;
 }
 
diff --git a/src/ros_publish.hh b/src/ros_publish.hh
index 9b73716..31b8cce 100644
--- a/src/ros_publish.hh
+++ b/src/ros_publish.hh
@@ -34,9 +34,6 @@ using ::dynamicgraph::command::Value;
   }
 
 ROS_PUBLISH_MAKE_COMMAND(Add);
-ROS_PUBLISH_MAKE_COMMAND(Clear);
-ROS_PUBLISH_MAKE_COMMAND(List);
-ROS_PUBLISH_MAKE_COMMAND(Rm);
 
 #undef ROS_PUBLISH_MAKE_COMMAND
 
@@ -62,7 +59,7 @@ class RosPublish : public dynamicgraph::Entity {
 
   void add(const std::string& signal, const std::string& topic);
   void rm(const std::string& signal);
-  std::string list() const;
+  std::vector<std::string> list() const;
   void clear();
 
   int& trigger(int&, int);
-- 
GitLab