Robowflex  v0.1
Making MoveIt Easy
handler.h
Go to the documentation of this file.
1 /* Author: Zachary Kingston */
2 
3 #ifndef ROBOWFLEX_IO_HANDLER_
4 #define ROBOWFLEX_IO_HANDLER_
5 
6 #include <string> // for std::string
7 
8 #include <yaml-cpp/yaml.h> // for YAML::Node
9 
10 #include <ros/node_handle.h> // for ros::NodeHandle
11 
12 namespace robowflex
13 {
14  namespace IO
15  {
16  /** \brief ROS parameter server handler to handle namespacing and automatic parameter deletion.
17  */
18  class Handler
19  {
20  public:
21  /** \brief Constructor.
22  * \param[in] name Name for namespace.
23  */
24  Handler(const std::string &name);
25 
26  // non-copyable
27  Handler(Handler const &) = delete;
28  void operator=(Handler const &) = delete;
29 
30  /** \brief Copy constructor. Handles namespacing.
31  * \param[in] handler Parent handler.
32  * \param[in] name Additional namespace to add to parent handler.
33  */
34  Handler(const IO::Handler &handler, const std::string &name = "");
35 
36  /** \brief Destructor.
37  * Deletes all parameters created through this handler.
38  */
39  ~Handler();
40 
41  /** \brief Loads the contents of a YAML node to the parameter server under a \a prefix.
42  * \param[in] node YAML node to load.
43  * \param[in] prefix Prefix to put YAML node under.
44  */
45  void loadYAMLtoROS(const YAML::Node &node, const std::string &prefix = "");
46 
47  /** \brief Sets a parameter on the parameter server.
48  * \param[in] key Key to store parameter under.
49  * \param[in] value Value to store.
50  * \tparam T Type of the \a value.
51  */
52  template <typename T>
53  void setParam(const std::string &key, const T &value)
54  {
55  nh_.setParam(key, value);
56  params_.emplace_back(key);
57  }
58 
59  /** \brief Checks if the parameter server has \a key.
60  * \param[in] key Key to check.
61  * \return True if \a key exists, false otherwise.
62  */
63  bool hasParam(const std::string &key) const;
64 
65  /** \brief Gets a parameter from the parameter server.
66  * \param[in] key Key of parameter.
67  * \param[out] value Value to store.
68  * \tparam T Type of the \a value.
69  */
70  template <typename T>
71  bool getParam(const std::string &key, T &value) const
72  {
73  return nh_.getParam(key, value);
74  }
75 
76  /** \brief Gets the node handle.
77  * \return The node handle.
78  */
79  const ros::NodeHandle &getHandle() const;
80 
81  /** \brief Gets the name of the handler.
82  * \return The name of the handler.
83  */
84  const std::string &getName() const;
85 
86  /** \brief Gets the namespace of the handler.
87  * \return The namespace of the handler.
88  */
89  const std::string &getNamespace() const;
90 
91  private:
92  static const std::string UUID; ///< UUID of handler.
93 
94  const std::string name_; ///< Name of handler.
95  const std::string namespace_; ///< Full namespace of handler.
96  ros::NodeHandle nh_; ///< ROS node handle.
97 
98  std::vector<std::string> params_; ///< Set parameter keys.
99  };
100  } // namespace IO
101 } // namespace robowflex
102 
103 #endif
ROS parameter server handler to handle namespacing and automatic parameter deletion.
Definition: handler.h:19
void setParam(const std::string &key, const T &value)
Sets a parameter on the parameter server.
Definition: handler.h:53
const std::string & getName() const
Gets the name of the handler.
~Handler()
Destructor. Deletes all parameters created through this handler.
bool hasParam(const std::string &key) const
Checks if the parameter server has key.
Handler(Handler const &)=delete
std::vector< std::string > params_
Set parameter keys.
Definition: handler.h:98
const ros::NodeHandle & getHandle() const
Gets the node handle.
void operator=(Handler const &)=delete
const std::string namespace_
Full namespace of handler.
Definition: handler.h:95
ros::NodeHandle nh_
ROS node handle.
Definition: handler.h:96
bool getParam(const std::string &key, T &value) const
Gets a parameter from the parameter server.
Definition: handler.h:71
void loadYAMLtoROS(const YAML::Node &node, const std::string &prefix="")
Loads the contents of a YAML node to the parameter server under a prefix.
const std::string & getNamespace() const
Gets the namespace of the handler.
const std::string name_
Name of handler.
Definition: handler.h:94
Handler(const std::string &name)
Constructor.
static const std::string UUID
UUID of handler.
Definition: handler.h:92
T emplace_back(T... args)
Main namespace. Contains all library classes and functions.
Definition: scene.cpp:25