Flow123d  3.9.0-72cb42cb8
reader_to_storage.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 reader_to_storage.hh
15  * @brief
16  */
17 
18 #ifndef READER_TO_STORAGE_HH_
19 #define READER_TO_STORAGE_HH_
20 
21 
22 #include <sstream>
23 
24 
25 #include "system/file_path.hh"
26 #include <sys/types.h> // for int64_t
27 
28 #include <memory> // for shared_ptr
29 #include <string> // for string
30 #include "system/exceptions.hh" // for operator<<, ExcStream, EI, TYP...
31 namespace Input { class PathBase; }
32 namespace Input { class StorageBase; }
33 namespace Input { namespace Type { class Abstract; } }
34 namespace Input { namespace Type { class Array; } }
35 namespace Input { namespace Type { class Bool; } }
36 namespace Input { namespace Type { class Double; } }
37 namespace Input { namespace Type { class Integer; } }
38 namespace Input { namespace Type { class Record; } }
39 namespace Input { namespace Type { class Selection; } }
40 namespace Input { namespace Type { class String; } }
41 namespace Input { namespace Type { class Tuple; } }
42 namespace Input { namespace Type { class TypeBase; } }
43 
44 namespace Input { namespace Type { class Default; } }
45 namespace Input { namespace Type { class TypeBase; } }
46 
47 
48 
49 namespace Input {
50 
51 using namespace std;
52 
53 //class ReaderInternalBase;
54 //class ReaderInternalCsvInclude;
55 
56 
57 
58 /// Possible formats of input files.
59 typedef enum {
62 } FileFormat;
63 
64 
65 
66 /**
67  * @brief Enum of possible input types.
68  *
69  * Values in @p json_type_names must be stored in same order.
70  */
71 typedef enum {
73 } ValueTypes;
74 
75 
76 
77 /**
78  * @brief Reader for (slightly) modified input files.
79  *
80  * This class implements a reader of modified input file format (JSON or YAML). The modifications include
81  * shell-like comments (using hash '#' character), this is implemented in comment_filter.hh, optional quoting of
82  * keys in JSON objects that do not contain spaces, and possibility to use '=' instead of ':'. So you can write:
83  * @code
84  * { key1="text", key2=2, "key 3"=4 }
85  * @endcode
86  * Note, however, that our input interface allows only C identifiers for keys. The reader use json_spirit library
87  * (based on Spirit parser from Boost) with slightly modified grammar.
88  *
89  * The input file is at first read and parsed by json_spirit. Then ReaderToStorage pass through tree with parsed data along
90  * with passing through declaration tree. The input data are check against declaration and stored in the Storage tree.
91  *
92  * Accessor to the root record is provided by ReaderToStorage::get_root_interface<T> method template.
93  *
94  * @ingroup input
95  */
97 public:
98 
99  /**
100  * @brief Read a storage from input stream.
101  *
102  * Parameter @p root_type provides input type tree declaration. See @p read_from_stream for details.
103  */
104  ReaderToStorage(const FilePath &in_file, Type::TypeBase &root_type);
105 
106  /// Read a storage from string (e.g. complex default value).
107  ReaderToStorage( const string &default_str, Type::TypeBase &root_type, FileFormat format);
108 
109  /**
110  * @brief Returns the root accessor.
111  *
112  * The template type \p T should correspond to the kind of the input type at root of the declaration tree.
113  */
114  template <class T>
115  T get_root_interface() const;
116 
117 
118  /**
119  * @brief Default constructor.
120  *
121  * Provides common initialization for public constructors.
122  */
123  ReaderToStorage();
124 
125  /**
126  * @brief This method actually reads the given stream \p in
127  *
128  * Checks the data just read against the declaration tree given by \p root_type, and
129  * store the data into private storage tree using \p StorageBase classes.
130  */
131  void read_stream(istream &in, const Type::TypeBase &root_type, FileFormat format);
132 
133 protected:
134 
135  /// Getter for root of the storage tree.
136  StorageBase *get_storage();
137 
138 
139  /// Storage of the read and checked input data
141 
142  /// Root of the declaration tree of the data in the storage.
144 
145  friend class Type::Default;
146  friend class ReaderInternalBase;
147 
148 };
149 
150 
151 
152 
153 
154 
155 
156 
157 
158 
159 
160 } // namespace Input
161 
162 
163 
164 #endif /* READER_TO_STORAGE_HH_ */
format
manipulators::Array< T, Delim > format(T const &deduce, Delim delim=", ")
Definition: logger.hh:325
Input::StorageBase
Base class for nodes of a data storage tree.
Definition: storage.hh:68
Input::int_type
@ int_type
Definition: reader_to_storage.hh:72
Input::ReaderToStorage::storage_
StorageBase * storage_
Storage of the read and checked input data.
Definition: reader_to_storage.hh:140
Input::str_type
@ str_type
Definition: reader_to_storage.hh:72
Input::ReaderToStorage
Reader for (slightly) modified input files.
Definition: reader_to_storage.hh:96
Input::FileFormat
FileFormat
Possible formats of input files.
Definition: reader_to_storage.hh:59
Input::ReaderToStorage::root_type_
const Type::TypeBase * root_type_
Root of the declaration tree of the data in the storage.
Definition: reader_to_storage.hh:143
Input
Abstract linear system class.
Definition: balance.hh:40
file_path.hh
FilePath
Dedicated class for storing path to input and output files.
Definition: file_path.hh:54
Input::undef_type
@ undef_type
Definition: reader_to_storage.hh:72
Input::scalar_type
@ scalar_type
Definition: reader_to_storage.hh:72
Input::real_type
@ real_type
Definition: reader_to_storage.hh:72
Input::bool_type
@ bool_type
Definition: reader_to_storage.hh:72
exceptions.hh
Input::Type::Default
Class Input::Type::Default specifies default value of keys of a Input::Type::Record.
Definition: type_record.hh:61
Input::obj_type
@ obj_type
Definition: reader_to_storage.hh:72
json_spirit::read_stream
bool read_stream(Istream_type &is, Value_type &value)
Definition: json_spirit_reader_template.h:656
Input::array_type
@ array_type
Definition: reader_to_storage.hh:72
Input::format_JSON
@ format_JSON
Definition: reader_to_storage.hh:60
std
Definition: doxy_dummy_defs.hh:5
Input::null_type
@ null_type
Definition: reader_to_storage.hh:72
Input::format_YAML
@ format_YAML
Definition: reader_to_storage.hh:61
Input::Type::TypeBase
Base of classes for declaring structure of the input data.
Definition: type_base.hh:92
Input::ValueTypes
ValueTypes
Enum of possible input types.
Definition: reader_to_storage.hh:71
Input::ReaderInternalBase
Internal part of IST reader.
Definition: reader_internal_base.hh:42