ROS 2 rclcpp + rcl - rolling  rolling-a919a6e5
ROS 2 C++ Client Library with ROS Client Library
node_parameters.hpp
1 // Copyright 2016 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__NODE_INTERFACES__NODE_PARAMETERS_HPP_
16 #define RCLCPP__NODE_INTERFACES__NODE_PARAMETERS_HPP_
17 
18 #include <list>
19 #include <map>
20 #include <memory>
21 #include <mutex>
22 #include <string>
23 #include <vector>
24 
25 #include "rcutils/macros.h"
26 
27 #include "rcl_interfaces/msg/list_parameters_result.hpp"
28 #include "rcl_interfaces/msg/parameter_descriptor.hpp"
29 #include "rcl_interfaces/msg/parameter_event.hpp"
30 #include "rcl_interfaces/msg/set_parameters_result.hpp"
31 
32 #include "rclcpp/macros.hpp"
33 #include "rclcpp/node_interfaces/node_base_interface.hpp"
34 #include "rclcpp/node_interfaces/node_logging_interface.hpp"
35 #include "rclcpp/node_interfaces/node_parameters_interface.hpp"
36 #include "rclcpp/node_interfaces/node_services_interface.hpp"
37 #include "rclcpp/node_interfaces/node_topics_interface.hpp"
38 #include "rclcpp/parameter.hpp"
39 #include "rclcpp/parameter_service.hpp"
40 #include "rclcpp/publisher.hpp"
41 #include "rclcpp/visibility_control.hpp"
42 
43 namespace rclcpp
44 {
45 namespace node_interfaces
46 {
47 
48 // Internal struct for holding useful info about parameters
50 {
53 
55  rcl_interfaces::msg::ParameterDescriptor descriptor;
56 };
57 
58 // Internal RAII-style guard for mutation recursion
60 {
61 public:
62  explicit ParameterMutationRecursionGuard(bool & allow_mod)
63  : allow_modification_(allow_mod)
64  {
65  if (!allow_modification_) {
67  "cannot set or declare a parameter, or change the callback from within set callback");
68  }
69 
70  allow_modification_ = false;
71  }
72 
74  {
75  allow_modification_ = true;
76  }
77 
78 private:
79  bool & allow_modification_;
80 };
81 
84 {
85 public:
86  RCLCPP_SMART_PTR_ALIASES_ONLY(NodeParameters)
87 
88 
98  RCLCPP_PUBLIC
100  const node_interfaces::NodeBaseInterface::SharedPtr node_base,
101  const node_interfaces::NodeLoggingInterface::SharedPtr node_logging,
102  const node_interfaces::NodeTopicsInterface::SharedPtr node_topics,
103  const node_interfaces::NodeServicesInterface::SharedPtr node_services,
104  const node_interfaces::NodeClockInterface::SharedPtr node_clock,
105  const std::vector<Parameter> & parameter_overrides,
106  bool start_parameter_services,
107  bool start_parameter_event_publisher,
108  const rclcpp::QoS & parameter_event_qos,
109  const rclcpp::PublisherOptionsBase & parameter_event_publisher_options,
110  bool allow_undeclared_parameters,
111  bool automatically_declare_parameters_from_overrides);
112 
113  RCLCPP_PUBLIC
114  virtual
115  ~NodeParameters();
116 
117  RCLCPP_PUBLIC
118  const rclcpp::ParameterValue &
120  const std::string & name,
121  const rclcpp::ParameterValue & default_value,
122  const rcl_interfaces::msg::ParameterDescriptor & parameter_descriptor =
123  rcl_interfaces::msg::ParameterDescriptor{},
124  bool ignore_override = false) override;
125 
126  RCLCPP_PUBLIC
127  const rclcpp::ParameterValue &
129  const std::string & name,
130  rclcpp::ParameterType type,
131  const rcl_interfaces::msg::ParameterDescriptor & parameter_descriptor =
132  rcl_interfaces::msg::ParameterDescriptor(),
133  bool ignore_override = false) override;
134 
135  RCLCPP_PUBLIC
136  void
137  undeclare_parameter(const std::string & name) override;
138 
139  RCLCPP_PUBLIC
140  bool
141  has_parameter(const std::string & name) const override;
142 
143  RCLCPP_PUBLIC
144  std::vector<rcl_interfaces::msg::SetParametersResult>
146  const std::vector<rclcpp::Parameter> & parameters) override;
147 
148  RCLCPP_PUBLIC
149  rcl_interfaces::msg::SetParametersResult
151  const std::vector<rclcpp::Parameter> & parameters) override;
152 
153  RCLCPP_PUBLIC
154  std::vector<rclcpp::Parameter>
155  get_parameters(const std::vector<std::string> & names) const override;
156 
157  RCLCPP_PUBLIC
159  get_parameter(const std::string & name) const override;
160 
161  RCLCPP_PUBLIC
162  bool
164  const std::string & name,
165  rclcpp::Parameter & parameter) const override;
166 
167  RCLCPP_PUBLIC
168  bool
170  const std::string & prefix,
171  std::map<std::string, rclcpp::Parameter> & parameters) const override;
172 
173  RCLCPP_PUBLIC
174  std::vector<rcl_interfaces::msg::ParameterDescriptor>
175  describe_parameters(const std::vector<std::string> & names) const override;
176 
177  RCLCPP_PUBLIC
178  std::vector<uint8_t>
179  get_parameter_types(const std::vector<std::string> & names) const override;
180 
181  RCLCPP_PUBLIC
182  rcl_interfaces::msg::ListParametersResult
183  list_parameters(const std::vector<std::string> & prefixes, uint64_t depth) const override;
184 
185  RCLCPP_PUBLIC
186  RCUTILS_WARN_UNUSED
187  PreSetParametersCallbackHandle::SharedPtr
188  add_pre_set_parameters_callback(PreSetParametersCallbackType callback) override;
189 
190  RCLCPP_PUBLIC
191  RCUTILS_WARN_UNUSED
192  OnSetParametersCallbackHandle::SharedPtr
193  add_on_set_parameters_callback(OnSetParametersCallbackType callback) override;
194 
195  RCLCPP_PUBLIC
196  RCUTILS_WARN_UNUSED
197  PostSetParametersCallbackHandle::SharedPtr
198  add_post_set_parameters_callback(PostSetParametersCallbackType callback) override;
199 
200  RCLCPP_PUBLIC
201  void
203 
204  RCLCPP_PUBLIC
205  void
207  override;
208 
209  RCLCPP_PUBLIC
210  void
212 
213  RCLCPP_PUBLIC
214  const std::map<std::string, rclcpp::ParameterValue> &
215  get_parameter_overrides() const override;
216 
217  RCLCPP_PUBLIC
218  void
220 
221  using PreSetCallbacksHandleContainer = std::list<PreSetParametersCallbackHandle::WeakPtr>;
222  using OnSetCallbacksHandleContainer = std::list<OnSetParametersCallbackHandle::WeakPtr>;
223  using PostSetCallbacksHandleContainer = std::list<PostSetParametersCallbackHandle::WeakPtr>;
224  using CallbacksContainerType [[deprecated("use OnSetCallbacksHandleContainer instead")]] =
225  OnSetCallbacksHandleContainer;
226 
227 protected:
228  RCLCPP_PUBLIC
229  void
230  perform_automatically_declare_parameters_from_overrides();
231 
232 private:
233  RCLCPP_DISABLE_COPY(NodeParameters)
234 
235  mutable std::recursive_mutex mutex_;
236 
237  // There are times when we don't want to allow modifications to parameters
238  // (particularly when a set_parameter callback tries to call set_parameter,
239  // declare_parameter, etc). In those cases, this will be set to false.
240  bool parameter_modification_enabled_{true};
241 
242  PreSetCallbacksHandleContainer pre_set_parameters_callback_container_;
243 
244  OnSetCallbacksHandleContainer on_set_parameters_callback_container_;
245 
246  PostSetCallbacksHandleContainer post_set_parameters_callback_container_;
247 
248  std::map<std::string, ParameterInfo> parameters_;
249 
250  std::map<std::string, rclcpp::ParameterValue> parameter_overrides_;
251 
252  bool allow_undeclared_ = false;
253 
255 
256  std::shared_ptr<ParameterService> parameter_service_;
257 
258  std::string combined_name_;
259 
260  node_interfaces::NodeLoggingInterface::SharedPtr node_logging_;
261  node_interfaces::NodeClockInterface::SharedPtr node_clock_;
262 };
263 
264 } // namespace node_interfaces
265 } // namespace rclcpp
266 
267 #endif // RCLCPP__NODE_INTERFACES__NODE_PARAMETERS_HPP_
Store the type and value of a parameter.
Structure to store an arbitrary parameter with templated get/set methods.
Definition: parameter.hpp:53
A publisher publishes messages of any type to a topic.
Definition: publisher.hpp:81
Encapsulation of Quality of Service settings.
Definition: qos.hpp:116
Thrown if parameter is modified while in a set callback.
Definition: exceptions.hpp:324
Pure virtual interface class for the NodeParameters part of the Node API.
Implementation of the NodeParameters part of the Node API.
RCLCPP_PUBLIC RCUTILS_WARN_UNUSED PostSetParametersCallbackHandle::SharedPtr add_post_set_parameters_callback(PostSetParametersCallbackType callback) override
Add a callback that gets triggered after parameters are set successfully.
RCLCPP_PUBLIC const std::map< std::string, rclcpp::ParameterValue > & get_parameter_overrides() const override
Return the initial parameter values used by the NodeParameters to override default values.
RCLCPP_PUBLIC void remove_on_set_parameters_callback(const OnSetParametersCallbackHandle *const handler) override
Remove a callback registered with add_on_set_parameters_callback.
RCLCPP_PUBLIC NodeParameters(const node_interfaces::NodeBaseInterface::SharedPtr node_base, const node_interfaces::NodeLoggingInterface::SharedPtr node_logging, const node_interfaces::NodeTopicsInterface::SharedPtr node_topics, const node_interfaces::NodeServicesInterface::SharedPtr node_services, const node_interfaces::NodeClockInterface::SharedPtr node_clock, const std::vector< Parameter > &parameter_overrides, bool start_parameter_services, bool start_parameter_event_publisher, const rclcpp::QoS &parameter_event_qos, const rclcpp::PublisherOptionsBase &parameter_event_publisher_options, bool allow_undeclared_parameters, bool automatically_declare_parameters_from_overrides)
Constructor.
RCLCPP_PUBLIC std::vector< rclcpp::Parameter > get_parameters(const std::vector< std::string > &names) const override
Get descriptions of parameters given their names.
RCLCPP_PUBLIC void undeclare_parameter(const std::string &name) override
Undeclare a parameter.
RCLCPP_PUBLIC const rclcpp::ParameterValue & declare_parameter(const std::string &name, const rclcpp::ParameterValue &default_value, const rcl_interfaces::msg::ParameterDescriptor &parameter_descriptor=rcl_interfaces::msg::ParameterDescriptor{}, bool ignore_override=false) override
Declare and initialize a parameter.
RCLCPP_PUBLIC RCUTILS_WARN_UNUSED OnSetParametersCallbackHandle::SharedPtr add_on_set_parameters_callback(OnSetParametersCallbackType callback) override
Add a callback to validate parameters before they are set.
RCLCPP_PUBLIC bool get_parameters_by_prefix(const std::string &prefix, std::map< std::string, rclcpp::Parameter > &parameters) const override
Get all parameters that have the specified prefix into the parameters map.
RCLCPP_PUBLIC std::vector< rcl_interfaces::msg::SetParametersResult > set_parameters(const std::vector< rclcpp::Parameter > &parameters) override
Set one or more parameters, one at a time.
RCLCPP_PUBLIC void remove_pre_set_parameters_callback(const PreSetParametersCallbackHandle *const handler) override
Remove a callback registered with add_pre_set_parameters_callback.
RCLCPP_PUBLIC void enable_parameter_modification() override
Enable parameter modification recursively during parameter callbacks.
RCLCPP_PUBLIC rcl_interfaces::msg::SetParametersResult set_parameters_atomically(const std::vector< rclcpp::Parameter > &parameters) override
Set one or more parameters, all at once.
RCLCPP_PUBLIC RCUTILS_WARN_UNUSED PreSetParametersCallbackHandle::SharedPtr add_pre_set_parameters_callback(PreSetParametersCallbackType callback) override
Add a callback that gets triggered before parameters are validated.
RCLCPP_PUBLIC rclcpp::Parameter get_parameter(const std::string &name) const override
Get the description of one parameter given a name.
RCLCPP_PUBLIC void remove_post_set_parameters_callback(const PostSetParametersCallbackHandle *const handler) override
Remove a callback registered with add_post_set_parameters_callback.
RCLCPP_PUBLIC bool has_parameter(const std::string &name) const override
Return true if the parameter has been declared, otherwise false.
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
Non-templated part of PublisherOptionsWithAllocator<Allocator>.
rcl_interfaces::msg::ParameterDescriptor descriptor
A description of the parameter.
rclcpp::ParameterValue value
Current value of the parameter.