Flow123d  release_3.0.0-506-g34af125
type_abstract.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_abstract.hh
15  * @brief
16  */
17 
18 #ifndef TYPE_ABSTRACT_HH_
19 #define TYPE_ABSTRACT_HH_
20 
21 #include "type_base.hh"
22 #include "type_record.hh"
23 
24 #include <string>
25 #include <vector>
26 #include <memory>
27 
28 
29 namespace Input {
30 namespace Type {
31 
32 
33 using namespace std;
34 
35 
36 class Selection;
37 //class AdHocAbstract;
38 //class OutputBase;
39 
40 
41 /**
42  * @brief Class for declaration of polymorphic Record.
43  *
44  * Like the Record class this is only proxy class. It allows add Record as descendants, but
45  * further there is method \p no_more_descendants to close adding them. After this
46  * you can not derive any Record from this Abstract.
47  *
48  *
49  *
50  * A static method (typically part of an abstract class) for construction of an AbstractType can look like:
51  *
52  @code
53  static Input::Type::Abstract &SomeAbstractClass::get_input_type() {
54  using namespace Input::Type;
55  return Abstract("Function", "Input for generic time-space function.")
56  .close();
57  }
58  @endcode
59  *
60  * @ingroup input_types
61  */
62 class Abstract : public TypeBase {
63  friend class OutputBase;
64  friend class AdHocAbstract;
65 
66 protected:
67 
68  /**
69  * @brief Actual data of the abstract record.
70  */
71  class ChildData {
72  public:
73  /// Constructor
74  ChildData(const string &name, const string &description);
75 
76  /**
77  * @brief Selection composed from names of derived Records.
78  *
79  * Indices are according to the order of derivation (starting from zero).
80  */
81  std::shared_ptr< Selection> selection_of_childs;
82 
83  /// Vector of derived Records (proxies) in order of derivation.
85 
86  /// Description of the whole Abstract type.
87  const string description_;
88 
89  /// type_name of the whole Abstract type.
90  const string type_name_;
91 
92  /// Abstract is finished when it has added all descendant records.
94 
95  /// If Abstract is closed, we do not allow any further declaration calls.
96  bool closed_;
97 
98  /**
99  * @brief Default value of selection_of_childs (used for automatic conversion).
100  *
101  * If default value isn't set, selection_default_ is set to obligatory.
102  */
104 
105  /**
106  * Allow store hash of part of generic subtree.
107  *
108  * This hash can be typically used if descendants of Abstract contains different
109  * structure of parameter location.
110  * For example we have have Records with key represents generic part of subtree:
111  * - in first descendant this key is of the type Parameter
112  * - in second descendant this key is of the type Array of Parameter
113  * - in third descendant this key is of the type Array of Parameter with fixed size
114  * etc.
115  */
116  //TypeHash generic_content_hash_;
117 
118  };
119 
120 public:
121  /// Public typedef of constant iterator into array of keys.
123 
124  /// Default constructor.
125  Abstract();
126 
127  /**
128  * @brief Copy constructor.
129  *
130  * We check that other is non empty.
131  */
132  Abstract(const Abstract& other);
133 
134 
135  /**
136  * @brief Basic constructor.
137  *
138  * You has to provide \p type_name of the new declared Abstract type and its \p description.
139  */
140  Abstract(const string & type_name_in, const string & description);
141 
142  /**
143  * @brief Implements @p TypeBase::content_hash.
144  *
145  * Hash is calculated by type name, description and hash of attributes.
146  */
147  TypeHash content_hash() const override;
148 
149  /**
150  * @brief Allows shorter input of the Abstract providing the default value to the "TYPE" key.
151  *
152  * If the input reader come across the Abstract in the declaration tree and the input
153  * is not 'record-like' with specified value for TYPE, it tries to use the descendant Record specified by
154  * @p type_default parameter of this method. Further auto conversion of such Record may be possible.
155  */
156  Abstract &allow_auto_conversion(const string &type_default);
157 
158  /// Close the Abstract and add its to type repository (see @p TypeRepository::add_type).
159  Abstract &close();
160 
161  /**
162  * Mark the type to be root of a generic subtree.
163  * Such type can not appear in IST directly but only as the internal type
164  * of the Instance auxiliary object.
165  */
166  Abstract &root_of_generic_subtree();
167 
168  /// Frontend to TypeBase::add_attribute_
169  Abstract &add_attribute(std::string key, TypeBase::json_string value);
170 
171  /**
172  * @brief Finish declaration of the Abstract type.
173  *
174  * Checks if Abstract is closed and completes Abstract (check default descendant, parameters of generic types etc).
175  */
176  FinishStatus finish(FinishStatus finish_type = FinishStatus::regular_) override;
177 
178  /// Returns reference to the inherited Record with given name.
179  const Record &get_descendant(const string& name) const;
180 
181  /**
182  * @brief Returns default descendant.
183  *
184  * Returns only if TYPE key has default value, otherwise returns empty Record.
185  */
186  const Record * get_default_descendant() const;
187 
188  /// Returns reference to Selection type of the implicit key TYPE.
189  const Selection &get_type_selection() const;
190 
191  /// Returns number of descendants in the child_data_.
192  unsigned int child_size() const;
193 
194  /// Implements @p TypeBase::finish_status.
195  virtual FinishStatus finish_status() const override;
196 
197  /// Implements @p TypeBase::is_finished.
198  virtual bool is_finished() const override;
199 
200  /// Returns true if @p data_ is closed.
201  virtual bool is_closed() const override;
202 
203  /**
204  * @brief Implements @p Type::TypeBase::type_name.
205  *
206  * Name corresponds to @p data_->type_name_.
207  */
208  virtual string type_name() const override;
209  /// Override @p Type::TypeBase::class_name.
210  string class_name() const override;
211 
212  /**
213  * @brief Container-like access to the descendants of the Abstract.
214  *
215  * Returns iterator to the first data.
216  */
217  ChildDataIter begin_child_data() const;
218 
219  /**
220  * @brief Container-like access to the descendants of the Abstract.
221  *
222  * Returns iterator to the last data.
223  */
224  ChildDataIter end_child_data() const;
225 
226  /**
227  * @brief Add inherited Record.
228  *
229  * Do not use this method for set Record as descendant! For this case should be used \p Record::derive_from
230  * method in generating function of Record.
231  *
232  * This method is used primarily for registration of Record during its closing.
233  */
234  int add_child(Record &subrec);
235 
236  // Get default value of selection_of_childs
237  Default &get_selection_default() const;
238 
239  // Implements @p TypeBase::make_instance.
240  virtual MakeInstanceReturnType make_instance(std::vector<ParameterPair> vec = std::vector<ParameterPair>()) override;
241 
242 
243 
244 
245 protected:
246  /// Create deep copy of Abstract (copy all data stored in shared pointers etc.)
247  Abstract deep_copy() const;
248 
249  /// Check if type has set value of default descendants.
250  bool have_default_descendant() const;
251 
252  /// Actual data of the Abstract.
253  std::shared_ptr<ChildData> child_data_;
254 
255  friend class Record;
256 };
257 
258 
259 /** ******************************************************************************************************************************
260  * @brief Class for declaration of polymorphic Record.
261  *
262  * Abstract extends on list of descendants provided immediately
263  * after construction by add_child(). These descendants derive
264  * only keys from common AR. AdHocAR has separate instance for every
265  * key of this type.
266  *
267  * @ingroup input_types
268  */
269 class AdHocAbstract : public Abstract {
270  friend class OutputBase;
271 public:
272  /// Constructor
273  AdHocAbstract(const Abstract &ancestor);
274 
275  /**
276  * @brief Implements @p TypeBase::content_hash.
277  *
278  * Hash is calculated by type name, description and name of ancestor.
279  */
280  TypeHash content_hash() const override;
281 
282  /// Override @p Type::TypeBase::class_name.
283  string class_name() const override;
284 
285 
286  /**
287  * @brief Finish declaration of the AdHocAbstract type.
288  *
289  * Adds descendants of ancestor Abstract, calls close() and complete keys with non-null
290  * pointers to lazy types.
291  */
292  FinishStatus finish(FinishStatus finish_type = FinishStatus::regular_) override;
293 
294  /// Add inherited Record.
295  AdHocAbstract &add_child(Record &subrec);
296 
297 protected:
298  /// Reference to ancestor Abstract
300 };
301 
302 
303 
304 } // closing namespace Type
305 } // closing namespace Input
306 
307 
308 
309 
310 #endif /* TYPE_ABSTRACT_HH_ */
const string description_
Description of the whole Abstract type.
Base of classes for declaring structure of the input data.
Definition: type_base.hh:99
std::vector< Record >::const_iterator ChildDataIter
Public typedef of constant iterator into array of keys.
Class Input::Type::Default specifies default value of keys of a Input::Type::Record.
Definition: type_record.hh:61
std::pair< std::shared_ptr< TypeBase >, ParameterMap > MakeInstanceReturnType
Return type of make_instance methods, contains instance of generic type and map of used parameters...
Definition: type_base.hh:118
Abstract linear system class.
Definition: balance.hh:35
Base abstract class for output description of the Input::Type tree.
Definition: type_output.hh:65
Class for declaration of polymorphic Record.
Actual data of the abstract record.
const Abstract & ancestor_
Reference to ancestor Abstract.
bool closed_
If Abstract is closed, we do not allow any further declaration calls.
static constexpr bool value
Definition: json.hpp:87
vector< Record > list_of_childs
Vector of derived Records (proxies) in order of derivation.
Class for declaration of polymorphic Record.
FinishStatus finish_status_
Abstract is finished when it has added all descendant records.
Default selection_default_
Default value of selection_of_childs (used for automatic conversion).
const string type_name_
type_name of the whole Abstract type.
std::string json_string
String stored in JSON format.
Definition: type_base.hh:106
Record type proxy class.
Definition: type_record.hh:182
std::shared_ptr< ChildData > child_data_
Actual data of the Abstract.
std::shared_ptr< Selection > selection_of_childs
Selection composed from names of derived Records.
std::size_t TypeHash
Type returned by content_hash methods.
Definition: type_base.hh:102
Template for classes storing finite set of named values.