Ariles
reader.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 <boost/lexical_cast.hpp>
13 
14 #include "common.h"
15 
16 
17 namespace ariles2
18 {
19  namespace ns_rosparam
20  {
21  namespace impl
22  {
24  {
25  public:
26  std::vector<XmlRpc::XmlRpcValue::iterator> iterator_stack_;
27 
28  public:
29  explicit Reader(const ::ros::NodeHandle &nh)
30  {
31  nh_ = nh;
32  }
33  };
34  } // namespace impl
35  } // namespace ns_rosparam
36 } // namespace ariles2
37 
38 
39 namespace ariles2
40 {
41  namespace ns_rosparam
42  {
43  Reader::Reader(const ::ros::NodeHandle &nh)
44  {
45  makeImplPtr(nh);
46  }
47 
48 
49  void Reader::startMap(const SizeLimitEnforcementType limit_type, const std::size_t min, const std::size_t max)
50  {
52  if (XmlRpc::XmlRpcValue::TypeStruct == impl_->getRawNode().getType())
53  {
54  checkSize(limit_type, impl_->getRawNode().size(), min, max);
55  }
56  else
57  {
58  CPPUT_PERSISTENT_ASSERT(SIZE_LIMIT_NONE == limit_type or (0 == min and min == max), "Expected struct.");
59  }
60  }
61 
62  bool Reader::startMapEntry(const std::string &child_name)
63  {
64  if (impl_->empty())
65  {
66  impl_->root_name_ = child_name;
67  impl_->nh_.getParam(impl_->root_name_, impl_->root_value_);
68  impl_->emplace(&impl_->root_value_);
69  return (true);
70  }
71 
72  XmlRpc::XmlRpcValue &node = impl_->getRawNode();
73  if ((XmlRpc::XmlRpcValue::TypeStruct == node.getType()) && (node.hasMember(child_name)))
74  {
75  impl_->emplace(&(node[child_name]));
76  return (true);
77  }
78  return (false);
79  }
80 
82  {
83  impl_->pop();
84  }
85 
86 
87 
89  const SizeLimitEnforcementType limit_type,
90  const std::size_t min,
91  const std::size_t max)
92  {
94  if (XmlRpc::XmlRpcValue::TypeStruct == impl_->getRawNode().getType())
95  {
96  checkSize(limit_type, impl_->getRawNode().size(), min, max);
97  impl_->iterator_stack_.push_back(impl_->getRawNode().begin());
98  return (true);
99  }
100  CPPUT_PERSISTENT_ASSERT(0 == min and min == max, "Expected struct.");
101  return (false);
102  }
103 
104  bool Reader::startIteratedMapElement(std::string &entry_name)
105  {
106  if (impl_->iterator_stack_.back() != impl_->getRawNode().end())
107  {
108  impl_->emplace(&impl_->iterator_stack_.back()->second);
109  entry_name = impl_->iterator_stack_.back()->first;
110  return (true);
111  }
112  return (false);
113  }
114 
116  {
117  ++impl_->iterator_stack_.back();
118  impl_->pop();
119  }
120 
122  {
123  CPPUT_ASSERT(
124  impl_->iterator_stack_.back() == impl_->getRawNode().end(),
125  "End of iterated map has not been reached.");
126  impl_->iterator_stack_.pop_back();
127  }
128 
129 
130  std::size_t Reader::startArray()
131  {
132  CPPUT_ASSERT(XmlRpc::XmlRpcValue::TypeArray == impl_->getRawNode().getType(), "Expected array.");
133 
134  std::size_t size = impl_->getRawNode().size();
135  impl_->emplace(0, size);
136 
137  return (size);
138  }
139 
141  {
142  CPPUT_ASSERT(
143  impl_->back().index_ < impl_->back().size_,
144  "Internal error: array has more elements than expected.");
145  }
146 
148  {
149  impl_->shiftArray();
150  }
151 
153  {
154  impl_->pop();
155  }
156 
157 
158  bool Reader::startRoot(const std::string &name)
159  {
161  if (name.empty())
162  {
163  return (startMapEntry("ariles"));
164  }
165  return (startMapEntry(name));
166  }
167 
168  void Reader::endRoot(const std::string & /*name*/)
169  {
171  endMapEntry();
172  }
173 
174 
175 #define ARILES2_BASIC_TYPE(type) \
176  void Reader::readElement(type &element) \
177  { \
178  CPPUT_ASSERT(impl_->getRawNode().getType() == XmlRpc::XmlRpcValue::TypeInt, "Integer type expected."); \
179  int tmp_value = static_cast<int>(impl_->getRawNode()); \
180  CPPUT_ASSERT( \
181  static_cast<int64_t>(tmp_value) <= std::numeric_limits<type>::max() \
182  && static_cast<int64_t>(tmp_value) >= std::numeric_limits<type>::min(), \
183  "Value is out of range."); \
184  element = static_cast<type>(tmp_value); \
185  }
186 
188 
189 #undef ARILES2_BASIC_TYPE
190 
191 
192 #define ARILES2_BASIC_TYPE(type) \
193  void Reader::readElement(type &element) \
194  { \
195  CPPUT_ASSERT(impl_->getRawNode().getType() == XmlRpc::XmlRpcValue::TypeInt, "Integer type expected."); \
196  int tmp_value = static_cast<int>(impl_->getRawNode()); \
197  CPPUT_ASSERT(tmp_value >= 0, "Expected positive value."); \
198  CPPUT_ASSERT(static_cast<uint64_t>(tmp_value) <= std::numeric_limits<type>::max(), "Value is too large."); \
199  element = static_cast<type>(tmp_value); \
200  }
201 
203 
204 #undef ARILES2_BASIC_TYPE
205 
206 
207 #define ARILES2_BASIC_TYPE(type) \
208  void Reader::readElement(type &element) \
209  { \
210  switch (impl_->getRawNode().getType()) \
211  { \
212  case XmlRpc::XmlRpcValue::TypeDouble: \
213  element = static_cast<double>(impl_->getRawNode()); \
214  break; \
215  case XmlRpc::XmlRpcValue::TypeString: \
216  element = boost::lexical_cast<double>(static_cast<std::string>(impl_->getRawNode())); \
217  break; \
218  case XmlRpc::XmlRpcValue::TypeInt: \
219  element = static_cast<int>(impl_->getRawNode()); \
220  break; \
221  default: \
222  CPPUT_THROW("Could not convert value to type."); \
223  break; \
224  } \
225  }
226 
228 
229 #undef ARILES2_BASIC_TYPE
230 
231 
232  void Reader::readElement(std::string &element)
233  {
234  element = static_cast<std::string>(impl_->getRawNode());
235  }
236 
237 
238  void Reader::readElement(bool &element)
239  {
240  switch (impl_->getRawNode().getType())
241  {
242  case XmlRpc::XmlRpcValue::TypeString:
243  element = boost::lexical_cast<bool>(static_cast<std::string>(impl_->getRawNode()));
244  break;
245 
246  case XmlRpc::XmlRpcValue::TypeBoolean:
247  element = static_cast<bool>(impl_->getRawNode());
248  break;
249 
250  case XmlRpc::XmlRpcValue::TypeInt:
251  element = static_cast<int>(impl_->getRawNode()) > 0;
252  break;
253 
254  default:
255  CPPUT_THROW("Could not convert value to boolean.");
256  break;
257  }
258  }
259  } // namespace ns_rosparam
260 } // namespace ariles2
::ros::NodeHandle nh_
Definition: common.h:34
bool startMapEntry(const std::string &child_name)
startMapEntry to the entry with the given name
Definition: reader.cpp:62
bool startIteratedMap(const SizeLimitEnforcementType=SIZE_LIMIT_NONE, const std::size_t=0, const std::size_t=0)
Definition: reader.cpp:88
bool startRoot(const std::string &name)
Definition: reader.cpp:158
void endMapEntry()
endMapEntry from the current entry to its parent.
Definition: reader.cpp:81
void endRoot(const std::string &name)
Definition: reader.cpp:168
bool startIteratedMapElement(std::string &entry_name)
Definition: reader.cpp:104
Reader(const ::ros::NodeHandle &nh)
Constructor.
Definition: reader.cpp:43
std::size_t startArray()
Definition: reader.cpp:130
void startMap(const SizeLimitEnforcementType limit_type=SIZE_LIMIT_NONE, const std::size_t min=0, const std::size_t max=0)
Definition: reader.cpp:49
Reader(const ::ros::NodeHandle &nh)
Definition: reader.cpp:29
std::vector< XmlRpc::XmlRpcValue::iterator > iterator_stack_
Definition: reader.cpp:26
void checkSize(const SizeLimitEnforcementType limit_type, const std::size_t size=0, const std::size_t min=0, const std::size_t max=0) const
Definition: read.h:47
void readElement(std::complex< t_Scalar > &entry)
Definition: read.h:357
#define CPPUT_PERSISTENT_ASSERT(condition,...)
Definition: exception.h:23
#define CPPUT_THROW(...)
Definition: exception.h:19
#define CPPUT_ASSERT(condition,...)
Definition: exception.h:32
#define ARILES2_BASIC_REAL_TYPES_LIST
Definition: helpers.h:59
#define ARILES2_BASIC_UNSIGNED_INTEGER_TYPES_LIST
Definition: helpers.h:50
#define ARILES2_BASIC_SIGNED_INTEGER_TYPES_LIST
Definition: helpers.h:37
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:187
Definition: basic.h:17
#define CPPUT_TRACE_FUNCTION
Definition: trace.h:126