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 ariles
16 {
17  namespace ns_yaml_cpp
18  {
20  }
21 } // namespace ariles
22 
23 
24 namespace ariles
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 
36 
37  public:
38  const YAML::Node getRawNode(const std::size_t depth)
39  {
40  if (node_stack_[depth].isArray())
41  {
42  return (getRawNode(depth - 1)[node_stack_[depth].index_]);
43  }
44  else
45  {
46  return (node_stack_[depth].node_);
47  }
48  }
49 
50 
51  const YAML::Node getRawNode()
52  {
53  return (getRawNode(node_stack_.size() - 1));
54  }
55  };
56  } // namespace impl
57  } // namespace ns_yaml_cpp
58 } // namespace ariles
59 
60 
61 namespace ariles
62 {
63  namespace ns_yaml_cpp
64  {
65  Reader::Reader(const std::string &file_name)
66  {
67  impl_ = ImplPtr(new Impl());
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());
75  impl_->node_stack_.push_back(NodeWrapper(YAML::Load(input_stream)));
76  }
77 
78 
79 
80  std::size_t Reader::getMapSize(const bool /*expect_empty*/)
81  {
83  return (impl_->getRawNode().size());
84  }
85 
86 
87  bool Reader::descend(const std::string &child_name)
88  {
90  YAML::Node child = impl_->getRawNode()[child_name];
91 
92  if (false == child.IsDefined() or true == child.IsNull())
93  {
94  return (false);
95  }
96  else
97  {
98  impl_->node_stack_.push_back(NodeWrapper(child));
99  return (true);
100  }
101  }
102 
103 
104 
106  {
108  impl_->node_stack_.pop_back();
109  }
110 
111 
112  bool Reader::getMapEntryNames(std::vector<std::string> &child_names)
113  {
115  YAML::Node selected_node = impl_->getRawNode();
116 
117  if (false == selected_node.IsMap())
118  {
119  return (false);
120  }
121  else
122  {
123  child_names.resize(selected_node.size());
124 
125  std::size_t i = 0;
126  for (YAML::const_iterator it = selected_node.begin(); it != selected_node.end(); ++it, ++i)
127  {
128  child_names[i] = it->first.as<std::string>();
129  }
130  return (true);
131  }
132  }
133 
134 
135  std::size_t Reader::startArray()
136  {
138  ARILES_ASSERT(true == impl_->getRawNode().IsSequence(), "Entry is not an array.");
139 
140  std::size_t size = impl_->getRawNode().size();
141  impl_->node_stack_.push_back(NodeWrapper(0, size));
142 
143  return (size);
144  }
145 
146 
148  {
150  ARILES_ASSERT(true == impl_->node_stack_.back().isArray(), "Internal error: expected array.");
151  ARILES_ASSERT(
152  impl_->node_stack_.back().index_ < impl_->node_stack_.back().size_,
153  "Internal error: array has more elements than expected.");
154  ++impl_->node_stack_.back().index_;
155  }
156 
157 
159  {
161  impl_->node_stack_.pop_back();
162  }
163 
164 
165 #define ARILES_BASIC_TYPE(type) \
166  void Reader::readElement(type &element) \
167  { \
168  ARILES_TRACE_FUNCTION; \
169  element = impl_->getRawNode().as<type>(); \
170  }
171 
173 
174 #undef ARILES_BASIC_TYPE
175  } // namespace ns_yaml_cpp
176 } // namespace ariles
#define ARILES_TRACE_FUNCTION
Definition: trace.h:118
const YAML::Node getRawNode(const std::size_t depth)
Definition: reader.cpp:38
ARILES_MACRO_SUBSTITUTE(ARILES_BASIC_INTEGER_TYPES_LIST) ARILES_MACRO_SUBSTITUTE(ARILES_BASIC_REAL_TYPES_LIST) void Writer
Definition: writer.cpp:200
ariles::Node< YAML::Node > NodeWrapper
Definition: reader.cpp:19
std::size_t startArray()
Definition: reader.cpp:135
t_Implementation Impl
Definition: yaml_cpp.h:27
std::vector< NodeWrapper > node_stack_
Stack of nodes.
Definition: reader.cpp:34
#define ARILES_BASIC_TYPES_LIST
Definition: helpers.h:105
const YAML::Node getRawNode()
Definition: reader.cpp:51
Reader(const std::string &file_name)
Constructor.
Definition: reader.cpp:65
bool getMapEntryNames(std::vector< std::string > &child_names)
Definition: reader.cpp:112
std::size_t getMapSize(const bool)
Definition: reader.cpp:80
bool descend(const std::string &child_name)
Definition: reader.cpp:87
#define ARILES_VISIBILITY_ATTRIBUTE
Definition: helpers.h:69
ARILES_SHARED_PTR< t_Implementation > ImplPtr
Definition: yaml_cpp.h:28
Definition: basic.h:17