Flow123d
type_output.hh
Go to the documentation of this file.
1 /*
2  * type_output.hh
3  *
4  * Created on: Nov 26, 2012
5  * Author: jb
6  */
7 
8 #ifndef TYPE_OUTPUT_HH_
9 #define TYPE_OUTPUT_HH_
10 
11 
12 #include "input/type_base.hh"
13 #include "input/type_record.hh"
14 #include <boost/regex.hpp>
15 
16 
17 namespace Input {
18 
19 namespace Type {
20 
21 /**
22  * @brief Base abstract class for output description of the Input::Type tree.
23  *
24  * Output into various formats is implemented by derived classes:
25  * - OutputText - for human readable description
26  * - OutputJSONTemplate - for creating a template of the input file
27  * - OutputLatex - for printed documentation (with support of particular Latex macros)
28  * - OutputJSONMachine - for full machine readable description in standard JSON format
29  *
30  * Usage example:
31  * @code
32  * cout << OutputText( &my_record, 3) << endl;
33  * @endcode
34  *
35  * @ingroup input_types
36  */
37 class OutputBase {
38 public:
39 
40  /**
41  * @brief Performs output of the documentation into given @p stream. The same effect has the reloaded operator '<<'.
42  * Returns reference to the same stream.
43  */
44  virtual ostream& print(ostream& stream);
45 
46  /**
47  * @brief Initializes and allocates regular expression filter @p regex_filter.
48  *
49  * Full names of Input::Type::Record objects are passed through the filter, deleting the first match of the regular expression given by @p regex_filter.
50  * Full Record description is performed only for the first occurrence of the filtered name, further Records with same filtered name are ignored
51  * (reported only in short descriptions of individual keys or array subtype, etc.) See @p ProcessedTypes for details.
52  */
53  void set_filter(string regex_filter);
54 
55 protected:
56  /**
57  * Types of documentation output.
58  *
59  * Used in print_impl methods for basic and full printout of Input::Type
60  */
62  key_record, ///< Printout only basic data
63  full_record ///< Printout full documentation
64  };
65 
66 
67 
68  /**
69  * Constructor
70  *
71  * @param type Stores input sequence
72  * @param depth Depth of output
73  */
74  OutputBase(const TypeBase *type, unsigned int depth = 0);
75 
76 
77  /// Destructor
78  virtual ~OutputBase();
79 
80  // data getters
81  /// Gets range of array
82  void get_array_sizes(Array array, unsigned int &lower , unsigned int &upper );
83  /// Gets description of the given record type.
84  const string & get_record_description(const Record *rec);
85  /// Gets record key for given index
86  void get_record_key(Record rec, unsigned int key_idx, Record::Key &key);
87  /// Gets range of integer
88  void get_integer_bounds(Integer integer, int &lower , int &upper );
89  /// Gets range of double
90  void get_double_bounds(Double dbl, double &lower , double &upper );
91  /// Gets pointer of parent AbstractRecord for given Record
92  void get_parent_ptr(Record rec, boost::shared_ptr<AbstractRecord> &parent_ptr);
93  /// Gets pointer of inner type for given Array
94  void get_array_type(Array array, boost::shared_ptr<const TypeBase> &arr_type);
95  /// Gets values of default for given record key
96  void get_default(Record::KeyIter it, string &type, string &value);
97  /// Gets description of the given selection type.
98  const string & get_selection_description(const Selection *sel);
99  /// Gets parent_name_ of the given AdHocAbstractRecord type.
100  const string & get_adhoc_parent_name(const AdHocAbstractRecord *a_rec);
101  /// Gets iterator to begin of parent_data_ of the given AdHocAbstractRecord type.
103 
104 
105  /// Gets pointer of inner data for given Record
106  const void * get_record_data(const Record *rec);
107  /// Gets pointer of inner data for given AbstractRecord
108  const void * get_abstract_record_data(const AbstractRecord *a_rec);
109  /// Gets pointer of inner data for given Selection
110  const void * get_selection_data(const Selection *sel);
111  /// Gets pointer of inner data for given Array
112  const void * get_array_data(const Array *array);
113  /// Gets pointer of inner data for given TypeBase
114  const void * get_type_base_data(const TypeBase *type);
115 
116 
117  /**
118  * Perform resolution according to actual @p type (using typeid) and call particular print_impl method.
119  */
120  void print(ostream& stream, const TypeBase *type, unsigned int depth);
121 
122 
123  /**
124  * Implements printout of Record @p type
125  */
126  virtual void print_impl(ostream& stream, const Record *type, unsigned int depth) = 0;
127  /**
128  * Implements printout of Array @p type
129  */
130  virtual void print_impl(ostream& stream, const Array *type, unsigned int depth) = 0;
131  /**
132  * Implements printout of AbstractRecord @p type
133  */
134  virtual void print_impl(ostream& stream, const AbstractRecord *type, unsigned int depth) = 0;
135  /**
136  * Implements printout of AdHocAbstractRecord @p type
137  */
138  virtual void print_impl(ostream& stream, const AdHocAbstractRecord *type, unsigned int depth) = 0;
139  /**
140  * Implements printout of Selection @p type
141  */
142  virtual void print_impl(ostream& stream, const Selection *type, unsigned int depth) = 0;
143  /**
144  * Implements printout of Integer @p type
145  */
146  virtual void print_impl(ostream& stream, const Integer *type, unsigned int depth) = 0;
147  /**
148  * Implements printout of Double @p type
149  */
150  virtual void print_impl(ostream& stream, const Double *type, unsigned int depth) = 0;
151  /**
152  * Implements printout of Bool @p type
153  */
154  virtual void print_impl(ostream& stream, const Bool *type, unsigned int depth) = 0;
155  /**
156  * Implements printout of String @p type
157  */
158  virtual void print_impl(ostream& stream, const String *type, unsigned int depth) = 0;
159  /**
160  * Implements printout of FileName @p type
161  */
162  virtual void print_impl(ostream& stream, const FileName *type, unsigned int depth) = 0;
163 
164  /**
165  * Write out a string with given padding of every new line.
166  *
167  * @param stream Output stream
168  * @param str Printed description
169  * @param padding Number of spaces added from left
170  * @param hash_count Count of '#' chars in description
171  */
172  void write_description(std::ostream& stream, const string& str, unsigned int padding, unsigned int hash_count = 1);
173  /**
174  * Write value stored in @p dft.
175  *
176  * Enclose value in quotes if it's needed or write info that value is optional or obligatory.
177  */
178  void write_default_value(std::ostream& stream, Default dft);
179 
180 
181  /// Padding of new level of printout, used where we use indentation.
182  static const unsigned int padding_size = 4;
183  /// Object for which is created printout
184  const TypeBase *type_;
185  /// Depth of printout (for value 0 is printed all input tree)
186  unsigned int depth_;
187  /// Type of documentation output
189  /// temporary value for printout of description (used in std::setw function)
190  unsigned int size_setw_;
191 
192  /**
193  * @brief Internal data class.
194  * Contains flags of written Input::Types objects and functionality of regular expression filter of Input::Types full names.
195  *
196  * Flags are stored to struct that contains unique internal data pointer of complex Input::Type,
197  * flag if extensive documentation was printed and reference to Input::Type.
198  *
199  * Regular expression filter is optional and stores printed Input::Type by filtered full_name.
200  * Input::Types with similar full names are printed only once.
201  */
203  public:
204 
205  /**
206  * Structure for flags about output of one TypeBase object in input tree
207  * Stores types what was printed
208  */
209  struct Key {
210  unsigned int key_index; ///< Position inside the record.
211  const void * type_data_; ///< Pointer to internal data of type.
212  //mutable bool extensive_doc_; ///< Flag captures if extensive documentation of type was printed.
213  mutable string reference_; ///< Reference to type.
214  };
215  /**
216  * Public typedef of constant iterator into array of keys.
217  */
219 
220 
221  /// Clear all data of processed types
222  void clear();
223 
224  /**
225  * Interface to mapping key -> index. Returns index (in continuous array) for given type.
226  */
227  unsigned int type_index(const void * type_data) const;
228 
229  //Set reference_ string of key of given type.
230  //void set_reference(const void * type_data, const string& ref);
231 
232  /// Destructor, deallocates filter_ if it was allocated.
233  ~ProcessedTypes();
234 
235  /**
236  * Returns true if the type was printed out
237  *
238  * Checks if the ProcessedTypes contains key of given type and key has true flag extensive_doc_
239  * or if the ProcessedTypes contains type of given full_name when regular expression filter_ is initialized.
240  */
241  bool was_written(const void * type_data, string full_name);
242 
243  /**
244  * Marks type as written.
245  *
246  * Inserts type to key_to_index map.
247  * If regular expression filter_ is initialized marks filtered full_name of type as written.
248  */
249  void mark_written(const void *type_data, string full_name, string reference = "");
250  /**
251  * Returns reference_ string of key of given type.
252  */
253  const string get_reference(const void * type_data) const;
254 
255  /// Database of valid keys
258 
259  /// Keys in order as they where declared.
261 
262  /// Regex filter for full names.
263  boost::regex *filter_;
264 
265  /// Regular expression of filter
266  string reg_exp_;
267 
268  /// Set of processed types by regular expression and full names
269  std::set<string> full_type_names;
270  };
271 
272  /// Stores flags and references of processed type
274 
275 };
276 
277 
278 /**********************************************************************************************************************/
279 
280 /**
281  * @brief Class for create text documentation
282  *
283  * Record, AbstractRecord and Selection are represented by block of text that contains type name, name, description
284  * and count and list of keys (for Record), descendants (for AbstractRecord) or values (for Selection).
285  *
286  * In list are displayed information about subtypes, e.g. type name, description, value, range of numeric values etc.
287  *
288  * @ingroup input_types
289  */
290 class OutputText : public OutputBase {
291 public:
292  OutputText(const TypeBase *type, unsigned int depth = 0) : OutputBase(type, depth) {}
293 
294 protected:
295 
296  void print_impl(ostream& stream, const Record *type, unsigned int depth);
297  void print_impl(ostream& stream, const Array *type, unsigned int depth);
298  void print_impl(ostream& stream, const AbstractRecord *type, unsigned int depth);
299  void print_impl(ostream& stream, const AdHocAbstractRecord *type, unsigned int depth);
300  void print_impl(ostream& stream, const Selection *type, unsigned int depth);
301  void print_impl(ostream& stream, const Integer *type, unsigned int depth);
302  void print_impl(ostream& stream, const Double *type, unsigned int depth);
303  void print_impl(ostream& stream, const Bool *type, unsigned int depth);
304  void print_impl(ostream& stream, const String *type, unsigned int depth);
305  void print_impl(ostream& stream, const FileName *type, unsigned int depth);
306 
307 
308 };
309 
310 
311 
312 
313 
314 
315 
316 /**
317  * @brief Class for create and JSON template documentation
318  *
319  * Every type is represented by JSON object.
320  * Type name, description and other data are displayed as comments.
321  * Among other data belongs range of numeric values, size limits of arrays, possible values of selections etc.
322  *
323  *
324  * @ingroup input_types
325  */
327 public:
328  /**
329  * Constructor for output of the input type tree with root @p type.
330  * The input type tree is searched by DFS algorithm into @p depth.
331  */
332  OutputJSONTemplate(TypeBase *type, unsigned int depth = 0) : OutputBase(type, depth) {}
333 
334  /**
335  * Perform output of the documentation into given stream.
336  */
337  ostream& print(ostream& stream);
338 
339 protected:
340  // Need to implement the resolution function. Just call that in the base class.
341  void print(ostream& stream, const TypeBase *type, unsigned int depth) {
342  OutputBase::print(stream, type, depth);
343  }
344 
345 
346  void print_impl(ostream& stream, const Record *type, unsigned int depth);
347  void print_impl(ostream& stream, const Array *type, unsigned int depth);
348  void print_impl(ostream& stream, const AbstractRecord *type, unsigned int depth);
349  void print_impl(ostream& stream, const AdHocAbstractRecord *type, unsigned int depth);
350  void print_impl(ostream& stream, const Selection *type, unsigned int depth);
351  void print_impl(ostream& stream, const Integer *type, unsigned int depth);
352  void print_impl(ostream& stream, const Double *type, unsigned int depth);
353  void print_impl(ostream& stream, const Bool *type, unsigned int depth);
354  void print_impl(ostream& stream, const String *type, unsigned int depth);
355  void print_impl(ostream& stream, const FileName *type, unsigned int depth);
356 
357  //void write_description(std::ostream& stream, const string& str, unsigned int hash_count = 1);
358 
359 private:
360  /**
361  * Prints value according to DefaultType
362  * Respects obligatory, optional and read time flag
363  *
364  * @param stream Output stream
365  * @param depth Depth of output
366  * @param empty_val Default empty value (zero for numeric types, empty string ...)
367  * @param invalid_val Flag if value is invalid for its type
368  * @param has_quote Flag if value is enclosed in quotes
369  */
370  void print_default_value(ostream& stream, unsigned int depth, string empty_val, bool invalid_val, bool has_quote = false);
371 
372  /// temporary value of actually record type
373  string key_name_;
374  /// temporary value of actually reference
375  string reference_;
376  /// temporary value of actually record value
378 };
379 
380 
381 
382 
383 /**
384  * @brief Class for create documentation in Latex format using particular macros.
385  *
386  * @ingroup input_types
387  */
388 class OutputLatex : public OutputBase {
389 public:
390  OutputLatex(TypeBase *type, unsigned int depth = 0) : OutputBase(type, depth) {}
391 
392  ostream & print(ostream& stream);
393 
394 protected:
395 
396  // Need to implement the resolution function. Just call that in the base class.
397  void print(ostream& stream, const TypeBase *type, unsigned int depth) {
398  OutputBase::print( stream, type, depth);
399  }
400 
401  void print_impl(ostream& stream, const Record *type, unsigned int depth);
402  void print_impl(ostream& stream, const Array *type, unsigned int depth);
403  void print_impl(ostream& stream, const AbstractRecord *type, unsigned int depth);
404  void print_impl(ostream& stream, const AdHocAbstractRecord *type, unsigned int depth);
405  void print_impl(ostream& stream, const Selection *type, unsigned int depth);
406  void print_impl(ostream& stream, const Integer *type, unsigned int depth);
407  void print_impl(ostream& stream, const Double *type, unsigned int depth);
408  void print_impl(ostream& stream, const Bool *type, unsigned int depth);
409  void print_impl(ostream& stream, const String *type, unsigned int depth);
410  void print_impl(ostream& stream, const FileName *type, unsigned int depth);
411 
412 
413 };
414 
415 
416 /**
417  * @brief Class for create JSON machine readable documentation
418  *
419  * Every type is represented by one JSON object, for Selection e.g.:
420  * "name" : (string),
421  * "full_name" : (string),
422  * "type" : "Selection",
423  * "values" : [ { "value" : (int), "name": (string), "description" : (string) } ]
424  *
425  * @ingroup input_types
426  */
428 public:
429  OutputJSONMachine(TypeBase *type, unsigned int depth = 0) : OutputBase(type, depth) {}
430 
431 protected:
432 
433  // Need to implement the resolution function. Just call that in the base class.
434  /*void print(ostream& stream, const TypeBase *type, unsigned int depth) {
435  OutputBase::print( stream, type, depth);
436  }*/
437 
438  void print_impl(ostream& stream, const Record *type, unsigned int depth);
439  void print_impl(ostream& stream, const Array *type, unsigned int depth);
440  void print_impl(ostream& stream, const AbstractRecord *type, unsigned int depth);
441  void print_impl(ostream& stream, const AdHocAbstractRecord *type, unsigned int depth);
442  void print_impl(ostream& stream, const Selection *type, unsigned int depth);
443  void print_impl(ostream& stream, const Integer *type, unsigned int depth);
444  void print_impl(ostream& stream, const Double *type, unsigned int depth);
445  void print_impl(ostream& stream, const Bool *type, unsigned int depth);
446  void print_impl(ostream& stream, const String *type, unsigned int depth);
447  void print_impl(ostream& stream, const FileName *type, unsigned int depth);
448 
449 
450  /// Print all keys of AbstractRecord type or AdHocAbstractRecord type
451  void print_abstract_record_keys(ostream& stream, const AbstractRecord *type);
452 
453 
454 };
455 
456 
457 /**
458  * Overrides output operator for simple output of the input type tree.
459  */
460 std::ostream& operator<<(std::ostream& stream, OutputText type_output);
461 std::ostream& operator<<(std::ostream& stream, OutputJSONTemplate type_output);
462 std::ostream& operator<<(std::ostream& stream, OutputLatex type_output);
463 std::ostream& operator<<(std::ostream& stream, OutputJSONMachine type_output);
464 
465 
466 
467 } // closing namespace Type
468 } // closing namespace Input
469 
470 
471 
472 
473 #endif /* TYPE_OUTPUT_HH_ */