Flow123d  release_2.1.0-87-gfbc1563
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 
24 
25 namespace Input {
26 using namespace std;
27 
28 
29 PathYAML::PathYAML(istream &in)
30 : PathBase()
31 {
32  nodes_.push_back( YAML::Load( in ) );
33 
34  json_type_names.push_back("YAML map");
35  json_type_names.push_back("YAML sequence");
36  json_type_names.push_back("YAML string");
37  json_type_names.push_back("YAML bool");
38  json_type_names.push_back("YAML int");
39  json_type_names.push_back("YAML real");
40  json_type_names.push_back("YAML null");
41  json_type_names.push_back("other scalar type");
42  json_type_names.push_back("undefined type");
43 }
44 
46 {
47  this->go_to_root();
48 }
49 
50 
51 bool PathYAML::down(unsigned int index) {
52  ASSERT(head().IsSequence()).error("Head node must be of type Array.");
53 
54  if ( index >= head().size() ) return false;
55  path_.push_back( make_pair( index, string("") ) );
56  nodes_.push_back( head()[index] );
57 
58  return true;
59 }
60 
61 
62 bool PathYAML::down(const string& key) {
63  ASSERT(head().IsMap()).error("Head node must be of type Record.");
64 
65  if ( head()[key] ) {
66  path_.push_back( make_pair( (int)(-1), key) );
67  nodes_.push_back( head()[key] );
68  } else {
69  return false;
70  }
71  return true;
72 }
73 
74 
75 void PathYAML::up() {
76  if (path_.size() > 1) {
77  path_.pop_back();
78  nodes_.pop_back();
79  }
80 }
81 
82 
83 bool PathYAML::is_null_type() const {
84  return head().IsNull();
85 }
86 
87 
89  if (head().IsScalar()) {
90  try {
91  return head().as<bool>();
92  } catch (YAML::Exception) {
93  THROW( ReaderToStorage::ExcInputError() );
94  }
95  } else {
96  THROW( ReaderToStorage::ExcInputError() );
97  }
98  return false;
99 }
100 
101 
102 std::int64_t PathYAML::get_int_value() const {
103  if (head().IsScalar()) {
104  try {
105  return head().as<std::int64_t>();
106  } catch (YAML::Exception) {
107  THROW( ReaderToStorage::ExcInputError() );
108  }
109  } else {
110  THROW( ReaderToStorage::ExcInputError() );
111  }
112  return 0;
113 }
114 
115 
117  if (head().IsScalar()) {
118  try {
119  return head().as<double>();
120  } catch (YAML::Exception) {
121  THROW( ReaderToStorage::ExcInputError() );
122  }
123  } else {
124  THROW( ReaderToStorage::ExcInputError() );
125  }
126  return 0.0;
127 }
128 
129 
130 std::string PathYAML::get_string_value() const {
131  if (head().IsScalar()) {
132  try {
133  return head().as<std::string>();
134  } catch (YAML::Exception) {
135  THROW( ReaderToStorage::ExcInputError() );
136  }
137  } else {
138  THROW( ReaderToStorage::ExcInputError() );
139  }
140  return "";
141 }
142 
143 
144 unsigned int PathYAML::get_node_type_index() const {
145  switch (head().Type()) {
146  case YAML::NodeType::Null: return ValueTypes::null_type;
147  case YAML::NodeType::Scalar: return ValueTypes::scalar_type;
148  case YAML::NodeType::Sequence: return ValueTypes::array_type;
149  case YAML::NodeType::Map: return ValueTypes::obj_type;
150  default: return ValueTypes::undef_type;
151  }
152 }
153 
154 
155 bool PathYAML::get_record_key_set(std::set<std::string> &keys_list) const {
156  if ( head().IsMap() ) {
157  for (YAML::const_iterator it=head().begin(); it!=head().end(); ++it) {
158  keys_list.insert( it->first.as<std::string>() );
159  }
160  return true;
161  }
162 
163  return false;
164 }
165 
166 
168  if (head().IsSequence()) {
169  return head().size();
170  }
171  return -1;
172 }
173 
174 
176  return head().IsMap();
177 }
178 
179 
181  return head().IsSequence();
182 }
183 
184 
186  return new PathYAML(*this);
187 }
188 
189 
191 {
192  return NULL;
193 }
194 
195 
196 
197 std::string PathYAML::get_record_name() const {
198  std::string tag = head().Tag();
199  if (tag == "?") {
200  return "";
201  } else {
202  return tag.erase(0, 1); // tag starts with '!' char
203  }
204 }
205 
206 
207 
209  if ( head().IsNull() ) {
210  // null value indicates empty record ...
211  return true;
212  } else if ( head().IsScalar() ) {
213  try {
214  // ... or empty string indicates empty record too
215  return (head().as<std::string>() == "");
216  } catch (YAML::Exception) {
217  // other cases don't have to lead to an error
218  }
219 
220  }
221  return false;
222 }
223 
224 
225 
226 std::ostream& operator<<(std::ostream& stream, const PathYAML& path) {
227  path.output(stream);
228  return stream;
229 }
230 
231 
232 } // namespace Input
unsigned int get_node_type_index() const override
Implements PathBase::get_node_type_index.
Definition: path_yaml.cc:144
Class used by ReaderToStorage class to iterate over the YAML tree provided by yaml-cpp library...
Definition: path_yaml.hh:39
int get_array_size() const override
Implements PathBase::get_array_size.
Definition: path_yaml.cc:167
std::ostream & operator<<(std::ostream &stream, const Address &address)
Definition: accessors.hh:247
Base abstract class used by ReaderToStorage class to iterate over the input tree. ...
Definition: path_base.hh:39
std::int64_t get_int_value() const override
Implements PathBase::get_int_value.
Definition: path_yaml.cc:102
bool is_null_type() const override
Implements PathBase::is_null_type.
Definition: path_yaml.cc:83
std::vector< std::string > json_type_names
Names of all possible node types in parsed input tree.
Definition: path_base.hh:173
PathBase * find_ref_node() override
Implements reading of reference keys, and check of cyclic references.
Definition: path_yaml.cc:190
bool get_record_key_set(std::set< std::string > &) const override
Implements PathBase::get_record_key_set.
Definition: path_yaml.cc:155
#define ASSERT(expr)
Allow use shorter versions of macro names if these names is not used with external library...
Definition: asserts.hh:347
void go_to_root()
Move to root node.
Definition: path_base.cc:56
std::string get_record_name() const override
Implements PathBase::get_record_name.
Definition: path_yaml.cc:197
Global macros to enhance readability and debugging, general constants.
bool is_array_type() const override
Implements PathBase::is_array_type.
Definition: path_yaml.cc:180
PathYAML(std::istream &in)
Constructor.
Definition: path_yaml.cc:29
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:164
~PathYAML() override
Destructor.
Definition: path_yaml.cc:45
const Node & head() const
Pointer to YAML Value object at current path.
Definition: path_yaml.hh:103
std::vector< Node > nodes_
Definition: path_yaml.hh:107
bool is_effectively_null() const override
Implements PathBase::is_effectively_null.
Definition: path_yaml.cc:208
bool is_record_type() const override
Implements PathBase::is_record_type.
Definition: path_yaml.cc:175
PathYAML * clone() const override
Implements PathBase::clone.
Definition: path_yaml.cc:185
double get_double_value() const override
Implements PathBase::get_double_value.
Definition: path_yaml.cc:116
bool get_bool_value() const override
Implements PathBase::get_bool_value.
Definition: path_yaml.cc:88
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:51
std::string get_string_value() const override
Implements PathBase::get_string_value.
Definition: path_yaml.cc:130
void up() override
Return one level up in the hierarchy.
Definition: path_yaml.cc:75
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.
Definition: exceptions.hh:45