Ariles
Loading...
Searching...
No Matches
declarator.cpp
Go to the documentation of this file.
1/**
2 @file
3 @author Alexander Sherikov
4
5 @copyright 2018 Alexander Sherikov, Licensed under the Apache License, Version 2.0.
6 (see @ref LICENSE or http://www.apache.org/licenses/LICENSE-2.0)
7
8 @brief
9*/
10
11
12#include <variant>
13#include <boost/lexical_cast.hpp>
14
16
17#include "modifier.h"
18
19
20namespace ariles2
21{
22 namespace ns_ros2param
23 {
24 namespace impl
25 {
27 {
28 public:
30
31 [[nodiscard]] bool publishParameters() const
32 {
33 for (const rclcpp::Parameter &parameter : parameters_)
34 {
35 if (not nh_->has_parameter(parameter.get_name()))
36 {
37 const rclcpp::ParameterValue &declared_value =
38 nh_->declare_parameter(parameter.get_name(), parameter.get_parameter_value());
39
41 declared_value.get_type() == parameter.get_type(),
42 std::string("Parameter type mismatch: ") + parameter.get_name());
43 }
44
45 // https://github.com/ros2/rclcpp/blob/master/rclcpp/src/rclcpp/node_interfaces/node_parameters.cpp#L652
46 // "cannot undeclare a statically typed parameter"
47 // nh_->undeclare_parameter(parameter.get_name());
48 }
49 return (true);
50 }
51 };
52 } // namespace impl
53 } // namespace ns_ros2param
54} // namespace ariles2
55
56
57namespace ariles2
58{
59 namespace ns_ros2param
60 {
61 Declarator::Declarator(const rclcpp::node_interfaces::NodeParametersInterface::SharedPtr &nh)
62 {
63 makeImplPtr(nh);
64 }
65
66
68 {
70 CPPUT_ASSERT(impl_->publishParameters(), "Failed to declare parameters.");
71 }
72
73
74 void Declarator::startMapEntry(const std::string &child_name)
75 {
77 CPPUT_TRACE_VALUE(child_name);
78 if (impl_->empty())
79 {
80 impl_->emplace(child_name);
81 }
82 else
83 {
84 if (impl_->back().isArray())
85 {
86 impl_->concatWithNodeAndEmplace(
87 impl_->separator_,
88 boost::lexical_cast<std::string>(impl_->back().index_),
89 impl_->separator_,
90 child_name);
91 }
92 else
93 {
94 impl_->concatWithNodeAndEmplace(impl_->separator_, child_name);
95 }
96 }
97 }
98
100 {
102 impl_->pop();
103 }
104
105
106 void Declarator::startArray(const std::size_t size, const bool /*compact*/)
107 {
109 if (impl_->back().isArray())
110 {
111 impl_->emplace(
112 impl_->concatWithNode(
113 impl_->separator_, boost::lexical_cast<std::string>(impl_->back().index_)),
114 /*index=*/0,
115 size);
116 }
117 else
118 {
119 impl_->emplace(impl_->back().node_, /*index=*/0, size);
120 }
121 }
122
124 {
126 CPPUT_ASSERT(not impl_->back().isCompleted(), "Internal error: array has more elements than expected.");
127 }
128
130 {
132 impl_->shiftArray();
133 }
134
136 {
138 impl_->setParameter();
139 impl_->pop();
140 }
141
142
143 void Declarator::writeElement(const unsigned char &element, const Parameters &)
144 {
146 if (not impl_->back().tryPushArray<uint8_t>(element))
147 {
148 impl_->setParameter(static_cast<int64_t>(element));
149 }
150 }
151
152
153 void Declarator::writeElement(const float &element, const Parameters &)
154 {
156 if (not impl_->back().tryPushArray<double>(element))
157 {
158 impl_->setParameter(static_cast<double>(element));
159 }
160 }
161
162
163#define ARILES2_ROS2PARAM_NATIVE_TYPES_LIST \
164 ARILES2_BASIC_TYPE(bool) \
165 ARILES2_BASIC_TYPE(double) \
166 ARILES2_BASIC_TYPE(std::string)
167
168
169#define ARILES2_BASIC_TYPE(type) \
170 void Declarator::writeElement(const type &element, const Parameters &) \
171 { \
172 CPPUT_TRACE_FUNCTION; \
173 if (not impl_->back().tryPushArray(element)) \
174 { \
175 impl_->setParameter(element); \
176 } \
177 }
178
180
181#undef ARILES2_BASIC_TYPE
182
183
184#define ARILES2_BASIC_TYPE(type) \
185 void Declarator::writeElement(const type &element, const Parameters &) \
186 { \
187 CPPUT_TRACE_FUNCTION; \
188 if (not impl_->back().tryPushArray<int64_t>(element)) \
189 { \
190 impl_->setParameter(static_cast<int64_t>(element)); \
191 } \
192 }
193
195
196#undef ARILES2_BASIC_TYPE
197
198
199#define ARILES2_BASIC_TYPE(type) \
200 void Declarator::writeElement(const type &element, const Parameters &) \
201 { \
202 CPPUT_TRACE_FUNCTION; \
203 CPPUT_ASSERT( \
204 static_cast<uint64_t>(element) <= static_cast<uint64_t>(std::numeric_limits<int64_t>::max()), \
205 "Value is too large."); \
206 if (not impl_->back().tryPushArray<int64_t>(element)) \
207 { \
208 impl_->setParameter(static_cast<int64_t>(element)); \
209 } \
210 }
211
213
214#undef ARILES2_BASIC_TYPE
215 } // namespace ns_ros2param
216} // namespace ariles2
void flush()
Flush the configuration to the output.
void startMapEntry(const std::string &child_name)
Starts a nested map in the configuration file.
Declarator(const rclcpp::node_interfaces::NodeParametersInterface::SharedPtr &nh)
void startArray(const std::size_t size, const bool=false)
rclcpp::node_interfaces::NodeParametersInterface::SharedPtr nh_
Definition modifier.h:22
std::vector< rclcpp::Parameter > parameters_
Definition modifier.h:24
ModifierImplBase(const rclcpp::node_interfaces::NodeParametersInterface::SharedPtr &nh)
Definition modifier.h:29
void writeElement(const std::complex< t_Scalar > &entry, const Parameters &param)
Definition write.h:264
#define ARILES2_ROS2PARAM_NATIVE_TYPES_LIST
#define CPPUT_ASSERT(condition,...)
Definition exception.h:32
#define ARILES2_BASIC_UNSIGNED_INTEGER_TYPES_LIST_WITHOUT_BYTE
Definition helpers.h:44
#define ARILES2_BASIC_SIGNED_INTEGER_TYPES_LIST
Definition helpers.h:37
#define CPPUT_MACRO_SUBSTITUTE(macro)
Definition misc.h:21
#define CPPUT_TRACE_FUNCTION
Definition trace.h:126
#define CPPUT_TRACE_VALUE(value)
Definition trace.h:127