rclcpp  beta1
C++ ROS Client Library API
parameter.hpp
Go to the documentation of this file.
1 // Copyright 2015 Open Source Robotics Foundation, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef RCLCPP__PARAMETER_HPP_
16 #define RCLCPP__PARAMETER_HPP_
17 
18 #include <ostream>
19 #include <sstream>
20 #include <string>
21 #include <vector>
22 
23 #include "rcl_interfaces/msg/parameter.hpp"
24 #include "rcl_interfaces/msg/parameter_type.hpp"
25 #include "rcl_interfaces/msg/parameter_value.hpp"
27 #include "rmw/rmw.h"
28 
29 namespace rclcpp
30 {
31 namespace parameter
32 {
33 
35 {
42 };
43 
44 // Structure to store an arbitrary parameter with templated get/set methods
46 {
47 public:
51  explicit ParameterVariant(const std::string & name, const bool bool_value);
53  explicit ParameterVariant(const std::string & name, const int int_value);
55  explicit ParameterVariant(const std::string & name, const int64_t int_value);
57  explicit ParameterVariant(const std::string & name, const float double_value);
59  explicit ParameterVariant(const std::string & name, const double double_value);
61  explicit ParameterVariant(const std::string & name, const std::string & string_value);
63  explicit ParameterVariant(const std::string & name, const char * string_value);
65  explicit ParameterVariant(const std::string & name, const std::vector<uint8_t> & bytes_value);
66 
69  get_type() const;
70 
73  get_type_name() const;
74 
76  const std::string &
77  get_name() const;
78 
80  rcl_interfaces::msg::ParameterValue
81  get_parameter_value() const;
82 
83  // The following get_value() variants require the use of ParameterType
84 
85  template<ParameterType type>
87  get_value() const
88  {
90  // TODO(wjwwood): use custom exception
91  throw std::runtime_error("Invalid type");
92  }
93  return value_.integer_value;
94  }
95 
96  template<ParameterType type>
98  get_value() const
99  {
101  // TODO(wjwwood): use custom exception
102  throw std::runtime_error("Invalid type");
103  }
104  return value_.double_value;
105  }
106 
107  template<ParameterType type>
109  get_value() const
110  {
112  // TODO(wjwwood): use custom exception
113  throw std::runtime_error("Invalid type");
114  }
115  return value_.string_value;
116  }
117 
118  template<ParameterType type>
120  get_value() const
121  {
123  // TODO(wjwwood): use custom exception
124  throw std::runtime_error("Invalid type");
125  }
126  return value_.bool_value;
127  }
128 
129  template<ParameterType type>
130  typename std::enable_if<
132  get_value() const
133  {
135  // TODO(wjwwood): use custom exception
136  throw std::runtime_error("Invalid type");
137  }
138  return value_.bytes_value;
139  }
140 
141  // The following get_value() variants allow the use of primitive types
142 
143  template<typename type>
144  typename std::enable_if<
146  get_value() const
147  {
148  return get_value<ParameterType::PARAMETER_INTEGER>();
149  }
150 
151  template<typename type>
153  get_value() const
154  {
155  return get_value<ParameterType::PARAMETER_DOUBLE>();
156  }
157 
158  template<typename type>
160  get_value() const
161  {
162  return get_value<ParameterType::PARAMETER_STRING>();
163  }
164 
165  template<typename type>
167  get_value() const
168  {
169  return get_value<ParameterType::PARAMETER_BOOL>();
170  }
171 
172  template<typename type>
173  typename std::enable_if<
175  type, const std::vector<uint8_t> &>::value, const std::vector<uint8_t> &>::type
176  get_value() const
177  {
178  return get_value<ParameterType::PARAMETER_BYTES>();
179  }
180 
182  int64_t
183  as_int() const;
184 
186  double
187  as_double() const;
188 
190  const std::string &
191  as_string() const;
192 
194  bool
195  as_bool() const;
196 
198  const std::vector<uint8_t> &
199  as_bytes() const;
200 
202  static ParameterVariant
203  from_parameter(const rcl_interfaces::msg::Parameter & parameter);
204 
206  rcl_interfaces::msg::Parameter
207  to_parameter();
208 
211  value_to_string() const;
212 
213 private:
214  std::string name_;
215  rcl_interfaces::msg::ParameterValue value_;
216 };
217 
218 
223 
225 std::ostream &
227 
229 std::ostream &
230 operator<<(std::ostream & os, const std::vector<ParameterVariant> & parameters);
231 
232 } // namespace parameter
233 } // namespace rclcpp
234 
235 namespace std
236 {
237 
242 
247 
248 } // namespace std
249 
250 #endif // RCLCPP__PARAMETER_HPP_
std::ostream & operator<<(std::ostream &os, const rclcpp::parameter::ParameterVariant &pv)
const std::string & as_string() const
std::enable_if< type==ParameterType::PARAMETER_BYTES, const std::vector< uint8_t > & >::type get_value() const
Definition: parameter.hpp:132
rcl_interfaces::msg::ParameterValue get_parameter_value() const
std::enable_if< type==ParameterType::PARAMETER_DOUBLE, double >::type get_value() const
Definition: parameter.hpp:98
static ParameterVariant from_parameter(const rcl_interfaces::msg::Parameter &parameter)
Definition: parameter.hpp:40
Definition: allocator_common.hpp:24
std::enable_if< std::is_floating_point< type >::value, double >::type get_value() const
Definition: parameter.hpp:153
std::enable_if< type==ParameterType::PARAMETER_STRING, const std::string & >::type get_value() const
Definition: parameter.hpp:109
Definition: parameter.hpp:37
std::enable_if< std::is_same< type, bool >::value, bool >::type get_value() const
Definition: parameter.hpp:167
std::string to_string(const std::vector< rclcpp::parameter::ParameterVariant > &parameters)
Return a json encoded version of a vector of parameters, as a string.
std::enable_if< type==ParameterType::PARAMETER_INTEGER, int64_t >::type get_value() const
Definition: parameter.hpp:87
Definition: parameter.hpp:41
ParameterType
Definition: parameter.hpp:34
rcl_interfaces::msg::Parameter to_parameter()
std::enable_if< std::is_convertible< type, const std::vector< uint8_t > & >::value, const std::vector< uint8_t > & >::type get_value() const
Definition: parameter.hpp:176
std::enable_if< type==ParameterType::PARAMETER_BOOL, bool >::type get_value() const
Definition: parameter.hpp:120
std::string _to_json_dict_entry(const ParameterVariant &param)
Return a json encoded version of the parameter intended for a dict.
std::string value_to_string() const
#define RCLCPP_PUBLIC
Definition: visibility_control.hpp:50
std::enable_if< std::is_convertible< type, std::string >::value, const std::string & >::type get_value() const
Definition: parameter.hpp:160
const std::vector< uint8_t > & as_bytes() const
const std::string & get_name() const
std::enable_if< std::is_integral< type >::value &&!std::is_same< type, bool >::value, int64_t >::type get_value() const
Definition: parameter.hpp:146
Definition: parameter.hpp:36
Definition: parameter.hpp:39
ParameterType get_type() const
Definition: parameter.hpp:45
Definition: parameter.hpp:38