Flow123d  last_with_con_2.0.0-4-g42e6930
path_base.hh
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_base.hh
15  * @brief
16  */
17 
18 #ifndef PATH_BASE_HH_
19 #define PATH_BASE_HH_
20 
21 #include <utility>
22 #include <set>
23 #include <string>
24 #include <vector>
25 
26 #include "input/input_exception.hh"
27 
28 
29 namespace Input {
30 
31 
32 /**
33  * @brief Base abstract class used by ReaderToStorage class to iterate over the input tree.
34  *
35  * Currently this class has two descendants
36  * - PathJSON: work with JSON input tree
37  * - PathYAML: work with YAML input tree
38  */
39 class PathBase {
40 public:
41 
42  /**
43  * Thrown if a reference in the input file
44  */
45  TYPEDEF_ERR_INFO(EI_ErrorAddress, std::string);
46  TYPEDEF_ERR_INFO(EI_RefAddress, std::string);
47  TYPEDEF_ERR_INFO(EI_JsonFile, std::string);
48  TYPEDEF_ERR_INFO(EI_RefStr, std::string);
49  TYPEDEF_ERR_INFO(EI_Specification, std::string);
50  DECLARE_INPUT_EXCEPTION(ExcRefOfWrongType,
51  << "Reference at address "
52  << EI_ErrorAddress::qval << " has wrong type, should by string.");
53  DECLARE_INPUT_EXCEPTION(ExcReferenceNotFound,
54  << "Error in input file: " << EI_JsonFile::qval << "\nReference {REF=\"" << EI_RefStr::val << "\"} at address " << EI_RefAddress::qval << " not found.\n"
55  << "failed to follow at address: " << EI_ErrorAddress::qval << " because " << EI_Specification::val);
56 
57  /// Must have virtual destructor to call the right one form child.
58  virtual ~PathBase() {};
59 
60 
61  /**
62  * @brief Returns level of actual path.
63  *
64  * Root has level == 0.
65  */
66  virtual int level() const =0;
67 
68  /**
69  * @brief Check if current head node is containing one key REF of type string.
70  *
71  * If yes, creates a new path object given by address string possibly relative to the current
72  * path. In other else return NULL.
73  *
74  * This method has the meaning only for JSON. For YAML (YAML has native references) return
75  * always NULL.
76  */
77  virtual PathBase * find_ref_node() =0;
78 
79  /// Create copy of derived class.
80  virtual PathBase * clone() const =0;
81 
82  /// Output to the given stream.
83  void output(std::ostream &stream) const;
84 
85  /// Check if type of head node is null
86  virtual bool is_null_type() const =0;
87 
88  /// Get boolean value of head node or throw exception
89  virtual bool get_bool_value() const =0;
90 
91  /// Get integer value of head node or throw exception
92  virtual std::int64_t get_int_value() const =0;
93 
94  /// Get double value of head node or throw exception
95  virtual double get_double_value() const =0;
96 
97  /// Get string value of head node or throw exception
98  virtual std::string get_string_value() const =0;
99 
100  /// Get short string description of node type, method is used for printout of messages
101  std::string get_node_type(unsigned int type_idx) const;
102 
103  /// Get index of head type, value corresponds with order in @p json_type_names vector
104  virtual unsigned int get_node_type_index() const =0;
105 
106  /// Get set of keys of head type record, if head type is not record return false
107  virtual bool get_record_key_set(std::set<std::string> &) const =0;
108 
109  /// Check empty Input Type Record, necessary for correct proccess of YAML output, for JSON has no effect
110  virtual bool is_effectively_null() const =0;
111 
112  /// Get size of array (sequence type), if object is not array return -1
113  virtual int get_array_size() const =0;
114 
115  /// Check if type of head node is record
116  virtual bool is_record_type() const =0;
117 
118  /// Check if type of head node is array
119  virtual bool is_array_type() const =0;
120 
121  /**
122  * @brief Dive one level down into path hierarchy.
123  *
124  * Store current path and returns true if pointer to new node is not NULL.
125  */
126  virtual bool down(unsigned int index) =0;
127 
128  /**
129  * @brief Dive one level down into path hierarchy.
130  *
131  * Store current path and returns true if pointer to new node is not NULL.
132  */
133  virtual bool down(const std::string& key) =0;
134 
135  /// Return one level up in the hierarchy.
136  virtual void up() =0;
137 
138  /// Move to root node.
139  void go_to_root();
140 
141  /// Returns string address of current position.
142  std::string as_string() const;
143 
144  /**
145  * @brief Gets name of descendant of Abstract.
146  *
147  * - for JSON returns value of TYPE key
148  * - for YAML returns value of tag
149  *
150  * If descendant name is not found returns empty string.
151  */
152  virtual std::string get_descendant_name() const =0;
153 
154 protected:
155  /// Forbid default constructor.
156  PathBase();
157 
158  /**
159  * @brief One level of the @p path_ is either index (nonnegative int) in array or string key in a json object.
160  *
161  * For the first type we save index into first part of the pair and empty string to the second.
162  * For the later type of level, we save -1 for index and the key into the secodn part of the pair.
163  */
165 
166  /**
167  * @brief Names of all possible node types in parsed input tree.
168  *
169  * Names are provided by JSON Spirit or YAML-cpp library.
170  * Initialized in constructor.
171  *
172  */
174 
175 };
176 
177 
178 
179 } // namespace Input
180 
181 
182 
183 #endif /* PATH_BASE_HH_ */
virtual PathBase * clone() const =0
Create copy of derived class.
virtual unsigned int get_node_type_index() const =0
Get index of head type, value corresponds with order in json_type_names vector.
virtual std::string get_string_value() const =0
Get string value of head node or throw exception.
virtual bool down(unsigned int index)=0
Dive one level down into path hierarchy.
Base abstract class used by ReaderToStorage class to iterate over the input tree. ...
Definition: path_base.hh:39
virtual bool is_array_type() const =0
Check if type of head node is array.
virtual bool get_record_key_set(std::set< std::string > &) const =0
Get set of keys of head type record, if head type is not record return false.
std::string as_string() const
Returns string address of current position.
Definition: path_base.cc:48
std::vector< std::string > json_type_names
Names of all possible node types in parsed input tree.
Definition: path_base.hh:173
PathBase()
Forbid default constructor.
Definition: path_base.cc:26
virtual std::string get_descendant_name() const =0
Gets name of descendant of Abstract.
std::string get_node_type(unsigned int type_idx) const
Get short string description of node type, method is used for printout of messages.
Definition: path_base.cc:64
void go_to_root()
Move to root node.
Definition: path_base.cc:56
TYPEDEF_ERR_INFO(EI_ErrorAddress, std::string)
virtual bool get_bool_value() const =0
Get boolean value of head node or throw exception.
virtual ~PathBase()
Must have virtual destructor to call the right one form child.
Definition: path_base.hh:58
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
virtual int get_array_size() const =0
Get size of array (sequence type), if object is not array return -1.
DECLARE_INPUT_EXCEPTION(ExcRefOfWrongType,<< "Reference at address "<< EI_ErrorAddress::qval<< " has wrong type, should by string.")
virtual int level() const =0
Returns level of actual path.
virtual bool is_record_type() const =0
Check if type of head node is record.
void output(std::ostream &stream) const
Output to the given stream.
Definition: path_base.cc:31
virtual double get_double_value() const =0
Get double value of head node or throw exception.
virtual PathBase * find_ref_node()=0
Check if current head node is containing one key REF of type string.
virtual std::int64_t get_int_value() const =0
Get integer value of head node or throw exception.
virtual bool is_null_type() const =0
Check if type of head node is null.
virtual void up()=0
Return one level up in the hierarchy.
virtual bool is_effectively_null() const =0
Check empty Input Type Record, necessary for correct proccess of YAML output, for JSON has no effect...