Flow123d  last_with_con_2.0.0-4-g42e6930
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 
117  /// Perform resolution according to actual @p type (using typeid) and call particular print_impl method.
118  void print_base(ostream& stream, const TypeBase *type);
119 
120 
121  /// Implements printout of Record @p type
122  virtual void print_impl(ostream& stream, const Record *type) = 0;
123  /// Implements printout of Array @p type
124  virtual void print_impl(ostream& stream, const Array *type) = 0;
125  /// Implements printout of Abstract @p type
126  virtual void print_impl(ostream& stream, const Abstract *type) = 0;
127  /// Implements printout of AdHocAbstract @p type
128  virtual void print_impl(ostream& stream, const AdHocAbstract *type) = 0;
129  /// Implements printout of Selection @p type
130  virtual void print_impl(ostream& stream, const Selection *type) = 0;
131  /// Implements printout of Integer @p type
132  virtual void print_impl(ostream& stream, const Integer *type) = 0;
133  /// Implements printout of Double @p type
134  virtual void print_impl(ostream& stream, const Double *type) = 0;
135  /// Implements printout of Bool @p type
136  virtual void print_impl(ostream& stream, const Bool *type) = 0;
137  /// Implements printout of String @p type
138  virtual void print_impl(ostream& stream, const String *type) = 0;
139  /// Implements printout of FileName @p type
140  virtual void print_impl(ostream& stream, const FileName *type) = 0;
141  /// Implements printout of FileName @p type
142  virtual void print_impl(ostream& stream, const Parameter *type) = 0;
143 
144  /**
145  * @brief Write out a string with given padding of every new line.
146  *
147  * @param stream Output stream
148  * @param str Printed description
149  * @param padding Number of spaces added from left
150  * @param hash_count Count of '#' chars in description
151  */
152  void write_description(std::ostream& stream, const string& str, unsigned int padding, unsigned int hash_count = 1);
153  /**
154  * @brief Write value stored in @p dft.
155  *
156  * Enclose value in quotes if it's needed or write info that value is optional or obligatory.
157  */
158  void write_default_value(std::ostream& stream, Default dft);
159  /// Clear all data of processed types
160  void clear_processed_types();
161 
162  /**
163  * @brief Returns true if the type was printed out
164  *
165  * Checks if the @p processed_types_hash_ contains hash of given type.
166  */
167  bool was_written(std::size_t hash);
168 
169 
170  /// Padding of new level of printout, used where we use indentation.
171  static const unsigned int padding_size = 4;
172  /// Type of documentation output
174  /// temporary value for printout of description (used in std::setw function)
175  unsigned int size_setw_;
176 
177  /// Set of hashes of outputed types. Should replace keys.
178  std::set<std::size_t> processed_types_hash_;
179  /// Content hash of full IST, value is used for key IST_hash
181 
182 };
183 
184 
185 
186 
187 /**********************************************************************************************************************/
188 
189 /**
190  * @brief Class for create text documentation
191  *
192  * Record, Abstract and Selection are represented by block of text that contains type name, name, description
193  * and count and list of keys (for Record), descendants (for Abstract) or values (for Selection).
194  *
195  * In list are displayed information about subtypes, e.g. type name, description, value, range of numeric values etc.
196  *
197  * @ingroup input_types
198  */
199 class OutputText : public OutputBase {
200 public:
201  /**
202  * @brief Constructor
203  *
204  * @param type Stores input sequence
205  */
206  OutputText(const TypeBase *type) : OutputBase(), type_(type) {}
207 
208  ostream& print(ostream& stream) override;
209 protected:
210  void print_impl(ostream& stream, const Record *type);
211  void print_impl(ostream& stream, const Array *type);
212  void print_impl(ostream& stream, const Abstract *type);
213  void print_impl(ostream& stream, const AdHocAbstract *type);
214  void print_impl(ostream& stream, const Selection *type);
215  void print_impl(ostream& stream, const Integer *type);
216  void print_impl(ostream& stream, const Double *type);
217  void print_impl(ostream& stream, const Bool *type);
218  void print_impl(ostream& stream, const String *type);
219  void print_impl(ostream& stream, const FileName *type);
220  void print_impl(ostream& stream, const Parameter *type);
221 
222  /// Object for which is created printout
223  const TypeBase *type_;
224 };
225 
226 
227 
228 
229 
230 
231 
232 /**
233  * @brief Class for create JSON machine readable documentation
234  *
235  * Every type is represented by one JSON object, for Selection e.g.:
236  * "name" : (string),
237  * "full_name" : (string),
238  * "type" : "Selection",
239  * "values" : [ { "value" : (int), "name": (string), "description" : (string) } ]
240  *
241  * @ingroup input_types
242  */
244 public:
245  /// Simple constructor.
246  OutputJSONMachine(RevNumData rev_num_data);
247  /// Constructor allowing to set root_type_
248  OutputJSONMachine(const Record &root_type, RevNumData rev_num_data);
249 
250  ostream& print(ostream& stream) override;
251 
252 
253 protected:
254  /// Replace slashes, quotes and special chars with escaped format of these chars.
255  std::string escape_description(std::string desc);
256 
257  /**
258  * @brief Print header of a JSON object describing single TypeBase instance.
259  *
260  * @param type - type to print
261  * @param input_type - name of TypeBase descendant.
262  */
263  void print_type_header(ostream& stream, const TypeBase *type);
264 
265  void print_impl(ostream& stream, const Record *type);
266  void print_impl(ostream& stream, const Array *type);
267  void print_impl(ostream& stream, const Abstract *type);
268  void print_impl(ostream& stream, const AdHocAbstract *type);
269  void print_impl(ostream& stream, const Selection *type);
270  void print_impl(ostream& stream, const Integer *type);
271  void print_impl(ostream& stream, const Double *type);
272  void print_impl(ostream& stream, const Bool *type);
273  void print_impl(ostream& stream, const String *type);
274  void print_impl(ostream& stream, const FileName *type);
275  void print_impl(ostream& stream, const Parameter *type);
276 
277 
278  /// Print all keys of Abstract type or AdHocAbstract type
279  void print_abstract_record_keys(ostream& stream, const Abstract *type);
280 
281  /// Print actual version of program.
282  void print_program_info(ostream& stream);
283 
284  /// Print @p full_hash_ key.
285  void print_full_hash(ostream& stream);
286 
287 
288  /**
289  * @brief Header of the format, printed before call of version print.
290  *
291  * See @p print(stream) method
292  */
293  std::string format_head;
294  /**
295  * @brief Inner part of the format, printed before first call of recursive print.
296  *
297  * see @p print(stream) method
298  */
299  std::string format_inner;
300  /**
301  * @brief Tail of the format, printed after all recursive prints are finished and before full hash prints.
302  *
303  * see @p print(stream) method
304  */
305  std::string format_full_hash;
306  /**
307  * @brief Tail of the format, printed after all recursive prints are finished.
308  *
309  * see @p print(stream) method
310  */
311  std::string format_tail;
312  /// Contains version of program and other base data
314 
315  /**
316  * @brief Root type for output.
317  *
318  * Have to be printed as first, then we pass through the individual type lists to output everything.
319  */
321 };
322 
323 
324 /// Redirect text output to @p stream.
325 std::ostream& operator<<(std::ostream& stream, OutputText type_output);
326 /// Redirect machine readable output to @p stream.
327 std::ostream& operator<<(std::ostream& stream, OutputJSONMachine type_output);
328 
329 
330 
331 } // closing namespace Type
332 } // closing namespace Input
333 
334 
335 
336 
337 #endif /* TYPE_OUTPUT_HH_ */
Base of classes for declaring structure of the input data.
Definition: type_base.hh:79
const TypeBase * type_
Object for which is created printout.
Definition: type_output.hh:223
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:311
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:293
Class for declaration of the input of type Bool.
Definition: type_base.hh:434
std::string format_inner
Inner part of the format, printed before first call of recursive print.
Definition: type_output.hh:299
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:305
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:173
Class for create JSON machine readable documentation.
Definition: type_output.hh:243
Class for declaration of the input data that are file names.
Definition: type_base.hh:597
Class for declaration of polymorphic Record.
Record root_type_
Root type for output.
Definition: type_output.hh:320
TypeBase::TypeHash full_hash_
Content hash of full IST, value is used for key IST_hash.
Definition: type_output.hh:180
Class for declaration of the integral input data.
Definition: type_base.hh:465
std::string branch
Actual branch of application.
Definition: type_output.hh:40
Class for declaration of inputs sequences.
Definition: type_base.hh:321
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:313
Class for declaration of the input data that are floating point numbers.
Definition: type_base.hh:518
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:175
std::set< std::size_t > processed_types_hash_
Set of hashes of outputed types. Should replace keys.
Definition: type_output.hh:178
Class for declaration of polymorphic Record.
OutputText(const TypeBase *type)
Constructor.
Definition: type_output.hh:206
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:85
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:169
Class for create text documentation.
Definition: type_output.hh:199
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:568
std::size_t TypeHash
Type returned by content_hash methods.
Definition: type_base.hh:82
Template for classes storing finite set of named values.
std::string url
Url of application.
Definition: type_output.hh:41