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 "rcl_interfaces/msg/list_parameters_result.hpp"
25 #include "rcl_interfaces/msg/parameter_descriptor.hpp"
26 #include "rcl_interfaces/msg/parameter_event.hpp"
27 #include "rcl_interfaces/msg/set_parameters_result.hpp"
28 
29 #include "rclcpp/macros.hpp"
34 #include "rclcpp/parameter.hpp"
36 #include "rclcpp/publisher.hpp"
38 
39 namespace rclcpp
40 {
41 namespace node_interfaces
42 {
43 
44 // Internal struct for holding useful info about parameters
46 {
49 
51  rcl_interfaces::msg::ParameterDescriptor descriptor;
52 };
53 
54 // Internal RAII-style guard for mutation recursion
56 {
57 public:
58  explicit ParameterMutationRecursionGuard(bool & allow_mod)
59  : allow_modification_(allow_mod)
60  {
61  if (!allow_modification_) {
63  "cannot set or declare a parameter, or change the callback from within set callback");
64  }
65 
66  allow_modification_ = false;
67  }
68 
70  {
71  allow_modification_ = true;
72  }
73 
74 private:
75  bool & allow_modification_;
76 };
77 
80 {
81 public:
83 
86  const node_interfaces::NodeBaseInterface::SharedPtr node_base,
87  const node_interfaces::NodeLoggingInterface::SharedPtr node_logging,
88  const node_interfaces::NodeTopicsInterface::SharedPtr node_topics,
89  const node_interfaces::NodeServicesInterface::SharedPtr node_services,
90  const node_interfaces::NodeClockInterface::SharedPtr node_clock,
91  const std::vector<Parameter> & parameter_overrides,
92  bool start_parameter_services,
93  bool start_parameter_event_publisher,
94  const rclcpp::QoS & parameter_event_qos,
95  const rclcpp::PublisherOptionsBase & parameter_event_publisher_options,
96  bool allow_undeclared_parameters,
97  bool automatically_declare_parameters_from_overrides);
98 
100  virtual
101  ~NodeParameters();
102 
104  const rclcpp::ParameterValue &
105  declare_parameter(
106  const std::string & name,
107  const rclcpp::ParameterValue & default_value,
108  const rcl_interfaces::msg::ParameterDescriptor & parameter_descriptor,
109  bool ignore_override) override;
110 
112  void
113  undeclare_parameter(const std::string & name) override;
114 
116  bool
117  has_parameter(const std::string & name) const override;
118 
121  set_parameters(
122  const std::vector<rclcpp::Parameter> & parameters) override;
123 
125  rcl_interfaces::msg::SetParametersResult
126  set_parameters_atomically(
127  const std::vector<rclcpp::Parameter> & parameters) override;
128 
131  get_parameters(const std::vector<std::string> & names) const override;
132 
135  get_parameter(const std::string & name) const override;
136 
138  bool
139  get_parameter(
140  const std::string & name,
141  rclcpp::Parameter & parameter) const override;
142 
144  bool
145  get_parameters_by_prefix(
146  const std::string & prefix,
147  std::map<std::string, rclcpp::Parameter> & parameters) const override;
148 
151  describe_parameters(const std::vector<std::string> & names) const override;
152 
155  get_parameter_types(const std::vector<std::string> & names) const override;
156 
158  rcl_interfaces::msg::ListParametersResult
159  list_parameters(const std::vector<std::string> & prefixes, uint64_t depth) const override;
160 
163  add_on_set_parameters_callback(OnParametersSetCallbackType callback) override;
164 
166  void
167  remove_on_set_parameters_callback(const OnSetParametersCallbackHandle * const handler) override;
168 
171  set_on_parameters_set_callback(OnParametersSetCallbackType callback) override;
172 
175  get_parameter_overrides() const override;
176 
178 
179 private:
180  RCLCPP_DISABLE_COPY(NodeParameters)
181 
182  mutable std::recursive_mutex mutex_;
183 
184  // There are times when we don't want to allow modifications to parameters
185  // (particularly when a set_parameter callback tries to call set_parameter,
186  // declare_parameter, etc). In those cases, this will be set to false.
187  bool parameter_modification_enabled_{true};
188 
189  OnParametersSetCallbackType on_parameters_set_callback_ = nullptr;
190 
191  CallbacksContainerType on_parameters_set_callback_container_;
192 
194 
196 
197  bool allow_undeclared_ = false;
198 
200 
201  std::shared_ptr<ParameterService> parameter_service_;
202 
203  std::string combined_name_;
204 
205  node_interfaces::NodeLoggingInterface::SharedPtr node_logging_;
206  node_interfaces::NodeClockInterface::SharedPtr node_clock_;
207 };
208 
209 } // namespace node_interfaces
210 } // namespace rclcpp
211 
212 #endif // RCLCPP__NODE_INTERFACES__NODE_PARAMETERS_HPP_
#define RCLCPP_DISABLE_COPY(...)
Definition: macros.hpp:26
Non-templated part of PublisherOptionsWithAllocator<Allocator>.
Definition: publisher_options.hpp:39
rcl_interfaces::msg::ParameterDescriptor descriptor
A description of the parameter.
Definition: node_parameters.hpp:51
Encapsulation of Quality of Service settings.
Definition: qos.hpp:55
Structure to store an arbitrary parameter with templated get/set methods.
Definition: parameter.hpp:51
#define RCLCPP_SMART_PTR_ALIASES_ONLY(...)
Definition: macros.hpp:66
This header provides the get_node_base_interface() template function.
Definition: allocator_common.hpp:24
ParameterMutationRecursionGuard(bool &allow_mod)
Definition: node_parameters.hpp:58
rclcpp::ParameterValue value
Current value of the parameter.
Definition: node_parameters.hpp:48
A publisher publishes messages of any type to a topic.
Definition: publisher.hpp:51
Definition: node_parameters.hpp:45
Definition: node_parameters_interface.hpp:36
Implementation of the NodeParameters part of the Node API.
Definition: node_parameters.hpp:79
#define RCLCPP_PUBLIC
Definition: visibility_control.hpp:50
Set the data type used in the intra-process buffer as std::shared_ptr<MessageT>
~ParameterMutationRecursionGuard()
Definition: node_parameters.hpp:69
Store the type and value of a parameter.
Definition: parameter_value.hpp:71
Pure virtual interface class for the NodeParameters part of the Node API.
Definition: node_parameters_interface.hpp:49
Thrown if parameter is modified while in a set callback.
Definition: exceptions.hpp:251