Flow123d  release_2.2.0-23-g01927c6
path_yaml.cc
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2015 Technical University of Liberec. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License version 3 as published by the
7  * Free Software Foundation. (http://www.gnu.org/licenses/gpl-3.0.en.html)
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12  *
13  *
14  * @file path_yaml.cc
15  * @brief
16  */
17 
18 #include "input/path_yaml.hh"
20 
21 #include "system/global_defs.h"
22 #include "system/system.hh"
23 #include "yaml-cpp/yaml.h"
24 
25 
26 namespace Input {
27 using namespace std;
28 
29 
30 PathYAML::PathYAML(istream &in)
31 : PathBase()
32 {
33  nodes_.push_back( YAML::Load( in ) );
34 
35  json_type_names.push_back("YAML map");
36  json_type_names.push_back("YAML sequence");
37  json_type_names.push_back("YAML string");
38  json_type_names.push_back("YAML bool");
39  json_type_names.push_back("YAML int");
40  json_type_names.push_back("YAML real");
41  json_type_names.push_back("YAML null");
42  json_type_names.push_back("other scalar type");
43  json_type_names.push_back("undefined type");
44 }
45 
47 {
48  this->go_to_root();
49 }
50 
51 
52 bool PathYAML::down(unsigned int index) {
53  ASSERT(head().IsSequence()).error("Head node must be of type Array.");
54 
55  if ( index >= head().size() ) return false;
56  path_.push_back( make_pair( index, string("") ) );
57  nodes_.push_back( head()[index] );
58 
59  return true;
60 }
61 
62 
63 bool PathYAML::down(const string& key, int index) {
64  ASSERT(head().IsMap()).error("Head node must be of type Record.");
65 
66  if ( head()[key] ) {
67  path_.push_back( make_pair( index, key) );
68  nodes_.push_back( head()[key] );
69  } else {
70  return false;
71  }
72  return true;
73 }
74 
75 
76 void PathYAML::up() {
77  if (path_.size() > 1) {
78  path_.pop_back();
79  nodes_.pop_back();
80  }
81 }
82 
83 
84 bool PathYAML::is_null_type() const {
85  return head().IsNull();
86 }
87 
88 
90  if (head().IsScalar()) {
91  try {
92  return head().as<bool>();
93  } catch (YAML::Exception) {
94  THROW( ReaderInternalBase::ExcInputError() );
95  }
96  } else {
97  THROW( ReaderInternalBase::ExcInputError() );
98  }
99  return false;
100 }
101 
102 
103 std::int64_t PathYAML::get_int_value() const {
104  if (head().IsScalar()) {
105  try {
106  return head().as<std::int64_t>();
107  } catch (YAML::Exception) {
108  THROW( ReaderInternalBase::ExcInputError() );
109  }
110  } else {
111  THROW( ReaderInternalBase::ExcInputError() );
112  }
113  return 0;
114 }
115 
116 
118  if (head().IsScalar()) {
119  try {
120  return head().as<double>();
121  } catch (YAML::Exception) {
122  THROW( ReaderInternalBase::ExcInputError() );
123  }
124  } else {
125  THROW( ReaderInternalBase::ExcInputError() );
126  }
127  return 0.0;
128 }
129 
130 
131 std::string PathYAML::get_string_value() const {
132  if (head().IsScalar()) {
133  try {
134  return head().as<std::string>();
135  } catch (YAML::Exception) {
136  THROW( ReaderInternalBase::ExcInputError() );
137  }
138  } else {
139  THROW( ReaderInternalBase::ExcInputError() );
140  }
141  return "";
142 }
143 
144 
145 unsigned int PathYAML::get_node_type_index() const {
146  switch (head().Type()) {
147  case YAML::NodeType::Null: return ValueTypes::null_type;
148  case YAML::NodeType::Scalar: return ValueTypes::scalar_type;
149  case YAML::NodeType::Sequence: return ValueTypes::array_type;
150  case YAML::NodeType::Map: return ValueTypes::obj_type;
151  default: return ValueTypes::undef_type;
152  }
153 }
154 
155 
156 bool PathYAML::get_record_key_set(std::set<std::string> &keys_list) const {
157  if ( head().IsMap() ) {
158  for (YAML::const_iterator it=head().begin(); it!=head().end(); ++it) {
159  keys_list.insert( it->first.as<std::string>() );
160  }
161  return true;
162  }
163 
164  return false;
165 }
166 
167 
169  if (head().IsSequence()) {
170  return head().size();
171  }
172  return -1;
173 }
174 
175 
177  return head().IsMap();
178 }
179 
180 
182  return head().IsSequence();
183 }
184 
185 
187  return new PathYAML(*this);
188 }
189 
190 
192 {
193  return NULL;
194 }
195 
196 
197 
198 std::string PathYAML::get_record_tag() const {
199  std::string tag = head().Tag();
200  if ( (tag == "?") || (tag.size() == 0) ) {
201  return "";
202  } else {
203  return tag.erase(0, 1); // tag starts with '!' char
204  }
205 }
206 
207 
208 
210  if ( head().IsNull() ) {
211  // null value indicates empty record ...
212  return true;
213  } else if ( head().IsScalar() ) {
214  try {
215  // ... or empty string indicates empty record too
216  return (head().as<std::string>() == "");
217  } catch (YAML::Exception) {
218  // other cases don't have to lead to an error
219  }
220 
221  }
222  return false;
223 }
224 
225 
226 
227 std::ostream& operator<<(std::ostream& stream, const PathYAML& path) {
228  path.output(stream);
229  return stream;
230 }
231 
232 
233 } // namespace Input
unsigned int get_node_type_index() const override
Implements PathBase::get_node_type_index.
Definition: path_yaml.cc:145
Class used by ReaderToStorage class to iterate over the YAML tree provided by yaml-cpp library...
Definition: path_yaml.hh:47
int get_array_size() const override
Implements PathBase::get_array_size.
Definition: path_yaml.cc:168
std::ostream & operator<<(std::ostream &stream, const Address &address)
Definition: accessors.hh:253
Base abstract class used by ReaderToStorage class to iterate over the input tree. ...
Definition: path_base.hh:41
std::int64_t get_int_value() const override
Implements PathBase::get_int_value.
Definition: path_yaml.cc:103
bool is_null_type() const override
Implements PathBase::is_null_type.
Definition: path_yaml.cc:84
std::vector< std::string > json_type_names
Names of all possible node types in parsed input tree.
Definition: path_base.hh:175
PathBase * find_ref_node() override
Implements reading of reference keys, and check of cyclic references.
Definition: path_yaml.cc:191
bool get_record_key_set(std::set< std::string > &) const override
Implements PathBase::get_record_key_set.
Definition: path_yaml.cc:156
#define ASSERT(expr)
Allow use shorter versions of macro names if these names is not used with external library...
Definition: asserts.hh:346
void go_to_root()
Move to root node.
Definition: path_base.cc:56
Global macros to enhance readability and debugging, general constants.
bool is_array_type() const override
Implements PathBase::is_array_type.
Definition: path_yaml.cc:181
PathYAML(std::istream &in)
Constructor.
Definition: path_yaml.cc:30
std::vector< std::pair< int, std::string > > path_
One level of the path_ is either index (nonnegative int) in array or string key in a json object...
Definition: path_base.hh:166
~PathYAML() override
Destructor.
Definition: path_yaml.cc:46
const Node & head() const
Pointer to YAML Value object at current path.
Definition: path_yaml.hh:111
std::vector< Node > nodes_
Definition: path_yaml.hh:115
bool is_effectively_null() const override
Implements PathBase::is_effectively_null.
Definition: path_yaml.cc:209
bool is_record_type() const override
Implements PathBase::is_record_type.
Definition: path_yaml.cc:176
PathYAML * clone() const override
Implements PathBase::clone.
Definition: path_yaml.cc:186
double get_double_value() const override
Implements PathBase::get_double_value.
Definition: path_yaml.cc:117
bool get_bool_value() const override
Implements PathBase::get_bool_value.
Definition: path_yaml.cc:89
void output(std::ostream &stream) const
Output to the given stream.
Definition: path_base.cc:31
bool down(unsigned int index) override
Dive into yaml-cpp hierarchy.
Definition: path_yaml.cc:52
std::string get_string_value() const override
Implements PathBase::get_string_value.
Definition: path_yaml.cc:131
void up() override
Return one level up in the hierarchy.
Definition: path_yaml.cc:76
std::string get_record_tag() const override
Implements PathBase::get_record_tag.
Definition: path_yaml.cc:198
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.
Definition: exceptions.hh:53