From 7d991eb38b3e74757ec418fbe62f4621f9f8be17 Mon Sep 17 00:00:00 2001
From: Joseph Mirabel <jmirabel@laas.fr>
Date: Wed, 10 Feb 2021 10:36:11 +0100
Subject: [PATCH] Clean RosQueuedSubscribe commands.

---
 src/ros_queued_subscribe-python-module-py.cc |  25 ++++
 src/ros_queued_subscribe-python.hh           |   3 -
 src/ros_queued_subscribe.cpp                 | 125 +------------------
 src/ros_queued_subscribe.hh                  |   8 +-
 4 files changed, 30 insertions(+), 131 deletions(-)
 create mode 100644 src/ros_queued_subscribe-python-module-py.cc
 delete mode 100644 src/ros_queued_subscribe-python.hh

diff --git a/src/ros_queued_subscribe-python-module-py.cc b/src/ros_queued_subscribe-python-module-py.cc
new file mode 100644
index 0000000..00f2fb9
--- /dev/null
+++ b/src/ros_queued_subscribe-python-module-py.cc
@@ -0,0 +1,25 @@
+#include <dynamic-graph/python/module.hh>
+#include "ros_queued_subscribe.hh"
+
+namespace dg = dynamicgraph;
+
+
+BOOST_PYTHON_MODULE(wrap)
+{
+  bp::import("dynamic_graph");
+
+  dg::python::exposeEntity<dg::RosQueuedSubscribe, bp::bases<dg::Entity>, dg::python::AddCommands>()
+    .def("clear", &dg::RosQueuedSubscribe::clear, "Remove all signals reading data from a ROS topic")
+    .def("rm", &dg::RosQueuedSubscribe::rm, "Remove a signal reading data from a ROS topic",
+        bp::args("signal_name"))
+    .def("list", &dg::RosQueuedSubscribe::list, "List signals reading data from a ROS topic")
+
+    .def("clearQueue", &dg::RosQueuedSubscribe::clearQueue,
+        "Empty the queue of a given signal", bp::args("signal_name"))
+    .def("queueSize", &dg::RosQueuedSubscribe::queueSize,
+        "Return the queue size of a given signal", bp::args("signal_name"))
+    .def("readQueue", &dg::RosQueuedSubscribe::queueSize,
+        "Whether signals should read values from the queues, and when.",
+        bp::args("start_time"))
+    ;
+}
diff --git a/src/ros_queued_subscribe-python.hh b/src/ros_queued_subscribe-python.hh
deleted file mode 100644
index e6880f8..0000000
--- a/src/ros_queued_subscribe-python.hh
+++ /dev/null
@@ -1,3 +0,0 @@
-#include "ros_queued_subscribe.hh"
-
-typedef boost::mpl::vector< dynamicgraph::RosQueuedSubscribe > entities_t;
diff --git a/src/ros_queued_subscribe.cpp b/src/ros_queued_subscribe.cpp
index 779d9d6..f9ecaf5 100644
--- a/src/ros_queued_subscribe.cpp
+++ b/src/ros_queued_subscribe.cpp
@@ -24,37 +24,6 @@ DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(RosQueuedSubscribe, "RosQueuedSubscribe");
 
 namespace command {
 namespace rosQueuedSubscribe {
-Clear::Clear(RosQueuedSubscribe& entity, const std::string& docstring)
-    : Command(entity, std::vector<Value::Type>(), docstring) {}
-
-Value Clear::doExecute() {
-  RosQueuedSubscribe& entity = static_cast<RosQueuedSubscribe&>(owner());
-
-  entity.clear();
-  return Value();
-}
-
-ClearQueue::ClearQueue(RosQueuedSubscribe& entity, const std::string& docstring)
-    : Command(entity, boost::assign::list_of(Value::STRING), docstring) {}
-
-Value ClearQueue::doExecute() {
-  RosQueuedSubscribe& entity = static_cast<RosQueuedSubscribe&>(owner());
-
-  std::vector<Value> values = getParameterValues();
-  const std::string& signal = values[0].value();
-  entity.clearQueue(signal);
-
-  return Value();
-}
-
-List::List(RosQueuedSubscribe& entity, const std::string& docstring)
-    : Command(entity, std::vector<Value::Type>(), docstring) {}
-
-Value List::doExecute() {
-  RosQueuedSubscribe& entity = static_cast<RosQueuedSubscribe&>(owner());
-  return Value(entity.list());
-}
-
 Add::Add(RosQueuedSubscribe& entity, const std::string& docstring)
     : Command(entity, boost::assign::list_of(Value::STRING)(Value::STRING)(Value::STRING), docstring) {}
 
@@ -84,42 +53,6 @@ Value Add::doExecute() {
     throw std::runtime_error("bad type");
   return Value();
 }
-
-Rm::Rm(RosQueuedSubscribe& entity, const std::string& docstring)
-    : Command(entity, boost::assign::list_of(Value::STRING), docstring) {}
-
-Value Rm::doExecute() {
-  RosQueuedSubscribe& entity = static_cast<RosQueuedSubscribe&>(owner());
-  std::vector<Value> values = getParameterValues();
-  const std::string& signal = values[0].value();
-  entity.rm(signal);
-  return Value();
-}
-
-QueueSize::QueueSize(RosQueuedSubscribe& entity, const std::string& docstring)
-    : Command(entity, boost::assign::list_of(Value::STRING), docstring) {}
-
-Value QueueSize::doExecute() {
-  RosQueuedSubscribe& entity = static_cast<RosQueuedSubscribe&>(owner());
-
-  std::vector<Value> values = getParameterValues();
-  const std::string& signal = values[0].value();
-
-  return Value((unsigned)entity.queueSize(signal));
-}
-
-ReadQueue::ReadQueue(RosQueuedSubscribe& entity, const std::string& docstring)
-    : Command(entity, boost::assign::list_of(Value::INT), docstring) {}
-
-Value ReadQueue::doExecute() {
-  RosQueuedSubscribe& entity = static_cast<RosQueuedSubscribe&>(owner());
-
-  std::vector<Value> values = getParameterValues();
-  int read = values[0].value();
-  entity.readQueue(read);
-
-  return Value();
-}
 }  // namespace rosQueuedSubscribe
 }  // end of namespace command.
 
@@ -141,53 +74,6 @@ RosQueuedSubscribe::RosQueuedSubscribe(const std::string& n)
       "    - topic:  the topic name in ROS.\n"
       "\n";
   addCommand("add", new command::rosQueuedSubscribe::Add(*this, docstring));
-  docstring =
-      "\n"
-      "  Remove a signal reading data from 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::rosQueuedSubscribe::Rm(*this, docstring));
-  docstring =
-      "\n"
-      "  Remove all signals reading data from a ROS topic\n"
-      "\n"
-      "  No input:\n"
-      "\n";
-  addCommand("clear", new command::rosQueuedSubscribe::Clear(*this, docstring));
-  docstring =
-      "\n"
-      "  List signals reading data from a ROS topic\n"
-      "\n"
-      "  No input:\n"
-      "\n";
-  addCommand("list", new command::rosQueuedSubscribe::List(*this, docstring));
-  docstring =
-      "\n"
-      "  Empty the queue of a given signal\n"
-      "\n"
-      "  Input is:\n"
-      "    - name of the signal (see method list for the list of signals).\n"
-      "\n";
-  addCommand("clearQueue", new command::rosQueuedSubscribe::ClearQueue(*this, docstring));
-  docstring =
-      "\n"
-      "  Return the queue size of a given signal\n"
-      "\n"
-      "  Input is:\n"
-      "    - name of the signal (see method list for the list of signals).\n"
-      "\n";
-  addCommand("queueSize", new command::rosQueuedSubscribe::QueueSize(*this, docstring));
-  docstring =
-      "\n"
-      "  Whether signals should read values from the queues, and when.\n"
-      "\n"
-      "  Input is:\n"
-      "    - int (dynamic graph time at which the reading begin).\n"
-      "\n";
-  addCommand("readQueue", new command::rosQueuedSubscribe::ReadQueue(*this, docstring));
 }
 
 RosQueuedSubscribe::~RosQueuedSubscribe() {}
@@ -206,13 +92,10 @@ void RosQueuedSubscribe::rm(const std::string& signal) {
   }
 }
 
-std::string RosQueuedSubscribe::list() {
-  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> RosQueuedSubscribe::list() {
+  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_queued_subscribe.hh b/src/ros_queued_subscribe.hh
index 72c36a3..ff0eb9f 100644
--- a/src/ros_queued_subscribe.hh
+++ b/src/ros_queued_subscribe.hh
@@ -38,12 +38,6 @@ using ::dynamicgraph::command::Value;
   }
 
 ROS_QUEUED_SUBSCRIBE_MAKE_COMMAND(Add);
-ROS_QUEUED_SUBSCRIBE_MAKE_COMMAND(Clear);
-ROS_QUEUED_SUBSCRIBE_MAKE_COMMAND(List);
-ROS_QUEUED_SUBSCRIBE_MAKE_COMMAND(Rm);
-ROS_QUEUED_SUBSCRIBE_MAKE_COMMAND(ClearQueue);
-ROS_QUEUED_SUBSCRIBE_MAKE_COMMAND(QueueSize);
-ROS_QUEUED_SUBSCRIBE_MAKE_COMMAND(ReadQueue);
 
 #undef ROS_QUEUED_SUBSCRIBE_MAKE_COMMAND
 
@@ -142,7 +136,7 @@ class RosQueuedSubscribe : public dynamicgraph::Entity {
   void display(std::ostream& os) const;
 
   void rm(const std::string& signal);
-  std::string list();
+  std::vector<std::string> list();
   void clear();
   void clearQueue(const std::string& signal);
   void readQueue(int beginReadingAt);
-- 
GitLab