rclcpp  master
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 <iostream>
19 #include <ostream>
20 #include <sstream>
21 #include <string>
22 #include <vector>
23 
24 #include "rcl_interfaces/msg/parameter.hpp"
27 
28 namespace rclcpp
29 {
30 
31 class Parameter;
32 
33 namespace node_interfaces
34 {
35 struct ParameterInfo;
36 } // namespace node_interfaces
37 
38 namespace detail
39 {
40 
41 // This helper function is required because you cannot do specialization on a
42 // class method, so instead we specialize this template function and call it
43 // from the unspecialized, but dependent, class method.
44 template<typename T>
45 auto
46 get_value_helper(const rclcpp::Parameter * parameter);
47 
48 } // namespace detail
49 
51 class Parameter
52 {
53 public:
56  Parameter();
57 
60  explicit Parameter(const std::string & name);
61 
64  Parameter(const std::string & name, const ParameterValue & value);
65 
67  template<typename ValueTypeT>
68  Parameter(const std::string & name, ValueTypeT value)
69  : Parameter(name, ParameterValue(value))
70  {}
71 
73  explicit Parameter(const rclcpp::node_interfaces::ParameterInfo & parameter_info);
74 
77  bool
78  operator==(const Parameter & rhs) const;
79 
82  bool
83  operator!=(const Parameter & rhs) const;
84 
87  get_type() const;
88 
91  get_type_name() const;
92 
94  const std::string &
95  get_name() const;
96 
98  rcl_interfaces::msg::ParameterValue
99  get_value_message() const;
100 
103  const rclcpp::ParameterValue &
104  get_parameter_value() const;
105 
107  template<ParameterType ParamT>
108  decltype(auto)
109  get_value() const
110  {
111  return value_.get<ParamT>();
112  }
113 
115  template<typename T>
116  decltype(auto)
117  get_value() const;
118 
120  bool
121  as_bool() const;
122 
124  int64_t
125  as_int() const;
126 
128  double
129  as_double() const;
130 
132  const std::string &
133  as_string() const;
134 
136  const std::vector<uint8_t> &
137  as_byte_array() const;
138 
140  const std::vector<bool> &
141  as_bool_array() const;
142 
144  const std::vector<int64_t> &
145  as_integer_array() const;
146 
148  const std::vector<double> &
149  as_double_array() const;
150 
153  as_string_array() const;
154 
156  static Parameter
157  from_parameter_msg(const rcl_interfaces::msg::Parameter & parameter);
158 
160  rcl_interfaces::msg::Parameter
161  to_parameter_msg() const;
162 
165  value_to_string() const;
166 
167 private:
168  std::string name_;
169  ParameterValue value_;
170 };
171 
175 _to_json_dict_entry(const Parameter & param);
176 
178 std::ostream &
179 operator<<(std::ostream & os, const rclcpp::Parameter & pv);
180 
182 std::ostream &
183 operator<<(std::ostream & os, const std::vector<Parameter> & parameters);
184 
185 namespace detail
186 {
187 
188 template<typename T>
189 auto
191 {
192  return parameter->get_parameter_value().get<T>();
193 }
194 
195 // Specialization allowing Parameter::get() to return a const ref to the parameter value object.
196 template<>
197 inline
198 auto
199 get_value_helper<rclcpp::ParameterValue>(const rclcpp::Parameter * parameter)
200 {
201  return parameter->get_parameter_value();
202 }
203 
204 // Specialization allowing Parameter::get() to return a const ref to the parameter itself.
205 template<>
206 inline
207 auto
208 get_value_helper<rclcpp::Parameter>(const rclcpp::Parameter * parameter)
209 {
210  // Use this lambda to ensure it's a const reference being returned (and not a copy).
211  auto type_enforcing_lambda =
212  [&parameter]() -> const rclcpp::Parameter & {
213  return *parameter;
214  };
215  return type_enforcing_lambda();
216 }
217 
218 } // namespace detail
219 
220 template<typename T>
221 decltype(auto)
222 Parameter::get_value() const
223 {
224  // use the helper to specialize for the ParameterValue and Parameter cases.
225  return detail::get_value_helper<T>(this);
226 }
227 
228 } // namespace rclcpp
229 
230 namespace std
231 {
232 
236 to_string(const rclcpp::Parameter & param);
237 
241 to_string(const std::vector<rclcpp::Parameter> & parameters);
242 
243 } // namespace std
244 
245 #endif // RCLCPP__PARAMETER_HPP_
auto get_value_helper(const rclcpp::Parameter *parameter)
Definition: parameter.hpp:190
Structure to store an arbitrary parameter with templated get/set methods.
Definition: parameter.hpp:51
This header provides the get_node_base_interface() template function.
Definition: allocator_common.hpp:24
decltype(auto) get_value() const
Get value of parameter using rclcpp::ParameterType as template argument.
Definition: parameter.hpp:109
Definition: node_parameters.hpp:45
std::string to_string(ParameterType type)
Return the name of a parameter type.
Parameter(const std::string &name, ValueTypeT value)
Construct with given name and given parameter value.
Definition: parameter.hpp:68
#define RCLCPP_PUBLIC
Definition: visibility_control.hpp:50
ParameterType
Definition: parameter_value.hpp:32
const rclcpp::ParameterValue & get_parameter_value() const
Get the internal storage for the parameter value.
constexpr std::enable_if< type==ParameterType::PARAMETER_BOOL, const bool & >::type get() const
Definition: parameter_value.hpp:148
Store the type and value of a parameter.
Definition: parameter_value.hpp:71
std::ostream & operator<<(std::ostream &os, const rclcpp::Parameter &pv)
std::string _to_json_dict_entry(const Parameter &param)
Return a json encoded version of the parameter intended for a dict.