Flow123d  last_with_con_2.0.0-4-g42e6930
path_json.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_json.hh
15  * @brief
16  */
17 
18 #ifndef PATH_JSON_HH_
19 #define PATH_JSON_HH_
20 
21 #include <memory>
22 #include "input/path_base.hh"
24 
25 
26 namespace Input {
27 
28 
29 
30 /**
31  * @brief Class used by ReaderToStorage class to iterate over the JSON tree provided by json_spirit library.
32  *
33  * This class keeps whole path from the root of the JSON tree to the current node. We store nodes along path in \p nodes_
34  * and address of the node in \p path_.
35  *
36  * The class also contains methods for processing of special keys 'REF' and 'TYPE'. The reference is record with only one key
37  * 'REF' with a string value that contains address of the reference. The string with the address is extracted and provided by
38  * method \p JSONtoStorage::find_ref_node.
39  */
40 class PathJSON : public PathBase {
41 public:
42 
43 
44  /**
45  * @brief Constructor.
46  *
47  * Call JSON parser for given stream and create PathJSON for the root
48  * of parsed data tree.
49  */
50  PathJSON(std::istream &in);
51 
52  /**
53  * @brief Destructor.
54  *
55  * Have to cleanup nodes_.
56  */
57  ~PathJSON() override;
58 
59  /**
60  * @brief Dive into json_spirit hierarchy.
61  *
62  * Implements @p PathBase::down(unsigned int)
63  */
64  bool down(unsigned int index) override;
65 
66  /**
67  * @brief Dive into json_spirit hierarchy.
68  *
69  * Implements @p PathBase::down(const std::string&)
70  */
71  bool down(const std::string& key) override;
72 
73  /// Return one level up in the hierarchy.
74  void up() override;
75 
76  /// Implements @p PathBase::level
77  inline int level() const
78  { return nodes_.size() - 1; }
79 
80  // These methods are derived from PathBase
81  bool is_null_type() const override; ///< Implements @p PathBase::is_null_type
82  bool get_bool_value() const override; ///< Implements @p PathBase::get_bool_value
83  std::int64_t get_int_value() const override; ///< Implements @p PathBase::get_int_value
84  double get_double_value() const override; ///< Implements @p PathBase::get_double_value
85  std::string get_string_value() const override; ///< Implements @p PathBase::get_string_value
86  unsigned int get_node_type_index() const override; ///< Implements @p PathBase::get_node_type_index
87  bool get_record_key_set(std::set<std::string> &) const override; ///< Implements @p PathBase::get_record_key_set
88  bool is_effectively_null() const override; ///< Implements @p PathBase::is_effectively_null
89  int get_array_size() const override; ///< Implements @p PathBase::get_array_size
90  bool is_record_type() const override; ///< Implements @p PathBase::is_record_type
91  bool is_array_type() const override; ///< Implements @p PathBase::is_array_type
92  PathJSON * clone() const override; ///< Implements @p PathBase::clone
93  std::string get_descendant_name() const override; ///< Implements @p PathBase::get_descendant_name
94 
95  /// Implements reading of reference keys, and check of cyclic references.
96  PathBase * find_ref_node() override;
97 
98  /// Put address of actual reference to previous_references_ set
99  void remember_reference();
100 
101 
102 
103 protected:
104 
105  /**
106  * @brief Default constructor.
107  *
108  * Provides common initialization for public constructors.
109  */
110  PathJSON();
111 
112  /// Definition of JSON Spirit node.
114 
115  /// Pointer to JSON Value object at current path.
116  inline const Node * head() const
117  { return nodes_.back(); }
118 
119  /**
120  * @brief Remember used references in order to avoid detect cyclic references.
121  *
122  * In JSON we allow usage of references using special key 'REF'.
123  */
124  std::set<std::string> previous_references_;
125 
126  // Root node has to be automatically deleted.
127  std::shared_ptr<Node> root_node_;
128 
129  // Pointers to all nodes from the root up to the current path.
131 
132 };
133 
134 /**
135  * @brief Output operator for PathJSON.
136  *
137  * Mainly for debugging purposes and error messages.
138  */
139 std::ostream& operator<<(std::ostream& stream, const PathJSON& path);
140 
141 
142 
143 } // namespace Input
144 
145 
146 
147 #endif /* PATH_JSON_HH_ */
json_spirit::mValue Node
Definition of JSON Spirit node.
Definition: path_json.hh:113
bool is_array_type() const override
Implements PathBase::is_array_type.
Definition: path_json.cc:286
Class used by ReaderToStorage class to iterate over the JSON tree provided by json_spirit library...
Definition: path_json.hh:40
std::ostream & operator<<(std::ostream &stream, const Address &address)
Definition: accessors.hh:238
Base abstract class used by ReaderToStorage class to iterate over the input tree. ...
Definition: path_base.hh:39
PathBase * find_ref_node() override
Implements reading of reference keys, and check of cyclic references.
Definition: path_json.cc:118
const Node * head() const
Pointer to JSON Value object at current path.
Definition: path_json.hh:116
PathJSON * clone() const override
Implements PathBase::clone.
Definition: path_json.cc:292
std::vector< const Node * > nodes_
Definition: path_json.hh:130
bool down(unsigned int index) override
Dive into json_spirit hierarchy.
Definition: path_json.cc:77
void up() override
Return one level up in the hierarchy.
Definition: path_json.cc:105
void remember_reference()
Put address of actual reference to previous_references_ set.
Definition: path_json.cc:317
std::string get_descendant_name() const override
Implements PathBase::get_descendant_name.
Definition: path_json.cc:298
~PathJSON() override
Destructor.
Definition: path_json.cc:72
double get_double_value() const override
Implements PathBase::get_double_value.
Definition: path_json.cc:221
int level() const
Implements PathBase::level.
Definition: path_json.hh:77
bool is_null_type() const override
Implements PathBase::is_null_type.
Definition: path_json.cc:194
bool is_record_type() const override
Implements PathBase::is_record_type.
Definition: path_json.cc:280
std::int64_t get_int_value() const override
Implements PathBase::get_int_value.
Definition: path_json.cc:210
int get_array_size() const override
Implements PathBase::get_array_size.
Definition: path_json.cc:269
std::shared_ptr< Node > root_node_
Definition: path_json.hh:127
bool get_record_key_set(std::set< std::string > &) const override
Implements PathBase::get_record_key_set.
Definition: path_json.cc:251
PathJSON()
Default constructor.
Definition: path_json.cc:54
std::string get_string_value() const override
Implements PathBase::get_string_value.
Definition: path_json.cc:234
bool is_effectively_null() const override
Implements PathBase::is_effectively_null.
Definition: path_json.cc:323
std::set< std::string > previous_references_
Remember used references in order to avoid detect cyclic references.
Definition: path_json.hh:124
bool get_bool_value() const override
Implements PathBase::get_bool_value.
Definition: path_json.cc:199
unsigned int get_node_type_index() const override
Implements PathBase::get_node_type_index.
Definition: path_json.cc:245