rclcpp  master
C++ ROS Client Library API
node_parameters.hpp
Go to the documentation of this file.
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"
37 #include "rclcpp/parameter.hpp"
39 #include "rclcpp/publisher.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:
86 
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 
103  virtual
104  ~NodeParameters();
105 
107  const rclcpp::ParameterValue &
109  const std::string & name,
110  const rclcpp::ParameterValue & default_value,
111  const rcl_interfaces::msg::ParameterDescriptor & parameter_descriptor,
112  bool ignore_override) override;
113 
115  void
116  undeclare_parameter(const std::string & name) override;
117 
119  bool
120  has_parameter(const std::string & name) const override;
121 
125  const std::vector<rclcpp::Parameter> & parameters) override;
126 
128  rcl_interfaces::msg::SetParametersResult
130  const std::vector<rclcpp::Parameter> & parameters) override;
131 
134  get_parameters(const std::vector<std::string> & names) const override;
135 
138  get_parameter(const std::string & name) const override;
139 
141  bool
143  const std::string & name,
144  rclcpp::Parameter & parameter) const override;
145 
147  bool
149  const std::string & prefix,
150  std::map<std::string, rclcpp::Parameter> & parameters) const override;
151 
154  describe_parameters(const std::vector<std::string> & names) const override;
155 
158  get_parameter_types(const std::vector<std::string> & names) const override;
159 
161  rcl_interfaces::msg::ListParametersResult
162  list_parameters(const std::vector<std::string> & prefixes, uint64_t depth) const override;
163 
166  OnSetParametersCallbackHandle::SharedPtr
168 
170  void
172 
173  [[deprecated("use add_on_set_parameters_callback(OnParametersSetCallbackType callback) instead")]]
177 
180  get_parameter_overrides() const override;
181 
183 
184 private:
186 
187  mutable std::recursive_mutex mutex_;
188 
189  // There are times when we don't want to allow modifications to parameters
190  // (particularly when a set_parameter callback tries to call set_parameter,
191  // declare_parameter, etc). In those cases, this will be set to false.
192  bool parameter_modification_enabled_{true};
193 
194  OnParametersSetCallbackType on_parameters_set_callback_ = nullptr;
195 
196  CallbacksContainerType on_parameters_set_callback_container_;
197 
199 
201 
202  bool allow_undeclared_ = false;
203 
205 
206  std::shared_ptr<ParameterService> parameter_service_;
207 
208  std::string combined_name_;
209 
210  node_interfaces::NodeLoggingInterface::SharedPtr node_logging_;
211  node_interfaces::NodeClockInterface::SharedPtr node_clock_;
212 };
213 
214 } // namespace node_interfaces
215 } // namespace rclcpp
216 
217 #endif // RCLCPP__NODE_INTERFACES__NODE_PARAMETERS_HPP_
rclcpp::ParameterValue
Store the type and value of a parameter.
Definition: parameter_value.hpp:71
rclcpp::Publisher
A publisher publishes messages of any type to a topic.
Definition: publisher.hpp:51
std::string
std::shared_ptr
rclcpp::node_interfaces::ParameterInfo::value
rclcpp::ParameterValue value
Current value of the parameter.
Definition: node_parameters.hpp:51
std::list< OnSetParametersCallbackHandle::WeakPtr >
RCLCPP_DISABLE_COPY
#define RCLCPP_DISABLE_COPY(...)
Definition: macros.hpp:26
rclcpp::node_interfaces::ParameterMutationRecursionGuard::ParameterMutationRecursionGuard
ParameterMutationRecursionGuard(bool &allow_mod)
Definition: node_parameters.hpp:61
rclcpp::node_interfaces::ParameterInfo
Definition: node_parameters.hpp:48
rclcpp::node_interfaces::ParameterInfo::descriptor
rcl_interfaces::msg::ParameterDescriptor descriptor
A description of the parameter.
Definition: node_parameters.hpp:54
std::vector
std::recursive_mutex
RCLCPP_SMART_PTR_ALIASES_ONLY
#define RCLCPP_SMART_PTR_ALIASES_ONLY(...)
Definition: macros.hpp:66
rclcpp::PublisherOptionsBase
Non-templated part of PublisherOptionsWithAllocator<Allocator>.
Definition: publisher_options.hpp:36
rclcpp::node_interfaces::NodeParameters
Implementation of the NodeParameters part of the Node API.
Definition: node_parameters.hpp:82
rclcpp::node_interfaces::NodeParameters::set_on_parameters_set_callback
OnParametersSetCallbackType set_on_parameters_set_callback(OnParametersSetCallbackType callback) override
Register a callback for when parameters are being set, return an existing one.
std::function< rcl_interfaces::msg::SetParametersResult(const std::vector< rclcpp::Parameter > &)>
rclcpp
This header provides the get_node_base_interface() template function.
Definition: allocator_common.hpp:24
publisher.hpp
rclcpp::node_interfaces::NodeParameters::get_parameter_overrides
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.
node_topics_interface.hpp
RCLCPP_PUBLIC
#define RCLCPP_PUBLIC
Definition: visibility_control.hpp:50
rclcpp::node_interfaces::OnSetParametersCallbackHandle
Definition: node_parameters_interface.hpp:36
rclcpp::QoS
Encapsulation of Quality of Service settings.
Definition: qos.hpp:59
rclcpp::node_interfaces::NodeParameters::undeclare_parameter
void undeclare_parameter(const std::string &name) override
Undeclare a parameter.
rclcpp::node_interfaces::NodeParameters::get_parameter_types
std::vector< uint8_t > get_parameter_types(const std::vector< std::string > &names) const override
macros.hpp
rclcpp::node_interfaces::NodeParameters::set_parameters
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::node_interfaces::NodeParametersInterface
Pure virtual interface class for the NodeParameters part of the Node API.
Definition: node_parameters_interface.hpp:49
node_base_interface.hpp
rclcpp::node_interfaces::NodeParameters::remove_on_set_parameters_callback
void remove_on_set_parameters_callback(const OnSetParametersCallbackHandle *const handler) override
Remove a callback registered with add_on_set_parameters_callback.
node_services_interface.hpp
rclcpp::exceptions::ParameterModifiedInCallbackException
Thrown if parameter is modified while in a set callback.
Definition: exceptions.hpp:270
rclcpp::node_interfaces::NodeParameters::describe_parameters
std::vector< rcl_interfaces::msg::ParameterDescriptor > describe_parameters(const std::vector< std::string > &names) const override
RCUTILS_WARN_UNUSED
#define RCUTILS_WARN_UNUSED
std::map
parameter.hpp
rclcpp::node_interfaces::ParameterMutationRecursionGuard
Definition: node_parameters.hpp:58
rclcpp::node_interfaces::NodeParameters::get_parameters
std::vector< rclcpp::Parameter > get_parameters(const std::vector< std::string > &names) const override
Get descriptions of parameters given their names.
rclcpp::node_interfaces::NodeParameters::set_parameters_atomically
rcl_interfaces::msg::SetParametersResult set_parameters_atomically(const std::vector< rclcpp::Parameter > &parameters) override
Set and initialize a parameter, all at once.
visibility_control.hpp
rclcpp::node_interfaces::NodeParameters::list_parameters
rcl_interfaces::msg::ListParametersResult list_parameters(const std::vector< std::string > &prefixes, uint64_t depth) const override
rclcpp::node_interfaces::NodeParameters::declare_parameter
const rclcpp::ParameterValue & declare_parameter(const std::string &name, const rclcpp::ParameterValue &default_value, const rcl_interfaces::msg::ParameterDescriptor &parameter_descriptor, bool ignore_override) override
Declare and initialize a parameter.
rclcpp::node_interfaces::NodeParameters::~NodeParameters
virtual ~NodeParameters()
rclcpp::node_interfaces::NodeParameters::get_parameter
rclcpp::Parameter get_parameter(const std::string &name) const override
Get the description of one parameter given a name.
rclcpp::Parameter
Structure to store an arbitrary parameter with templated get/set methods.
Definition: parameter.hpp:52
rclcpp::node_interfaces::NodeParameters::CallbacksContainerType
std::list< OnSetParametersCallbackHandle::WeakPtr > CallbacksContainerType
Definition: node_parameters.hpp:182
rclcpp::node_interfaces::NodeParameters::get_parameters_by_prefix
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::node_interfaces::NodeParameters::add_on_set_parameters_callback
RCUTILS_WARN_UNUSED OnSetParametersCallbackHandle::SharedPtr add_on_set_parameters_callback(OnParametersSetCallbackType callback) override
Add a callback for when parameters are being set.
macros.h
rclcpp::node_interfaces::ParameterMutationRecursionGuard::~ParameterMutationRecursionGuard
~ParameterMutationRecursionGuard()
Definition: node_parameters.hpp:72
rclcpp::node_interfaces::NodeParameters::has_parameter
bool has_parameter(const std::string &name) const override
Return true if the parameter has been declared, otherwise false.
rclcpp::node_interfaces::NodeParametersInterface::OnParametersSetCallbackType
OnSetParametersCallbackHandle::OnParametersSetCallbackType OnParametersSetCallbackType
Definition: node_parameters_interface.hpp:174
node_parameters_interface.hpp
parameter_service.hpp
node_logging_interface.hpp
rclcpp::node_interfaces::NodeParameters::NodeParameters
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)