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