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  impl_ = ImplPtr(new Impl(nh));
46  }
47 
48 
49  void Reader::startMap(const SizeLimitEnforcementType limit_type, const std::size_t min, const std::size_t max)
50  {
51  ARILES2_TRACE_FUNCTION;
52  if (XmlRpc::XmlRpcValue::TypeStruct == impl_->getRawNode().getType())
53  {
54  checkSize(limit_type, impl_->getRawNode().size(), min, max);
55  }
56  else
57  {
58  ARILES2_PERSISTENT_ASSERT(0 == min and min == max, "Expected struct.");
59  }
60  }
61 
62  bool Reader::startMapEntry(const std::string &child_name)
63  {
64  if (impl_->node_stack_.empty())
65  {
66  impl_->root_name_ = child_name;
67  impl_->nh_.getParam(impl_->root_name_, impl_->root_value_);
68  impl_->node_stack_.push_back(&impl_->root_value_);
69  return (true);
70  }
71 
72  XmlRpc::XmlRpcValue &node = impl_->getRawNode();
73  if ((XmlRpc::XmlRpcValue::TypeStruct == node.getType()) && (true == node.hasMember(child_name)))
74  {
75  impl_->node_stack_.push_back(NodeWrapper(&(node[child_name])));
76  return (true);
77  }
78  return (false);
79  }
80 
82  {
83  impl_->node_stack_.pop_back();
84  }
85 
86 
87 
89  const SizeLimitEnforcementType limit_type,
90  const std::size_t min,
91  const std::size_t max)
92  {
93  ARILES2_TRACE_FUNCTION;
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  ARILES2_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_->node_stack_.push_back(&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_->node_stack_.pop_back();
119  }
120 
122  {
123  ARILES2_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  ARILES2_ASSERT(XmlRpc::XmlRpcValue::TypeArray == impl_->getRawNode().getType(), "Expected array.");
133 
134  std::size_t size = impl_->getRawNode().size();
135  impl_->node_stack_.push_back(NodeWrapper(0, size));
136 
137  return (size);
138  }
139 
141  {
142  ARILES2_ASSERT(
143  impl_->node_stack_.back().index_ < impl_->node_stack_.back().size_,
144  "Internal error: namevalue.has more elements than expected.");
145  }
146 
148  {
149  ARILES2_ASSERT(true == impl_->node_stack_.back().isArray(), "Internal error: expected array.");
150  ++impl_->node_stack_.back().index_;
151  }
152 
154  {
155  impl_->node_stack_.pop_back();
156  }
157 
158 
159  bool Reader::startRoot(const std::string &name)
160  {
161  ARILES2_TRACE_FUNCTION;
162  if (true == name.empty())
163  {
164  return (startMapEntry("ariles"));
165  }
166  return (startMapEntry(name));
167  }
168 
169  void Reader::endRoot(const std::string & /*name*/)
170  {
171  ARILES2_TRACE_FUNCTION;
172  endMapEntry();
173  }
174 
175 
176 #define ARILES2_BASIC_TYPE(type) \
177  void Reader::readElement(type &element) \
178  { \
179  ARILES2_ASSERT(impl_->getRawNode().getType() == XmlRpc::XmlRpcValue::TypeInt, "Integer type expected."); \
180  int tmp_value = static_cast<int>(impl_->getRawNode()); \
181  ARILES2_ASSERT( \
182  static_cast<int64_t>(tmp_value) <= std::numeric_limits<type>::max() \
183  && static_cast<int64_t>(tmp_value) >= std::numeric_limits<type>::min(), \
184  "Value is out of range."); \
185  element = static_cast<type>(tmp_value); \
186  }
187 
189 
190 #undef ARILES2_BASIC_TYPE
191 
192 
193 #define ARILES2_BASIC_TYPE(type) \
194  void Reader::readElement(type &element) \
195  { \
196  ARILES2_ASSERT(impl_->getRawNode().getType() == XmlRpc::XmlRpcValue::TypeInt, "Integer type expected."); \
197  int tmp_value = static_cast<int>(impl_->getRawNode()); \
198  ARILES2_ASSERT(tmp_value >= 0, "Expected positive value."); \
199  ARILES2_ASSERT(static_cast<uint64_t>(tmp_value) <= std::numeric_limits<type>::max(), "Value is too large."); \
200  element = static_cast<type>(tmp_value); \
201  }
202 
204 
205 #undef ARILES2_BASIC_TYPE
206 
207 
208 #define ARILES2_BASIC_TYPE(type) \
209  void Reader::readElement(type &element) \
210  { \
211  switch (impl_->getRawNode().getType()) \
212  { \
213  case XmlRpc::XmlRpcValue::TypeDouble: \
214  element = static_cast<double>(impl_->getRawNode()); \
215  break; \
216  case XmlRpc::XmlRpcValue::TypeString: \
217  element = boost::lexical_cast<double>(static_cast<std::string>(impl_->getRawNode())); \
218  break; \
219  case XmlRpc::XmlRpcValue::TypeInt: \
220  element = static_cast<int>(impl_->getRawNode()); \
221  break; \
222  default: \
223  ARILES2_THROW("Could not convert value to type."); \
224  break; \
225  } \
226  }
227 
229 
230 #undef ARILES2_BASIC_TYPE
231 
232 
233  void Reader::readElement(std::string &element)
234  {
235  element = static_cast<std::string>(impl_->getRawNode());
236  }
237 
238 
239  void Reader::readElement(bool &element)
240  {
241  switch (impl_->getRawNode().getType())
242  {
243  case XmlRpc::XmlRpcValue::TypeString:
244  element = boost::lexical_cast<bool>(static_cast<std::string>(impl_->getRawNode()));
245  break;
246 
247  case XmlRpc::XmlRpcValue::TypeBoolean:
248  element = static_cast<bool>(impl_->getRawNode());
249  break;
250 
251  case XmlRpc::XmlRpcValue::TypeInt:
252  element = static_cast<int>(impl_->getRawNode()) > 0;
253  break;
254 
255  default:
256  ARILES2_THROW("Could not convert value to boolean.");
257  break;
258  }
259  }
260  } // namespace ns_rosparam
261 } // namespace ariles2
ariles2
Definition: basic.h:16
ariles2::read::Visitor::checkSize
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:62
ariles2::ns_rosparam::Reader::startRoot
bool startRoot(const std::string &name)
Definition: reader.cpp:159
ariles2::ns_rosparam::ARILES2_MACRO_SUBSTITUTE
ARILES2_MACRO_SUBSTITUTE(ARILES2_BASIC_SIGNED_INTEGER_TYPES_LIST) ARILES2_MACRO_SUBSTITUTE(ARILES2_BASIC_UNSIGNED_INTEGER_TYPES_LIST) ARILES2_MACRO_SUBSTITUTE(ARILES2_BASIC_REAL_TYPES_LIST) void Reader
Definition: reader.cpp:188
ariles2::read::Visitor::SizeLimitEnforcementType
SizeLimitEnforcementType
Definition: read.h:47
ariles2::ns_rosparam::impl::Reader::iterator_stack_
std::vector< XmlRpc::XmlRpcValue::iterator > iterator_stack_
Definition: reader.cpp:26
ariles2::ns_rosparam::Reader::endIteratedMap
void endIteratedMap()
Definition: reader.cpp:121
ariles2::read::Visitor::readElement
void readElement(std::complex< t_Scalar > &entry)
Definition: read.h:391
ariles2::ns_rosparam::Reader::startMap
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
ariles2::ns_rosparam::Reader::endArrayElement
void endArrayElement()
Definition: reader.cpp:147
ariles2::ns_rosparam::Reader::startArrayElement
void startArrayElement()
Definition: reader.cpp:140
ariles2::ns_rosparam::Reader::endIteratedMapElement
void endIteratedMapElement()
Definition: reader.cpp:115
ARILES2_BASIC_SIGNED_INTEGER_TYPES_LIST
#define ARILES2_BASIC_SIGNED_INTEGER_TYPES_LIST
Definition: helpers.h:101
ariles2::ns_rosparam::Reader::startIteratedMapElement
bool startIteratedMapElement(std::string &entry_name)
Definition: reader.cpp:104
ariles2::ns_rosparam::ImplBase
Definition: common.h:28
ARILES2_BASIC_UNSIGNED_INTEGER_TYPES_LIST
#define ARILES2_BASIC_UNSIGNED_INTEGER_TYPES_LIST
Definition: helpers.h:107
ariles2::serialization::PIMPLVisitor< read::Visitor, impl::Reader >::Impl
impl::Reader Impl
Definition: serialization.h:124
ariles2::ns_rosparam::Reader::Reader
Reader(const ::ros::NodeHandle &nh)
Constructor.
Definition: reader.cpp:43
ARILES2_VISIBILITY_ATTRIBUTE
#define ARILES2_VISIBILITY_ATTRIBUTE
Definition: helpers.h:138
common.h
ariles2::serialization::PIMPLVisitor< read::Visitor, impl::Reader >::impl_
ImplPtr impl_
Definition: serialization.h:128
ariles2::ns_rosparam::Reader::startArray
std::size_t startArray()
Definition: reader.cpp:130
ariles2::ns_rosparam::NodeWrapper
serialization::Node< XmlRpc::XmlRpcValue * > NodeWrapper
Definition: common.h:25
ariles2::ns_rosparam::Reader::endArray
void endArray()
Definition: reader.cpp:153
ariles2::ns_rosparam::Reader::startIteratedMap
bool startIteratedMap(const SizeLimitEnforcementType=SIZE_LIMIT_NONE, const std::size_t=0, const std::size_t=0)
Definition: reader.cpp:88
ariles2::ns_rosparam::impl::Reader::Reader
Reader(const ::ros::NodeHandle &nh)
Definition: reader.cpp:29
ariles2::ns_rosparam::Reader::endMapEntry
void endMapEntry()
endMapEntry from the current entry to its parent.
Definition: reader.cpp:81
ariles2::ns_rosparam::impl::Reader
Definition: reader.cpp:23
ARILES2_BASIC_REAL_TYPES_LIST
#define ARILES2_BASIC_REAL_TYPES_LIST
Definition: helpers.h:118
ariles2::ns_rosparam::Reader::startMapEntry
bool startMapEntry(const std::string &child_name)
startMapEntry to the entry with the given name
Definition: reader.cpp:62
ariles2::serialization::PIMPLVisitor< read::Visitor, impl::Reader >::ImplPtr
ARILES2_SHARED_PTR< impl::Reader > ImplPtr
Definition: serialization.h:125
ariles2::ns_rosparam::Reader::endRoot
void endRoot(const std::string &name)
Definition: reader.cpp:169