Ariles
reader.cpp
Go to the documentation of this file.
1 /**
2  @file
3  @author Jan Michalczyk
4  @author Alexander Sherikov
5 
6  @copyright 2014-2017 INRIA. Licensed under the Apache License, Version 2.0.
7  (see @ref LICENSE or http://www.apache.org/licenses/LICENSE-2.0)
8 
9  @copyright 2017-2020 Alexander Sherikov, Licensed under the Apache License, Version 2.0.
10  (see @ref LICENSE or http://www.apache.org/licenses/LICENSE-2.0)
11 
12  @brief
13 */
14 
16 #include "common.h"
17 
18 
19 namespace ariles
20 {
21  namespace ns_yaml_cpp03
22  {
24  }
25 } // namespace ariles
26 
27 
28 namespace ariles
29 {
30  namespace ns_yaml_cpp03
31  {
32  namespace impl
33  {
35  {
36  public:
37  /// instance of YAML parser
38  YAML::Parser parser_;
39 
40  /// root node
41  YAML::Node root_node_;
42 
43  /// Stack of nodes.
44  std::vector<NodeWrapper> node_stack_;
45 
46 
47  public:
48  const YAML::Node &getRawNode(const std::size_t depth)
49  {
50  if (node_stack_[depth].isArray())
51  {
52  return (getRawNode(depth - 1)[node_stack_[depth].index_]);
53  }
54  else
55  {
56  return (*node_stack_[depth].node_);
57  }
58  }
59 
60 
61  const YAML::Node &getRawNode()
62  {
63  return (getRawNode(node_stack_.size() - 1));
64  }
65  };
66  } // namespace impl
67  } // namespace ns_yaml_cpp03
68 } // namespace ariles
69 
70 namespace ariles
71 {
72  namespace ns_yaml_cpp03
73  {
74  Reader::Reader(const std::string &file_name)
75  {
76  std::ifstream config_ifs;
77  read::Visitor::openFile(config_ifs, file_name);
78 
79  impl_ = ImplPtr(new Impl());
80  impl_->parser_.Load(config_ifs);
81  impl_->parser_.GetNextDocument(impl_->root_node_);
82  impl_->node_stack_.push_back(NodeWrapper(&impl_->root_node_));
83  }
84 
85 
86  Reader::Reader(std::istream &input_stream)
87  {
88  impl_ = ImplPtr(new Impl());
89  impl_->parser_.Load(input_stream);
90  impl_->parser_.GetNextDocument(impl_->root_node_);
91  impl_->node_stack_.push_back(NodeWrapper(&impl_->root_node_));
92  }
93 
94 
95 
96  std::size_t Reader::getMapSize(const bool /*expect_empty*/)
97  {
98  return (impl_->getRawNode().size());
99  }
100 
101 
102 
103  bool Reader::descend(const std::string &child_name)
104  {
105  const YAML::Node *child = impl_->getRawNode().FindValue(child_name);
106 
107  if (child == NULL)
108  {
109  return (false);
110  }
111  else
112  {
113  impl_->node_stack_.push_back(NodeWrapper(child));
114  return (true);
115  }
116  }
117 
118 
119 
121  {
122  impl_->node_stack_.pop_back();
123  }
124 
125 
126  bool Reader::getMapEntryNames(std::vector<std::string> &child_names)
127  {
128  const YAML::Node *selected_node = &impl_->getRawNode();
129 
130  if (YAML::NodeType::Map != selected_node->Type())
131  {
132  return (false);
133  }
134  else
135  {
136  child_names.resize(selected_node->size());
137 
138  std::size_t i = 0;
139  for (YAML::Iterator it = selected_node->begin(); it != selected_node->end(); ++it, ++i)
140  {
141  it.first() >> child_names[i];
142  }
143  return (true);
144  }
145  }
146 
147 
148  std::size_t Reader::startArray()
149  {
150  std::size_t size = impl_->getRawNode().size();
151  impl_->node_stack_.push_back(NodeWrapper(0, size));
152 
153  return (size);
154  }
155 
156 
158  {
159  ARILES_ASSERT(true == impl_->node_stack_.back().isArray(), "Internal error: expected array.");
160  ARILES_ASSERT(
161  impl_->node_stack_.back().index_ < impl_->node_stack_.back().size_,
162  "Internal error: array has more elements than expected.");
163  ++impl_->node_stack_.back().index_;
164  }
165 
166 
168  {
169  impl_->node_stack_.pop_back();
170  }
171 
172 
173 #define ARILES_BASIC_TYPE(type) \
174  void Reader::readElement(type &element) \
175  { \
176  impl_->getRawNode() >> element; \
177  }
178 
180 
181 #undef ARILES_BASIC_TYPE
182  } // namespace ns_yaml_cpp03
183 } // namespace ariles
YAML::Parser parser_
instance of YAML parser
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:195
const YAML::Node & getRawNode(const std::size_t depth)
Definition: reader.cpp:48
void ascend()
Ascend from the current entry to its parent.
Definition: reader.cpp:120
static void openFile(std::ifstream &config_ifs, const std::string &file_name)
open configuration file
Definition: read.h:81
bool getMapEntryNames(std::vector< std::string > &child_names)
Definition: reader.cpp:126
std::size_t getMapSize(const bool)
Definition: reader.cpp:96
Reader(const std::string &file_name)
Constructor.
Definition: reader.cpp:74
const YAML::Node & getRawNode()
Definition: reader.cpp:61
#define ARILES_BASIC_TYPES_LIST
Definition: helpers.h:105
std::vector< NodeWrapper > node_stack_
Stack of nodes.
Definition: reader.cpp:44
ariles::Node< const YAML::Node * > NodeWrapper
Definition: reader.cpp:23
YAML::Node root_node_
root node
Definition: reader.cpp:41
bool descend(const std::string &child_name)
Descend to the entry with the given name.
Definition: reader.cpp:103
#define ARILES_VISIBILITY_ATTRIBUTE
Definition: helpers.h:69
Definition: basic.h:17