ROS 2 rclcpp + rcl - humble  humble
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 <map>
19 #include <memory>
20 #include <list>
21 #include <string>
22 #include <vector>
23 
24 #include "rcutils/macros.h"
25 
26 #include "rcl_interfaces/msg/list_parameters_result.hpp"
27 #include "rcl_interfaces/msg/parameter_descriptor.hpp"
28 #include "rcl_interfaces/msg/parameter_event.hpp"
29 #include "rcl_interfaces/msg/set_parameters_result.hpp"
30 
31 #include "rclcpp/macros.hpp"
32 #include "rclcpp/node_interfaces/node_base_interface.hpp"
33 #include "rclcpp/node_interfaces/node_logging_interface.hpp"
34 #include "rclcpp/node_interfaces/node_parameters_interface.hpp"
35 #include "rclcpp/node_interfaces/node_services_interface.hpp"
36 #include "rclcpp/node_interfaces/node_topics_interface.hpp"
37 #include "rclcpp/parameter.hpp"
38 #include "rclcpp/parameter_service.hpp"
39 #include "rclcpp/publisher.hpp"
40 #include "rclcpp/visibility_control.hpp"
41 
42 namespace rclcpp
43 {
44 namespace node_interfaces
45 {
46 
47 // Internal struct for holding useful info about parameters
49 {
52 
54  rcl_interfaces::msg::ParameterDescriptor descriptor;
55 };
56 
57 // Internal RAII-style guard for mutation recursion
59 {
60 public:
61  explicit ParameterMutationRecursionGuard(bool & allow_mod)
62  : allow_modification_(allow_mod)
63  {
64  if (!allow_modification_) {
66  "cannot set or declare a parameter, or change the callback from within set callback");
67  }
68 
69  allow_modification_ = false;
70  }
71 
73  {
74  allow_modification_ = true;
75  }
76 
77 private:
78  bool & allow_modification_;
79 };
80 
83 {
84 public:
85  RCLCPP_SMART_PTR_ALIASES_ONLY(NodeParameters)
86 
87  RCLCPP_PUBLIC
89  const node_interfaces::NodeBaseInterface::SharedPtr node_base,
90  const node_interfaces::NodeLoggingInterface::SharedPtr node_logging,
91  const node_interfaces::NodeTopicsInterface::SharedPtr node_topics,
92  const node_interfaces::NodeServicesInterface::SharedPtr node_services,
93  const node_interfaces::NodeClockInterface::SharedPtr node_clock,
94  const std::vector<Parameter> & parameter_overrides,
95  bool start_parameter_services,
96  bool start_parameter_event_publisher,
97  const rclcpp::QoS & parameter_event_qos,
98  const rclcpp::PublisherOptionsBase & parameter_event_publisher_options,
99  bool allow_undeclared_parameters,
100  bool automatically_declare_parameters_from_overrides);
101 
102  RCLCPP_PUBLIC
103  virtual
104  ~NodeParameters();
105 
106  RCLCPP_PUBLIC
107  const rclcpp::ParameterValue &
109  const std::string & name,
110  const rclcpp::ParameterValue & default_value,
111  const rcl_interfaces::msg::ParameterDescriptor & parameter_descriptor =
112  rcl_interfaces::msg::ParameterDescriptor{},
113  bool ignore_override = false) override;
114 
115  RCLCPP_PUBLIC
116  const rclcpp::ParameterValue &
118  const std::string & name,
119  rclcpp::ParameterType type,
120  const rcl_interfaces::msg::ParameterDescriptor & parameter_descriptor =
121  rcl_interfaces::msg::ParameterDescriptor(),
122  bool ignore_override = false) override;
123 
124  RCLCPP_PUBLIC
125  void
126  undeclare_parameter(const std::string & name) override;
127 
128  RCLCPP_PUBLIC
129  bool
130  has_parameter(const std::string & name) const override;
131 
132  RCLCPP_PUBLIC
133  std::vector<rcl_interfaces::msg::SetParametersResult>
135  const std::vector<rclcpp::Parameter> & parameters) override;
136 
137  RCLCPP_PUBLIC
138  rcl_interfaces::msg::SetParametersResult
140  const std::vector<rclcpp::Parameter> & parameters) override;
141 
142  RCLCPP_PUBLIC
143  std::vector<rclcpp::Parameter>
144  get_parameters(const std::vector<std::string> & names) const override;
145 
146  RCLCPP_PUBLIC
148  get_parameter(const std::string & name) const override;
149 
150  RCLCPP_PUBLIC
151  bool
153  const std::string & name,
154  rclcpp::Parameter & parameter) const override;
155 
156  RCLCPP_PUBLIC
157  bool
159  const std::string & prefix,
160  std::map<std::string, rclcpp::Parameter> & parameters) const override;
161 
162  RCLCPP_PUBLIC
163  std::vector<rcl_interfaces::msg::ParameterDescriptor>
164  describe_parameters(const std::vector<std::string> & names) const override;
165 
166  RCLCPP_PUBLIC
167  std::vector<uint8_t>
168  get_parameter_types(const std::vector<std::string> & names) const override;
169 
170  RCLCPP_PUBLIC
171  rcl_interfaces::msg::ListParametersResult
172  list_parameters(const std::vector<std::string> & prefixes, uint64_t depth) const override;
173 
174  RCLCPP_PUBLIC
175  RCUTILS_WARN_UNUSED
176  OnSetParametersCallbackHandle::SharedPtr
177  add_on_set_parameters_callback(OnParametersSetCallbackType callback) override;
178 
179  RCLCPP_PUBLIC
180  void
182 
183  RCLCPP_PUBLIC
184  const std::map<std::string, rclcpp::ParameterValue> &
185  get_parameter_overrides() const override;
186 
187  using CallbacksContainerType = std::list<OnSetParametersCallbackHandle::WeakPtr>;
188 
189 private:
190  RCLCPP_DISABLE_COPY(NodeParameters)
191 
192  mutable std::recursive_mutex mutex_;
193 
194  // There are times when we don't want to allow modifications to parameters
195  // (particularly when a set_parameter callback tries to call set_parameter,
196  // declare_parameter, etc). In those cases, this will be set to false.
197  bool parameter_modification_enabled_{true};
198 
199  OnParametersSetCallbackType on_parameters_set_callback_ = nullptr;
200 
201  CallbacksContainerType on_parameters_set_callback_container_;
202 
203  std::map<std::string, ParameterInfo> parameters_;
204 
205  std::map<std::string, rclcpp::ParameterValue> parameter_overrides_;
206 
207  bool allow_undeclared_ = false;
208 
210 
211  std::shared_ptr<ParameterService> parameter_service_;
212 
213  std::string combined_name_;
214 
215  node_interfaces::NodeLoggingInterface::SharedPtr node_logging_;
216  node_interfaces::NodeClockInterface::SharedPtr node_clock_;
217 };
218 
219 } // namespace node_interfaces
220 } // namespace rclcpp
221 
222 #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:78
Encapsulation of Quality of Service settings.
Definition: qos.hpp:111
Thrown if parameter is modified while in a set callback.
Definition: exceptions.hpp:299
Pure virtual interface class for the NodeParameters part of the Node API.
Implementation of the NodeParameters part of the Node API.
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 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 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 RCUTILS_WARN_UNUSED OnSetParametersCallbackHandle::SharedPtr add_on_set_parameters_callback(OnParametersSetCallbackType callback) override
Add a callback for when parameters are being set.
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 rclcpp::Parameter get_parameter(const std::string &name) const override
Get the description of one parameter given a name.
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.