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  {
31  {
32  public:
33  /// Stack of nodes.
34  std::vector<NodeWrapper> node_stack_;
35  std::vector<YAML::const_iterator> iterator_stack_;
36 
37 
38  public:
39  const YAML::Node getRawNode(const std::size_t depth)
40  {
41  ARILES2_TRACE_FUNCTION;
42  if (node_stack_[depth].isArray())
43  {
44  return (getRawNode(depth - 1)[node_stack_[depth].index_]);
45  }
46  return (node_stack_[depth].node_);
47  }
48 
49 
50  const YAML::Node getRawNode()
51  {
52  ARILES2_TRACE_FUNCTION;
53  return (getRawNode(node_stack_.size() - 1));
54  }
55  };
56  } // namespace impl
57  } // namespace ns_yaml_cpp
58 } // namespace ariles2
59 
60 
61 namespace ariles2
62 {
63  namespace ns_yaml_cpp
64  {
65  Reader::Reader(const std::string &file_name)
66  {
67  impl_ = ImplPtr(new impl::Reader());
68  impl_->node_stack_.push_back(NodeWrapper(YAML::LoadFile(file_name)));
69  }
70 
71 
72  Reader::Reader(std::istream &input_stream)
73  {
74  impl_ = ImplPtr(new impl::Reader());
75  impl_->node_stack_.push_back(NodeWrapper(YAML::Load(input_stream)));
76  }
77 
78 
79 
80  void Reader::startMap(const SizeLimitEnforcementType limit_type, const std::size_t min, const std::size_t max)
81  {
82  ARILES2_TRACE_FUNCTION;
83  checkSize(limit_type, impl_->getRawNode().size(), min, max);
84  }
85 
86  bool Reader::startMapEntry(const std::string &child_name)
87  {
88  ARILES2_TRACE_FUNCTION;
89  YAML::Node child = impl_->getRawNode()[child_name];
90 
91  if (false == child.IsDefined() or true == child.IsNull())
92  {
93  return (false);
94  }
95  impl_->node_stack_.push_back(NodeWrapper(child));
96  return (true);
97  }
98 
100  {
101  ARILES2_TRACE_FUNCTION;
102  impl_->node_stack_.pop_back();
103  }
104 
105 
106 
108  const SizeLimitEnforcementType limit_type,
109  const std::size_t min,
110  const std::size_t max)
111  {
112  ARILES2_TRACE_FUNCTION;
113  checkSize(limit_type, impl_->getRawNode().size(), min, max);
114 
115  YAML::Node selected_node = impl_->getRawNode();
116 
117  if (true == selected_node.IsMap())
118  {
119  impl_->iterator_stack_.push_back(selected_node.begin());
120  return (true);
121  }
122  return (false);
123  }
124 
125  bool Reader::startIteratedMapElement(std::string &entry_name)
126  {
127  ARILES2_TRACE_FUNCTION;
128  if (impl_->iterator_stack_.back() != impl_->getRawNode().end())
129  {
130  impl_->node_stack_.push_back(impl_->iterator_stack_.back()->second);
131  entry_name = impl_->iterator_stack_.back()->first.as<std::string>();
132  return (true);
133  }
134  return (false);
135  }
136 
138  {
139  ARILES2_TRACE_FUNCTION;
140  ++impl_->iterator_stack_.back();
141  impl_->node_stack_.pop_back();
142  }
143 
145  {
146  ARILES2_TRACE_FUNCTION;
147  ARILES2_ASSERT(
148  impl_->iterator_stack_.back() == impl_->getRawNode().end(),
149  "End of iterated map has not been reached.");
150  impl_->iterator_stack_.pop_back();
151  }
152 
153 
154  std::size_t Reader::startArray()
155  {
156  ARILES2_TRACE_FUNCTION;
157  ARILES2_ASSERT(true == impl_->getRawNode().IsSequence(), "Entry is not an array.");
158 
159  std::size_t size = impl_->getRawNode().size();
160  impl_->node_stack_.push_back(NodeWrapper(0, size));
161 
162  return (size);
163  }
164 
165 
167  {
168  ARILES2_TRACE_FUNCTION;
169  ARILES2_ASSERT(
170  impl_->node_stack_.back().index_ < impl_->node_stack_.back().size_,
171  "Internal error: namevalue.has more elements than expected.");
172  }
173 
174 
176  {
177  ARILES2_TRACE_FUNCTION;
178  ARILES2_ASSERT(true == impl_->node_stack_.back().isArray(), "Internal error: expected array.");
179  ++impl_->node_stack_.back().index_;
180  }
181 
182 
184  {
185  ARILES2_TRACE_FUNCTION;
186  impl_->node_stack_.pop_back();
187  }
188 
189 
190 #define ARILES2_BASIC_TYPE(type) \
191  void Reader::readElement(type &element) \
192  { \
193  ARILES2_TRACE_FUNCTION; \
194  element = impl_->getRawNode().as<type>(); \
195  }
196 
198 
199 #undef ARILES2_BASIC_TYPE
200  } // namespace ns_yaml_cpp
201 } // namespace ariles2
ariles2
Definition: basic.h:16
ariles2::ns_yaml_cpp::Reader::startMapEntry
bool startMapEntry(const std::string &child_name)
startMapEntry to the entry with the given name
Definition: reader.cpp:86
ariles2::ns_yaml_cpp::impl::Reader
Definition: reader.cpp:30
ariles2::ns_yaml_cpp::impl::Reader::iterator_stack_
std::vector< YAML::const_iterator > iterator_stack_
Definition: reader.cpp:35
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_yaml_cpp::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:80
ARILES2_BASIC_TYPES_LIST
#define ARILES2_BASIC_TYPES_LIST
Definition: helpers.h:131
ariles2::ns_yaml_cpp::impl::Reader::getRawNode
const YAML::Node getRawNode()
Definition: reader.cpp:50
yaml_cpp.h
ariles2::ns_yaml_cpp::Reader::endArrayElement
void endArrayElement()
Definition: reader.cpp:175
ariles2::read::Visitor::SizeLimitEnforcementType
SizeLimitEnforcementType
Definition: read.h:47
ariles2::ns_yaml_cpp::NodeWrapper
serialization::Node< YAML::Node > NodeWrapper
Definition: reader.cpp:19
ariles2::ns_yaml_cpp::Reader::startIteratedMapElement
bool startIteratedMapElement(std::string &entry_name)
Definition: reader.cpp:125
ariles2::ns_yaml_cpp::Reader::endIteratedMap
void endIteratedMap()
Definition: reader.cpp:144
ariles2::ns_yaml_cpp::Reader::endArray
void endArray()
Definition: reader.cpp:183
ariles2::ns_yaml_cpp::Reader::endMapEntry
void endMapEntry()
endMapEntry from the current entry to its parent.
Definition: reader.cpp:99
ariles2::ns_yaml_cpp::Reader::startArray
std::size_t startArray()
Definition: reader.cpp:154
ariles2::ns_yaml_cpp::Reader::startArrayElement
void startArrayElement()
Definition: reader.cpp:166
ariles2::ns_yaml_cpp::Reader::startIteratedMap
bool startIteratedMap(const SizeLimitEnforcementType=SIZE_LIMIT_NONE, const std::size_t=0, const std::size_t=0)
Definition: reader.cpp:107
ariles2::ns_yaml_cpp::Reader::Reader
Reader(const std::string &file_name)
Constructor.
Definition: reader.cpp:65
ARILES2_VISIBILITY_ATTRIBUTE
#define ARILES2_VISIBILITY_ATTRIBUTE
Definition: helpers.h:138
ariles2::ns_yaml_cpp::ARILES2_MACRO_SUBSTITUTE
ARILES2_MACRO_SUBSTITUTE(ARILES2_BASIC_INTEGER_TYPES_LIST) ARILES2_MACRO_SUBSTITUTE(ARILES2_BASIC_REAL_TYPES_LIST) void Writer
Definition: writer.cpp:198
ariles2::serialization::PIMPLVisitor< read::Visitor, impl::Reader >::impl_
ImplPtr impl_
Definition: serialization.h:128
ariles2::ns_yaml_cpp::Reader::endIteratedMapElement
void endIteratedMapElement()
Definition: reader.cpp:137
ariles2::ns_yaml_cpp::impl::Reader::getRawNode
const YAML::Node getRawNode(const std::size_t depth)
Definition: reader.cpp:39
ariles2::serialization::Node
Definition: serialization.h:49
ariles2::ns_yaml_cpp::impl::Reader::node_stack_
std::vector< NodeWrapper > node_stack_
Stack of nodes.
Definition: reader.cpp:34
ariles2::serialization::PIMPLVisitor< read::Visitor, impl::Reader >::ImplPtr
ARILES2_SHARED_PTR< impl::Reader > ImplPtr
Definition: serialization.h:125