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 
12 #include <yaml-cpp/yaml.h>
13 
14 
15 namespace ariles2
16 {
17  namespace ns_yaml_cpp
18  {
20  }
21 } // namespace ariles2
22 
23 
24 namespace ariles2
25 {
26  namespace ns_yaml_cpp
27  {
28  namespace impl
29  {
30  class Reader : public serialization::NodeStackBase<NodeWrapper>
31  {
32  public:
33  std::vector<YAML::const_iterator> iterator_stack_;
34 
35 
36  public:
37  const YAML::Node getRawNode(const std::size_t depth)
38  {
40  if (node_stack_[depth].isArray())
41  {
42  return (getRawNode(depth - 1)[node_stack_[depth].index_]);
43  }
44  return (node_stack_[depth].node_);
45  }
46 
47 
48  const YAML::Node getRawNode()
49  {
51  return (getRawNode(node_stack_.size() - 1));
52  }
53  };
54  } // namespace impl
55  } // namespace ns_yaml_cpp
56 } // namespace ariles2
57 
58 
59 namespace ariles2
60 {
61  namespace ns_yaml_cpp
62  {
63  Reader::Reader(const std::string &file_name)
64  {
65  makeImplPtr();
66  impl_->emplace(YAML::LoadFile(file_name));
67  }
68 
69 
70  Reader::Reader(std::istream &input_stream)
71  {
72  makeImplPtr();
73  impl_->emplace(YAML::Load(input_stream));
74  }
75 
76 
77 
78  void Reader::startMap(const SizeLimitEnforcementType limit_type, const std::size_t min, const std::size_t max)
79  {
81  checkSize(limit_type, impl_->getRawNode().size(), min, max);
82  }
83 
84  bool Reader::startMapEntry(const std::string &child_name)
85  {
87  const YAML::Node child = impl_->getRawNode()[child_name];
88 
89  if (not child.IsDefined() or child.IsNull())
90  {
91  return (false);
92  }
93  impl_->emplace(child);
94  return (true);
95  }
96 
98  {
100  impl_->pop();
101  }
102 
103 
104 
106  const SizeLimitEnforcementType limit_type,
107  const std::size_t min,
108  const std::size_t max)
109  {
111  checkSize(limit_type, impl_->getRawNode().size(), min, max);
112 
113  YAML::Node selected_node = impl_->getRawNode();
114 
115  if (selected_node.IsMap())
116  {
117  impl_->iterator_stack_.emplace_back(selected_node.begin());
118  return (true);
119  }
120  return (false);
121  }
122 
123  bool Reader::startIteratedMapElement(std::string &entry_name)
124  {
126  if (impl_->iterator_stack_.back() != impl_->getRawNode().end())
127  {
128  impl_->emplace(impl_->iterator_stack_.back()->second);
129  entry_name = impl_->iterator_stack_.back()->first.as<std::string>();
130  return (true);
131  }
132  return (false);
133  }
134 
136  {
138  ++impl_->iterator_stack_.back();
139  impl_->pop();
140  }
141 
143  {
145  CPPUT_ASSERT(
146  impl_->iterator_stack_.back() == impl_->getRawNode().end(),
147  "End of iterated map has not been reached.");
148  impl_->iterator_stack_.pop_back();
149  }
150 
151 
152  std::size_t Reader::startArray()
153  {
155  CPPUT_ASSERT(impl_->getRawNode().IsSequence(), "Entry is not an array.");
156 
157  const std::size_t size = impl_->getRawNode().size();
158  impl_->emplace(0, size);
159 
160  return (size);
161  }
162 
163 
165  {
167  CPPUT_ASSERT(
168  impl_->back().index_ < impl_->back().size_,
169  "Internal error: array has more elements than expected.");
170  }
171 
172 
174  {
175  impl_->shiftArray();
176  }
177 
178 
180  {
182  impl_->pop();
183  }
184 
185 
186 #define ARILES2_BASIC_TYPE(type) \
187  void Reader::readElement(type &element) \
188  { \
189  CPPUT_TRACE_FUNCTION; \
190  element = impl_->getRawNode().as<type>(); \
191  }
192 
194 
195 #undef ARILES2_BASIC_TYPE
196  } // namespace ns_yaml_cpp
197 } // namespace ariles2
Reader(const std::string &file_name)
Constructor.
Definition: reader.cpp:63
std::size_t startArray()
Definition: reader.cpp:152
bool startIteratedMapElement(std::string &entry_name)
Definition: reader.cpp:123
bool startIteratedMap(const SizeLimitEnforcementType=SIZE_LIMIT_NONE, const std::size_t=0, const std::size_t=0)
Definition: reader.cpp:105
void startMap(const SizeLimitEnforcementType limit_type=SIZE_LIMIT_NONE, const std::size_t min=0, const std::size_t max=0)
Definition: reader.cpp:78
void endMapEntry()
endMapEntry from the current entry to its parent.
Definition: reader.cpp:97
bool startMapEntry(const std::string &child_name)
startMapEntry to the entry with the given name
Definition: reader.cpp:84
std::vector< YAML::const_iterator > iterator_stack_
Definition: reader.cpp:33
const YAML::Node getRawNode()
Definition: reader.cpp:48
const YAML::Node getRawNode(const std::size_t depth)
Definition: reader.cpp:37
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
#define CPPUT_ASSERT(condition,...)
Definition: exception.h:32
#define ARILES2_BASIC_TYPES_LIST
Definition: helpers.h:72
CPPUT_MACRO_SUBSTITUTE(ARILES2_BASIC_INTEGER_TYPES_LIST) CPPUT_MACRO_SUBSTITUTE(ARILES2_BASIC_REAL_TYPES_LIST) void Writer
Definition: writer.cpp:184
Definition: basic.h:17
#define CPPUT_TRACE_FUNCTION
Definition: trace.h:126