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