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 
106 // This is overriding a deprecated method, so we need to ignore the deprecation warning here.
107 // Users of the method will still get a warning!
108 #ifndef _WIN32
109 # pragma GCC diagnostic push
110 # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
111 #else
112 # pragma warning(push)
113 # pragma warning(disable: 4996)
114 #endif
117  const rclcpp::ParameterValue &
118  declare_parameter(const std::string & name) override;
119 #ifndef _WIN32
120 # pragma GCC diagnostic pop
121 #else
122 # pragma warning(pop)
123 #endif
124 
126  const rclcpp::ParameterValue &
128  const std::string & name,
129  const rclcpp::ParameterValue & default_value,
130  const rcl_interfaces::msg::ParameterDescriptor & parameter_descriptor =
131  rcl_interfaces::msg::ParameterDescriptor{},
132  bool ignore_override = false) override;
133 
135  const rclcpp::ParameterValue &
137  const std::string & name,
139  const rcl_interfaces::msg::ParameterDescriptor & parameter_descriptor =
140  rcl_interfaces::msg::ParameterDescriptor(),
141  bool ignore_override = false) override;
142 
144  void
145  undeclare_parameter(const std::string & name) override;
146 
148  bool
149  has_parameter(const std::string & name) const override;
150 
154  const std::vector<rclcpp::Parameter> & parameters) override;
155 
157  rcl_interfaces::msg::SetParametersResult
159  const std::vector<rclcpp::Parameter> & parameters) override;
160 
163  get_parameters(const std::vector<std::string> & names) const override;
164 
167  get_parameter(const std::string & name) const override;
168 
170  bool
172  const std::string & name,
173  rclcpp::Parameter & parameter) const override;
174 
176  bool
178  const std::string & prefix,
179  std::map<std::string, rclcpp::Parameter> & parameters) const override;
180 
183  describe_parameters(const std::vector<std::string> & names) const override;
184 
187  get_parameter_types(const std::vector<std::string> & names) const override;
188 
190  rcl_interfaces::msg::ListParametersResult
191  list_parameters(const std::vector<std::string> & prefixes, uint64_t depth) const override;
192 
195  OnSetParametersCallbackHandle::SharedPtr
197 
199  void
201 
204  get_parameter_overrides() const override;
205 
207 
208 private:
210 
211  mutable std::recursive_mutex mutex_;
212 
213  // There are times when we don't want to allow modifications to parameters
214  // (particularly when a set_parameter callback tries to call set_parameter,
215  // declare_parameter, etc). In those cases, this will be set to false.
216  bool parameter_modification_enabled_{true};
217 
218  OnParametersSetCallbackType on_parameters_set_callback_ = nullptr;
219 
220  CallbacksContainerType on_parameters_set_callback_container_;
221 
223 
225 
226  bool allow_undeclared_ = false;
227 
229 
230  std::shared_ptr<ParameterService> parameter_service_;
231 
232  std::string combined_name_;
233 
234  node_interfaces::NodeLoggingInterface::SharedPtr node_logging_;
235  node_interfaces::NodeClockInterface::SharedPtr node_clock_;
236 };
237 
238 } // namespace node_interfaces
239 } // namespace rclcpp
240 
241 #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:53
std::string
std::shared_ptr
RCLCPP_INTERNAL_NODE_PARAMETERS_INTERFACE_DEPRECATE_DECLARE
#define RCLCPP_INTERNAL_NODE_PARAMETERS_INTERFACE_DEPRECATE_DECLARE
Definition: node_parameters_interface.hpp:48
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:37
rclcpp::node_interfaces::NodeParameters
Implementation of the NodeParameters part of the Node API.
Definition: node_parameters.hpp:82
std::function< rcl_interfaces::msg::SetParametersResult(const std::vector< rclcpp::Parameter > &)>
rclcpp::node_interfaces::NodeParameters::declare_parameter
const rclcpp::ParameterValue & declare_parameter(const std::string &name) override
Declare a 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:110
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:60
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:279
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
rclcpp::ParameterType
ParameterType
Definition: parameter_value.hpp:32
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 one or more parameters, 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::~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:206
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:208
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)