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