Ariles
writer.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 
20 namespace ariles2
21 {
22  namespace ns_ros2param
23  {
24  namespace impl
25  {
26  class Writer : public ModifierImplBase
27  {
28  public:
30 
31  [[nodiscard]] bool publishParameters() const
32  {
33  if (parameters_.empty())
34  {
35  return (true);
36  }
37  return (nh_->set_parameters_atomically(parameters_).successful);
38  }
39  };
40  } // namespace impl
41  } // namespace ns_ros2param
42 } // namespace ariles2
43 
44 
45 namespace ariles2
46 {
47  namespace ns_ros2param
48  {
49  Writer::Writer(const rclcpp::node_interfaces::NodeParametersInterface::SharedPtr &nh)
50  {
51  makeImplPtr(nh);
52  }
53 
54 
56  {
58  CPPUT_ASSERT(impl_->publishParameters(), "Failed to set parameters.");
59  }
60 
61 
62  void Writer::startMapEntry(const std::string &child_name)
63  {
65  CPPUT_TRACE_VALUE(child_name);
66  if (impl_->empty())
67  {
68  impl_->emplace(child_name);
69  }
70  else
71  {
72  if (impl_->back().isArray())
73  {
74  impl_->concatWithNodeAndEmplace(
75  impl_->separator_,
76  boost::lexical_cast<std::string>(impl_->back().index_),
77  impl_->separator_,
78  child_name);
79  }
80  else
81  {
82  impl_->concatWithNodeAndEmplace(impl_->separator_, child_name);
83  }
84  }
85  }
86 
88  {
90  impl_->pop();
91  }
92 
93 
94  void Writer::startArray(const std::size_t size, const bool /*compact*/)
95  {
97  if (impl_->back().isArray())
98  {
99  impl_->emplace(
100  impl_->concatWithNode(
101  impl_->separator_, boost::lexical_cast<std::string>(impl_->back().index_)),
102  /*index=*/0,
103  size);
104  }
105  else
106  {
107  impl_->emplace(impl_->back().node_, /*index=*/0, size);
108  }
109  }
110 
112  {
114  CPPUT_ASSERT(not impl_->back().isCompleted(), "Internal error: array has more elements than expected.");
115  }
116 
118  {
120  impl_->shiftArray();
121  }
122 
124  {
126  impl_->setParameter();
127  impl_->pop();
128  }
129 
130 
131  void Writer::writeElement(const unsigned char &element, const Parameters &)
132  {
134  if (not impl_->back().tryPushArray<uint8_t>(element))
135  {
136  impl_->setParameter(static_cast<int64_t>(element));
137  }
138  }
139 
140 
141  void Writer::writeElement(const float &element, const Parameters &)
142  {
144  if (not impl_->back().tryPushArray<double>(element))
145  {
146  impl_->setParameter(static_cast<double>(element));
147  }
148  }
149 
150 
151 #define ARILES2_ROS2PARAM_NATIVE_TYPES_LIST \
152  ARILES2_BASIC_TYPE(bool) \
153  ARILES2_BASIC_TYPE(double) \
154  ARILES2_BASIC_TYPE(std::string)
155 
156 
157 #define ARILES2_BASIC_TYPE(type) \
158  void Writer::writeElement(const type &element, const Parameters &) \
159  { \
160  CPPUT_TRACE_FUNCTION; \
161  if (not impl_->back().tryPushArray(element)) \
162  { \
163  impl_->setParameter(element); \
164  } \
165  }
166 
168 
169 #undef ARILES2_BASIC_TYPE
170 
171 
172 #define ARILES2_BASIC_TYPE(type) \
173  void Writer::writeElement(const type &element, const Parameters &) \
174  { \
175  CPPUT_TRACE_FUNCTION; \
176  if (not impl_->back().tryPushArray<int64_t>(element)) \
177  { \
178  impl_->setParameter(static_cast<int64_t>(element)); \
179  } \
180  }
181 
183 
184 #undef ARILES2_BASIC_TYPE
185 
186 
187 #define ARILES2_BASIC_TYPE(type) \
188  void Writer::writeElement(const type &element, const Parameters &) \
189  { \
190  CPPUT_TRACE_FUNCTION; \
191  CPPUT_ASSERT( \
192  static_cast<uint64_t>(element) <= static_cast<uint64_t>(std::numeric_limits<int64_t>::max()), \
193  "Value is too large."); \
194  if (not impl_->back().tryPushArray<int64_t>(element)) \
195  { \
196  impl_->setParameter(static_cast<int64_t>(element)); \
197  } \
198  }
199 
201 
202 #undef ARILES2_BASIC_TYPE
203  } // namespace ns_ros2param
204 } // namespace ariles2
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
Writer(const rclcpp::node_interfaces::NodeParametersInterface::SharedPtr &nh)
Definition: writer.cpp:49
void flush()
Flush the configuration to the output.
Definition: writer.cpp:55
void startArray(const std::size_t size, const bool=false)
Definition: writer.cpp:94
void startMapEntry(const std::string &child_name)
Starts a nested map in the configuration file.
Definition: writer.cpp:62
void writeElement(const std::complex< t_Scalar > &entry, const Parameters &param)
Definition: write.h:264
#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
visitor::Parameters Parameters
Definition: count.h:26
CPPUT_MACRO_SUBSTITUTE(ARILES2_BASIC_SIGNED_INTEGER_TYPES_LIST) CPPUT_MACRO_SUBSTITUTE(ARILES2_BASIC_UNSIGNED_INTEGER_TYPES_LIST) CPPUT_MACRO_SUBSTITUTE(ARILES2_BASIC_REAL_TYPES_LIST) void Reader
Definition: reader.cpp:417
Definition: basic.h:17
#define ARILES2_ROS2PARAM_NATIVE_TYPES_LIST
Definition: writer.cpp:151
#define CPPUT_TRACE_FUNCTION
Definition: trace.h:126
#define CPPUT_TRACE_VALUE(value)
Definition: trace.h:127