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 ariles
18 {
19  namespace ns_ros
20  {
21  namespace impl
22  {
24  {
25  public:
26  explicit Reader(const ::ros::NodeHandle &nh)
27  {
28  nh_ = nh;
29  }
30  };
31  } // namespace impl
32  } // namespace ns_ros
33 } // namespace ariles
34 
35 
36 namespace ariles
37 {
38  namespace ns_ros
39  {
40  Reader::Reader(const ::ros::NodeHandle &nh)
41  {
42  impl_ = ImplPtr(new Impl(nh));
43  }
44 
45 
46  std::size_t Reader::getMapSize(const bool expect_empty)
47  {
48  if (XmlRpc::XmlRpcValue::TypeStruct == impl_->getRawNode().getType())
49  {
50  return (impl_->getRawNode().size());
51  }
52  else
53  {
54  ARILES_PERSISTENT_ASSERT(true == expect_empty, "Expected struct.");
55  return (0);
56  }
57  }
58 
59 
60  bool Reader::descend(const std::string &child_name)
61  {
62  if (0 == impl_->node_stack_.size())
63  {
64  impl_->root_name_ = child_name;
65  impl_->nh_.getParam(impl_->root_name_, impl_->root_value_);
66  impl_->node_stack_.push_back(&impl_->root_value_);
67  return (true);
68  }
69  else
70  {
71  XmlRpc::XmlRpcValue &node = impl_->getRawNode();
72  if ((XmlRpc::XmlRpcValue::TypeStruct == node.getType()) && (true == node.hasMember(child_name)))
73  {
74  impl_->node_stack_.push_back(NodeWrapper(&(node[child_name])));
75  return (true);
76  }
77  else
78  {
79  return (false);
80  }
81  }
82  }
83 
84 
86  {
87  impl_->node_stack_.pop_back();
88  }
89 
90 
91  bool Reader::getMapEntryNames(std::vector<std::string> &child_names)
92  {
93  XmlRpc::XmlRpcValue selected_node = impl_->getRawNode();
94 
95  if (XmlRpc::XmlRpcValue::TypeStruct != selected_node.getType())
96  {
97  return (false);
98  }
99  else
100  {
101  child_names.resize(selected_node.size());
102 
103  std::size_t i = 0;
104  for (XmlRpc::XmlRpcValue::iterator it = selected_node.begin(); it != selected_node.end(); ++it, ++i)
105  {
106  child_names[i] = it->first;
107  }
108  return (true);
109  }
110  }
111 
112 
113  std::size_t Reader::startArray()
114  {
115  ARILES_ASSERT(XmlRpc::XmlRpcValue::TypeArray == impl_->getRawNode().getType(), "Expected array.");
116 
117  std::size_t size = impl_->getRawNode().size();
118  impl_->node_stack_.push_back(NodeWrapper(0, size));
119 
120  return (size);
121  }
122 
123 
125  {
126  ARILES_ASSERT(true == impl_->node_stack_.back().isArray(), "Internal error: expected array.");
127  ARILES_ASSERT(
128  impl_->node_stack_.back().index_ < impl_->node_stack_.back().size_,
129  "Internal error: array has more elements than expected.");
130  ++impl_->node_stack_.back().index_;
131  }
132 
133 
135  {
136  impl_->node_stack_.pop_back();
137  }
138 
139 
140  bool Reader::startRoot(const std::string &name)
141  {
143  if (true == name.empty())
144  {
145  return (descend("ariles"));
146  }
147  else
148  {
149  return (descend(name));
150  }
151  }
152 
153  void Reader::endRoot(const std::string & /*name*/)
154  {
156  ascend();
157  }
158 
159 
160 #define ARILES_BASIC_TYPE(type) \
161  void Reader::readElement(type &element) \
162  { \
163  ARILES_ASSERT(impl_->getRawNode().getType() == XmlRpc::XmlRpcValue::TypeInt, "Integer type expected."); \
164  int tmp_value = static_cast<int>(impl_->getRawNode()); \
165  ARILES_ASSERT( \
166  static_cast<int64_t>(tmp_value) <= std::numeric_limits<type>::max() \
167  && static_cast<int64_t>(tmp_value) >= std::numeric_limits<type>::min(), \
168  "Value is out of range."); \
169  element = static_cast<type>(tmp_value); \
170  }
171 
173 
174 #undef ARILES_BASIC_TYPE
175 
176 
177 #define ARILES_BASIC_TYPE(type) \
178  void Reader::readElement(type &element) \
179  { \
180  ARILES_ASSERT(impl_->getRawNode().getType() == XmlRpc::XmlRpcValue::TypeInt, "Integer type expected."); \
181  int tmp_value = static_cast<int>(impl_->getRawNode()); \
182  ARILES_ASSERT(tmp_value >= 0, "Expected positive value."); \
183  ARILES_ASSERT(static_cast<uint64_t>(tmp_value) <= std::numeric_limits<type>::max(), "Value is too large."); \
184  element = static_cast<type>(tmp_value); \
185  }
186 
188 
189 #undef ARILES_BASIC_TYPE
190 
191 
192 #define ARILES_BASIC_TYPE(type) \
193  void Reader::readElement(type &element) \
194  { \
195  switch (impl_->getRawNode().getType()) \
196  { \
197  case XmlRpc::XmlRpcValue::TypeDouble: \
198  element = static_cast<double>(impl_->getRawNode()); \
199  break; \
200  case XmlRpc::XmlRpcValue::TypeString: \
201  element = boost::lexical_cast<double>(static_cast<std::string>(impl_->getRawNode())); \
202  break; \
203  case XmlRpc::XmlRpcValue::TypeInt: \
204  element = static_cast<int>(impl_->getRawNode()); \
205  break; \
206  default: \
207  ARILES_THROW("Could not convert value to type."); \
208  break; \
209  } \
210  }
211 
213 
214 #undef ARILES_BASIC_TYPE
215 
216 
217  void Reader::readElement(std::string &element)
218  {
219  element = static_cast<std::string>(impl_->getRawNode());
220  }
221 
222 
223  void Reader::readElement(bool &element)
224  {
225  switch (impl_->getRawNode().getType())
226  {
227  case XmlRpc::XmlRpcValue::TypeString:
228  element = boost::lexical_cast<bool>(static_cast<std::string>(impl_->getRawNode()));
229  break;
230 
231  case XmlRpc::XmlRpcValue::TypeBoolean:
232  element = static_cast<bool>(impl_->getRawNode());
233  break;
234 
235  case XmlRpc::XmlRpcValue::TypeInt:
236  element = static_cast<int>(impl_->getRawNode()) > 0;
237  break;
238 
239  default:
240  ARILES_THROW("Could not convert value to boolean.");
241  break;
242  }
243  }
244  } // namespace ns_ros
245 } // namespace ariles
#define ARILES_TRACE_FUNCTION
Definition: trace.h:118
std::size_t startArray()
Definition: reader.cpp:113
Reader(const ::ros::NodeHandle &nh)
Definition: reader.cpp:26
#define ARILES_BASIC_UNSIGNED_INTEGER_TYPES_LIST
Definition: helpers.h:86
ImplPtr impl_
Definition: ros.h:34
ariles::Node< XmlRpc::XmlRpcValue * > NodeWrapper
Definition: common.h:25
bool descend(const std::string &child_name)
Definition: reader.cpp:60
#define ARILES_BASIC_SIGNED_INTEGER_TYPES_LIST
Definition: helpers.h:80
t_Implementation Impl
Definition: ros.h:30
bool getMapEntryNames(std::vector< std::string > &child_names)
Definition: reader.cpp:91
Reader(const ::ros::NodeHandle &nh)
Constructor.
Definition: reader.cpp:40
#define ARILES_BASIC_REAL_TYPES_LIST
Definition: helpers.h:96
ARILES_MACRO_SUBSTITUTE(ARILES_BASIC_SIGNED_INTEGER_TYPES_LIST) ARILES_MACRO_SUBSTITUTE(ARILES_BASIC_UNSIGNED_INTEGER_TYPES_LIST) ARILES_MACRO_SUBSTITUTE(ARILES_BASIC_REAL_TYPES_LIST) void Reader
Definition: reader.cpp:172
void endRoot(const std::string &name)
Definition: reader.cpp:153
std::size_t getMapSize(const bool expect_empty)
Definition: reader.cpp:46
#define ARILES_VISIBILITY_ATTRIBUTE
Definition: helpers.h:69
ARILES_SHARED_PTR< t_Implementation > ImplPtr
Definition: ros.h:31
Definition: basic.h:17
bool startRoot(const std::string &name)
Definition: reader.cpp:140