Flow123d  release_2.1.0-84-g6a13a75
type_output.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 type_output.hh
15  * @brief
16  */
17 
18 #ifndef TYPE_OUTPUT_HH_
19 #define TYPE_OUTPUT_HH_
20 
21 
23 #include "input/type_base.hh"
24 #include "input/type_record.hh"
25 #include "input/type_abstract.hh"
26 #include <string>
27 #include <ostream>
28 #include <vector>
29 #include <memory>
30 
31 
32 
33 namespace Input {
34 
35 namespace Type {
36 
37 using namespace std;
38 
39 /// Stores version of program and other base data of application
40 struct RevNumData {
41  std::string version; ///< Actual version of application.
42  std::string revision; ///< Actual revision of application.
43  std::string branch; ///< Actual branch of application.
44  std::string url; ///< Url of application.
45 };
46 
47 /**
48  * @brief Base abstract class for output description of the Input::Type tree.
49  *
50  * Output into various formats is implemented by derived classes:
51  * - OutputText - for human readable description (e. g. used for printout of errors)
52  * - OutputJSONMachine - for full machine readable description in standard JSON format
53  *
54  * Usage example:
55  * @code
56  * cout << OutputText( &my_record) << endl;
57  * @endcode
58  *
59  * @ingroup input_types
60  */
61 class OutputBase {
62 public:
63 
64  /**
65  * @brief Performs output of the documentation into given @p stream.
66  *
67  * The same effect has the reloaded operator '<<'.
68  * Returns reference to the same stream.
69  */
70  virtual ostream& print(ostream& stream) = 0;
71 
72 protected:
73  /**
74  * @brief Types of documentation output.
75  *
76  * Used in print_impl methods for basic and full printout of Input::Type
77  */
79  key_record, ///< Printout only basic data
80  full_record ///< Printout full documentation
81  };
82 
83 
84 
85  /// Constructor
86  OutputBase();
87 
88 
89  /// Destructor
90  virtual ~OutputBase();
91 
92  // data getters
93  /// Gets range of array
94  void get_array_sizes(Array array, unsigned int &lower , unsigned int &upper );
95  /// Gets description of the given record type.
96  const string & get_record_description(const Record *rec);
97  /// Gets description of the given abstract type.
98  const string & get_abstract_description(const Abstract *a_rec);
99  /// Gets range of integer
100  void get_integer_bounds(Integer integer, int &lower , int &upper );
101  /// Gets range of double
102  void get_double_bounds(Double dbl, double &lower , double &upper );
103  /// Gets pointer of parent Abstract for given Record
104  void get_parent_vec(Record rec, std::vector< std::shared_ptr<Abstract> > &parent_vec);
105  /// Gets pointer of inner type for given Array
106  void get_array_type(Array array, std::shared_ptr<TypeBase> &arr_type);
107  /// Gets values of default for given record key
108  void get_default(Record::KeyIter it, string &type, string &value);
109  /// Gets description of the given selection type.
110  const string & get_selection_description(const Selection *sel);
111  /// Gets ancestor_.type_name of the given AdHocAbstract type.
112  void get_adhoc_parent_name(const AdHocAbstract *a_rec, string &parent_name);
113  /// Gets iterator to begin of ancestor_.child_data_ of the given AdHocAbstract type.
114  Abstract::ChildDataIter get_adhoc_parent_data(const AdHocAbstract *a_rec);
115  /// Gets data of attributes and parameters of the given type.
116  void get_attr_and_param_data(const TypeBase *type, TypeBase::attribute_map &attr_map,
117  TypeBase::TypeHash &generic_type_hash, TypeBase::json_string &parameter_map_to_json);
118 
119  template <class T>
120  void print_generic(ostream& stream, const TypeBase *type);
121 
122  /// Perform resolution according to actual @p type (using typeid) and call particular print_impl method.
123  void print_base(ostream& stream, const TypeBase *type);
124 
125 
126  /// Implements printout of Record @p type
127  virtual void print_impl(ostream& stream, const Record *type) = 0;
128  /// Implements printout of Array @p type
129  virtual void print_impl(ostream& stream, const Array *type) = 0;
130  /// Implements printout of Abstract @p type
131  virtual void print_impl(ostream& stream, const Abstract *type) = 0;
132  /// Implements printout of AdHocAbstract @p type
133  virtual void print_impl(ostream& stream, const AdHocAbstract *type) = 0;
134  /// Implements printout of Selection @p type
135  virtual void print_impl(ostream& stream, const Selection *type) = 0;
136  /// Implements printout of Integer @p type
137  virtual void print_impl(ostream& stream, const Integer *type) = 0;
138  /// Implements printout of Double @p type
139  virtual void print_impl(ostream& stream, const Double *type) = 0;
140  /// Implements printout of Bool @p type
141  virtual void print_impl(ostream& stream, const Bool *type) = 0;
142  /// Implements printout of String @p type
143  virtual void print_impl(ostream& stream, const String *type) = 0;
144  /// Implements printout of FileName @p type
145  virtual void print_impl(ostream& stream, const FileName *type) = 0;
146  /// Implements printout of FileName @p type
147  virtual void print_impl(ostream& stream, const Parameter *type) = 0;
148 
149  /**
150  * @brief Write out a string with given padding of every new line.
151  *
152  * @param stream Output stream
153  * @param str Printed description
154  * @param padding Number of spaces added from left
155  * @param hash_count Count of '#' chars in description
156  */
157  void write_description(std::ostream& stream, const string& str, unsigned int padding, unsigned int hash_count = 1);
158  /**
159  * @brief Write value stored in @p dft.
160  *
161  * Enclose value in quotes if it's needed or write info that value is optional or obligatory.
162  */
163  void write_default_value(std::ostream& stream, Default dft);
164  /// Clear all data of processed types
165  void clear_processed_types();
166 
167  /**
168  * @brief Returns true if the type was printed out
169  *
170  * Checks if the @p processed_types_hash_ contains hash of given type.
171  */
172  bool was_written(std::size_t hash);
173 
174 
175  /// Padding of new level of printout, used where we use indentation.
176  static const unsigned int padding_size = 4;
177  /// Type of documentation output
179  /// temporary value for printout of description (used in std::setw function)
180  unsigned int size_setw_;
181 
182  /// Set of hashes of outputed types. Should replace keys.
183  std::set<std::size_t> processed_types_hash_;
184  /// Content hash of full IST, value is used for key IST_hash
186 
187 };
188 
189 
190 
191 
192 /**********************************************************************************************************************/
193 
194 /**
195  * @brief Class for create text documentation
196  *
197  * Record, Abstract and Selection are represented by block of text that contains type name, name, description
198  * and count and list of keys (for Record), descendants (for Abstract) or values (for Selection).
199  *
200  * In list are displayed information about subtypes, e.g. type name, description, value, range of numeric values etc.
201  *
202  * @ingroup input_types
203  */
204 class OutputText : public OutputBase {
205 public:
206  /**
207  * @brief Constructor
208  *
209  * @param type Stores input sequence
210  */
211  OutputText(const TypeBase *type) : OutputBase(), type_(type) {}
212 
213  ostream& print(ostream& stream) override;
214 protected:
215  void print_impl(ostream& stream, const Record *type);
216  void print_impl(ostream& stream, const Array *type);
217  void print_impl(ostream& stream, const Abstract *type);
218  void print_impl(ostream& stream, const AdHocAbstract *type);
219  void print_impl(ostream& stream, const Selection *type);
220  void print_impl(ostream& stream, const Integer *type);
221  void print_impl(ostream& stream, const Double *type);
222  void print_impl(ostream& stream, const Bool *type);
223  void print_impl(ostream& stream, const String *type);
224  void print_impl(ostream& stream, const FileName *type);
225  void print_impl(ostream& stream, const Parameter *type);
226 
227  /// Object for which is created printout
228  const TypeBase *type_;
229 };
230 
231 
232 
233 
234 
235 
236 
237 /**
238  * @brief Class for create JSON machine readable documentation
239  *
240  * Every type is represented by one JSON object, for Selection e.g.:
241  * "name" : (string),
242  * "full_name" : (string),
243  * "type" : "Selection",
244  * "values" : [ { "value" : (int), "name": (string), "description" : (string) } ]
245  *
246  * @ingroup input_types
247  */
249 public:
250  /// Simple constructor.
251  OutputJSONMachine(RevNumData rev_num_data);
252  /// Constructor allowing to set root_type_
253  OutputJSONMachine(const Record &root_type, RevNumData rev_num_data);
254 
255  ostream& print(ostream& stream) override;
256 
257 
258 protected:
259  /// Replace slashes, quotes and special chars with escaped format of these chars.
260  std::string escape_description(std::string desc);
261 
262  /**
263  * @brief Print header of a JSON object describing single TypeBase instance.
264  *
265  * @param type - type to print
266  * @param input_type - name of TypeBase descendant.
267  */
268  void print_type_header(ostream& stream, const TypeBase *type);
269 
270  void print_impl(ostream& stream, const Record *type);
271  void print_impl(ostream& stream, const Array *type);
272  void print_impl(ostream& stream, const Abstract *type);
273  void print_impl(ostream& stream, const AdHocAbstract *type);
274  void print_impl(ostream& stream, const Selection *type);
275  void print_impl(ostream& stream, const Integer *type);
276  void print_impl(ostream& stream, const Double *type);
277  void print_impl(ostream& stream, const Bool *type);
278  void print_impl(ostream& stream, const String *type);
279  void print_impl(ostream& stream, const FileName *type);
280  void print_impl(ostream& stream, const Parameter *type);
281 
282 
283  /// Print all keys of Abstract type or AdHocAbstract type
284  void print_abstract_record_keys(ostream& stream, const Abstract *type);
285 
286  /// Print actual version of program.
287  void print_program_info(ostream& stream);
288 
289  /// Print @p full_hash_ key.
290  void print_full_hash(ostream& stream);
291 
292 
293  /**
294  * @brief Header of the format, printed before call of version print.
295  *
296  * See @p print(stream) method
297  */
298  std::string format_head;
299  /**
300  * @brief Inner part of the format, printed before first call of recursive print.
301  *
302  * see @p print(stream) method
303  */
304  std::string format_inner;
305  /**
306  * @brief Tail of the format, printed after all recursive prints are finished and before full hash prints.
307  *
308  * see @p print(stream) method
309  */
310  std::string format_full_hash;
311  /**
312  * @brief Tail of the format, printed after all recursive prints are finished.
313  *
314  * see @p print(stream) method
315  */
316  std::string format_tail;
317  /// Contains version of program and other base data
319 
320  /**
321  * @brief Root type for output.
322  *
323  * Have to be printed as first, then we pass through the individual type lists to output everything.
324  */
326 };
327 
328 
329 /// Redirect text output to @p stream.
330 std::ostream& operator<<(std::ostream& stream, OutputText type_output);
331 /// Redirect machine readable output to @p stream.
332 std::ostream& operator<<(std::ostream& stream, OutputJSONMachine type_output);
333 
334 
335 
336 } // closing namespace Type
337 } // closing namespace Input
338 
339 
340 
341 
342 #endif /* TYPE_OUTPUT_HH_ */
Base of classes for declaring structure of the input data.
Definition: type_base.hh:93
const TypeBase * type_
Object for which is created printout.
Definition: type_output.hh:228
std::vector< struct Key >::const_iterator KeyIter
Public typedef of constant iterator into array of keys.
Definition: type_record.hh:211
std::string format_tail
Tail of the format, printed after all recursive prints are finished.
Definition: type_output.hh:316
std::vector< Record >::const_iterator ChildDataIter
Public typedef of constant iterator into array of keys.
FMT_API void print(std::FILE *f, CStringRef format_str, ArgList args)
Definition: format.cc:489
Class Input::Type::Default specifies default value of keys of a Input::Type::Record.
Definition: type_record.hh:56
std::string format_head
Header of the format, printed before call of version print.
Definition: type_output.hh:298
Class for declaration of the input of type Bool.
Definition: type_base.hh:452
std::string format_inner
Inner part of the format, printed before first call of recursive print.
Definition: type_output.hh:304
std::string format_full_hash
Tail of the format, printed after all recursive prints are finished and before full hash prints...
Definition: type_output.hh:310
Base abstract class for output description of the Input::Type tree.
Definition: type_output.hh:61
Class for representing parametric types in IST.
Definition: type_generic.hh:52
DocumentationType doc_type_
Type of documentation output.
Definition: type_output.hh:178
Class for create JSON machine readable documentation.
Definition: type_output.hh:248
Class for declaration of the input data that are file names.
Definition: type_base.hh:611
Class for declaration of polymorphic Record.
Record root_type_
Root type for output.
Definition: type_output.hh:325
TypeBase::TypeHash full_hash_
Content hash of full IST, value is used for key IST_hash.
Definition: type_output.hh:185
Class for declaration of the integral input data.
Definition: type_base.hh:483
std::string branch
Actual branch of application.
Definition: type_output.hh:43
Class for declaration of inputs sequences.
Definition: type_base.hh:339
static constexpr bool value
Definition: json.hpp:87
Stores version of program and other base data of application.
Definition: type_output.hh:40
RevNumData rev_num_data_
Contains version of program and other base data.
Definition: type_output.hh:318
Class for declaration of the input data that are floating point numbers.
Definition: type_base.hh:534
Printout only basic data.
Definition: type_output.hh:79
unsigned int size_setw_
temporary value for printout of description (used in std::setw function)
Definition: type_output.hh:180
std::set< std::size_t > processed_types_hash_
Set of hashes of outputed types. Should replace keys.
Definition: type_output.hh:183
Class for declaration of polymorphic Record.
OutputText(const TypeBase *type)
Constructor.
Definition: type_output.hh:211
std::string revision
Actual revision of application.
Definition: type_output.hh:42
std::string json_string
String stored in JSON format.
Definition: type_base.hh:99
std::ostream & operator<<(std::ostream &stream, const TypeBase &type)
For convenience we provide also redirection operator for output documentation of Input:Type classes...
Definition: type_base.cc:215
Class for create text documentation.
Definition: type_output.hh:204
Record type proxy class.
Definition: type_record.hh:177
DocumentationType
Types of documentation output.
Definition: type_output.hh:78
std::string version
Actual version of application.
Definition: type_output.hh:41
Class for declaration of the input data that are in string format.
Definition: type_base.hh:582
std::size_t TypeHash
Type returned by content_hash methods.
Definition: type_base.hh:96
Template for classes storing finite set of named values.
std::string url
Url of application.
Definition: type_output.hh:44