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