Ariles
reader.cpp
Go to the documentation of this file.
1 /**
2  @file
3  @author Alexander Sherikov
4 
5  @copyright 2018-2020 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 #include <boost/math/special_functions.hpp>
13 #include "common.h"
14 
15 
16 namespace ariles2
17 {
18  namespace ns_rapidjson
19  {
20  namespace impl
21  {
22  class ARILES2_VISIBILITY_ATTRIBUTE Reader : public ariles2::ns_rapidjson::ImplBase<const ::rapidjson::Value>
23  {
24  public:
25  std::vector< ::rapidjson::Value::ConstMemberIterator> iterator_stack_;
26 
27  public:
28  void initialize(std::istream &input_stream)
29  {
30  ariles2::ns_rapidjson::IStreamWrapper isw(input_stream);
31  document_.ParseStream(isw);
32  ARILES2_ASSERT(false == document_.HasParseError(), "Parsing failed");
33  }
34  };
35  } // namespace impl
36  } // namespace ns_rapidjson
37 } // namespace ariles2
38 
39 
40 
41 namespace ariles2
42 {
43  namespace ns_rapidjson
44  {
45  Reader::Reader(const std::string &file_name)
46  {
47  std::ifstream config_ifs;
48  read::Visitor::openFile(config_ifs, file_name);
49  impl_ = ImplPtr(new impl::Reader());
50  impl_->initialize(config_ifs);
51  }
52 
53 
54  Reader::Reader(std::istream &input_stream)
55  {
56  impl_ = ImplPtr(new impl::Reader());
57  impl_->initialize(input_stream);
58  }
59 
60 
61  void Reader::constructFromString(const char *input_string)
62  {
63  impl_ = ImplPtr(new impl::Reader());
64  impl_->document_.Parse(input_string);
65  }
66 
67 
68  void Reader::startMap(const SizeLimitEnforcementType limit_type, const std::size_t min, const std::size_t max)
69  {
70  ARILES2_TRACE_FUNCTION;
71  checkSize(limit_type, impl_->getRawNode().MemberCount(), min, max);
72  }
73 
74  bool Reader::startMapEntry(const std::string &child_name)
75  {
76  const ::rapidjson::Value::ConstMemberIterator child = impl_->getRawNode().FindMember(child_name.c_str());
77 
78  if (impl_->getRawNode().MemberEnd() == child)
79  {
80  return (false);
81  }
82  impl_->node_stack_.push_back(impl::Reader::NodeWrapper(&(child->value)));
83  return (true);
84  }
85 
87  {
88  impl_->node_stack_.pop_back();
89  }
90 
91 
93  const SizeLimitEnforcementType limit_type,
94  const std::size_t min,
95  const std::size_t max)
96  {
97  ARILES2_TRACE_FUNCTION;
98  checkSize(limit_type, impl_->getRawNode().MemberCount(), min, max);
99 
100 
101  const ::rapidjson::Value &selected_node = impl_->getRawNode();
102 
103  if (true == selected_node.IsObject())
104  {
105  impl_->iterator_stack_.push_back(selected_node.MemberBegin());
106  return (true);
107  }
108  return (false);
109  }
110 
111  bool Reader::startIteratedMapElement(std::string &entry_name)
112  {
113  if (impl_->iterator_stack_.back() != impl_->getRawNode().MemberEnd())
114  {
115  impl_->node_stack_.push_back(impl::Reader::NodeWrapper(&(impl_->iterator_stack_.back()->value)));
116  entry_name = impl_->iterator_stack_.back()->name.GetString();
117  return (true);
118  }
119  return (false);
120  }
121 
123  {
124  ++impl_->iterator_stack_.back();
125  impl_->node_stack_.pop_back();
126  }
127 
129  {
130  ARILES2_ASSERT(
131  impl_->iterator_stack_.back() == impl_->getRawNode().MemberEnd(),
132  "End of iterated map has not been reached.");
133  impl_->iterator_stack_.pop_back();
134  }
135 
136 
137  std::size_t Reader::startArray()
138  {
139  ARILES2_ASSERT(impl_->getRawNode().IsArray(), "Internal error: expected array.");
140 
141  std::size_t size = impl_->getRawNode().Size();
142  impl_->node_stack_.push_back(impl::Reader::NodeWrapper(0, size));
143 
144  return (size);
145  }
146 
147 
149  {
150  ARILES2_ASSERT(
151  impl_->node_stack_.back().index_ < impl_->node_stack_.back().size_,
152  "Internal error: namevalue.has more elements than expected.");
153  }
154 
155 
157  {
158  ARILES2_ASSERT(true == impl_->node_stack_.back().isArray(), "Internal error: expected array.");
159  ++impl_->node_stack_.back().index_;
160  }
161 
162 
164  {
165  impl_->node_stack_.pop_back();
166  }
167 
168 
169  void Reader::readElement(std::string &element)
170  {
171  element = impl_->getRawNode().GetString();
172  }
173 
174 
175  void Reader::readElement(bool &element)
176  {
177  element = impl_->getRawNode().GetBool();
178  }
179 
180 
181  void Reader::readElement(float &element)
182  {
183  float tmp_value = 0.0;
184  if (true == impl_->getRawNode().IsString())
185  {
186  tmp_value = boost::lexical_cast<float>(impl_->getRawNode().GetString());
187  if (true == boost::math::isnan(tmp_value))
188  {
189  element = std::numeric_limits<float>::signaling_NaN();
190  return;
191  }
192  if (true == boost::math::isinf(tmp_value))
193  {
194  element = static_cast<float>(tmp_value);
195  return;
196  }
197  }
198  else
199  {
200  tmp_value = static_cast<float>(impl_->getRawNode().GetDouble()); // old API compatibility
201  // tmp_value = impl_->getRawNode().GetFloat();
202  }
203  ARILES2_ASSERT(
204  tmp_value <= std::numeric_limits<float>::max() && tmp_value >= -std::numeric_limits<float>::max(),
205  "Value is out of range.");
206  element = static_cast<float>(tmp_value);
207  }
208 
209 
210  void Reader::readElement(double &element)
211  {
212  double tmp_value = 0.0;
213  if (true == impl_->getRawNode().IsString())
214  {
215  tmp_value = boost::lexical_cast<double>(impl_->getRawNode().GetString());
216  if (true == boost::math::isnan(tmp_value))
217  {
218  element = std::numeric_limits<double>::signaling_NaN();
219  return;
220  }
221  if (true == boost::math::isinf(tmp_value))
222  {
223  element = static_cast<double>(tmp_value);
224  return;
225  }
226  }
227  else
228  {
229  tmp_value = impl_->getRawNode().GetDouble();
230  }
231  ARILES2_ASSERT(
232  tmp_value <= std::numeric_limits<double>::max() && tmp_value >= -std::numeric_limits<double>::max(),
233  "Value is out of range.");
234  element = static_cast<double>(tmp_value);
235  }
236 
237 
238 #define ARILES2_BASIC_TYPE(type) \
239  void Reader::readElement(type &element) \
240  { \
241  int64_t tmp_value = impl_->getRawNode().GetInt64(); \
242  ARILES2_ASSERT( \
243  tmp_value <= std::numeric_limits<type>::max() && tmp_value >= std::numeric_limits<type>::min(), \
244  "Value is out of range."); \
245  element = static_cast<type>(tmp_value); \
246  }
247 
249 
250 #undef ARILES2_BASIC_TYPE
251 
252 
253 #define ARILES2_BASIC_TYPE(type) \
254  void Reader::readElement(type &element) \
255  { \
256  uint64_t tmp_value = impl_->getRawNode().GetUint64(); \
257  ARILES2_ASSERT(tmp_value <= std::numeric_limits<type>::max(), "Value is too large."); \
258  element = static_cast<type>(tmp_value); \
259  }
260 
262 
263 #undef ARILES2_BASIC_TYPE
264  } // namespace ns_rapidjson
265 } // 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_rapidjson::Reader::startMapEntry
bool startMapEntry(const std::string &child_name)
startMapEntry to the entry with the given name
Definition: reader.cpp:74
ariles2::ns_rapidjson::Reader::endArray
void endArray()
Definition: reader.cpp:163
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::ns_rapidjson::Reader::endMapEntry
void endMapEntry()
endMapEntry from the current entry to its parent.
Definition: reader.cpp:86
ariles2::ns_rapidjson::Reader::startArray
std::size_t startArray()
Definition: reader.cpp:137
ariles2::ns_rapidjson::Reader::endArrayElement
void endArrayElement()
Definition: reader.cpp:156
rapidjson.h
ariles2::read::Visitor::SizeLimitEnforcementType
SizeLimitEnforcementType
Definition: read.h:47
ariles2::ns_rapidjson::ImplBase
Definition: common.h:33
ariles2::read::Visitor::readElement
void readElement(std::complex< t_Scalar > &entry)
Definition: read.h:391
ariles2::ns_rapidjson::Reader::Reader
Reader()
Definition: reader.h:30
ARILES2_BASIC_SIGNED_INTEGER_TYPES_LIST
#define ARILES2_BASIC_SIGNED_INTEGER_TYPES_LIST
Definition: helpers.h:101
ariles2::ns_rapidjson::impl::Reader::initialize
void initialize(std::istream &input_stream)
Definition: reader.cpp:28
ariles2::ns_rapidjson::Reader::constructFromString
void constructFromString(const char *)
Definition: reader.cpp:61
ariles2::ns_rapidjson::Reader::startIteratedMapElement
bool startIteratedMapElement(std::string &entry_name)
Definition: reader.cpp:111
ARILES2_BASIC_UNSIGNED_INTEGER_TYPES_LIST
#define ARILES2_BASIC_UNSIGNED_INTEGER_TYPES_LIST
Definition: helpers.h:107
ariles2::read::Visitor::openFile
static void openFile(std::ifstream &config_ifs, const std::string &file_name)
open configuration file
Definition: read.h:95
ARILES2_VISIBILITY_ATTRIBUTE
#define ARILES2_VISIBILITY_ATTRIBUTE
Definition: helpers.h:138
ariles2::serialization::PIMPLVisitor< read::Visitor, impl::Reader >::impl_
ImplPtr impl_
Definition: serialization.h:128
ariles2::ns_rapidjson::Reader::startIteratedMap
bool startIteratedMap(const SizeLimitEnforcementType=SIZE_LIMIT_NONE, const std::size_t=0, const std::size_t=0)
Definition: reader.cpp:92
ariles2::ns_rapidjson::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:68
ariles2::ns_rapidjson::BasicIStreamWrapper
Wrapper of std::basic_istream into RapidJSON's Stream concept.
Definition: istreamwrapper.h:52
ariles2::ns_rapidjson::Reader::endIteratedMap
void endIteratedMap()
Definition: reader.cpp:128
ariles2::ns_rapidjson::Reader::endIteratedMapElement
void endIteratedMapElement()
Definition: reader.cpp:122
ariles2::ns_rapidjson::Reader::startArrayElement
void startArrayElement()
Definition: reader.cpp:148
ariles2::ns_rapidjson::impl::Reader
Definition: reader.cpp:22
common.h
ariles2::serialization::Node
Definition: serialization.h:49
ariles2::ns_rapidjson::impl::Reader::iterator_stack_
std::vector< ::rapidjson::Value::ConstMemberIterator > iterator_stack_
Definition: reader.cpp:25
ariles2::serialization::PIMPLVisitor< read::Visitor, impl::Reader >::ImplPtr
ARILES2_SHARED_PTR< impl::Reader > ImplPtr
Definition: serialization.h:125