Flow123d  last_with_con_2.0.0-4-g42e6930
type_abstract.cc
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.cc
15  * @brief
16  */
17 
18 #include "input_type.hh"
19 #include "type_repository.hh"
20 #include "attribute_lib.hh"
21 
22 #include "system/system.hh"
23 
24 #include <boost/functional/hash.hpp>
25 
26 namespace Input {
27 namespace Type {
28 
29 using namespace std;
30 
31 
32 /************************************************
33  * implementation of Abstract
34  */
35 
37 : child_data_( std::make_shared<ChildData>( "EmptyAbstract", "" ) )
38 {
39  close();
40  finish();
41 }
42 
43 
44 
46 : TypeBase( other ), child_data_(other.child_data_)
47 {}
48 
49 
50 
51 Abstract::Abstract(const string & type_name_in, const string & description)
52 : child_data_( std::make_shared<ChildData>( type_name_in, description ) )
53 {}
54 
55 
57 {
58  TypeHash seed=0;
59  boost::hash_combine(seed, "Abstract");
60  boost::hash_combine(seed, type_name());
61  boost::hash_combine(seed, child_data_->description_);
62  for (auto &param : parameter_map_) {
63  boost::hash_combine(seed, param.first );
64  boost::hash_combine(seed, param.second );
65  }
66  boost::hash_combine(seed, this->generic_type_hash_);
67  //boost::hash_combine(seed, child_data_->generic_content_hash_);
68  //for( Record &key : child_data_->list_of_childs) {
69  // boost::hash_combine(seed, key.content_hash() );
70  //}
71  return seed;
72 }
73 
74 
75 
76 Abstract & Abstract::allow_auto_conversion(const string &type_default) {
77  ASSERT(!child_data_->closed_)(type_name()).error("Can not specify default value for TYPE key as the Abstract is closed.");
78  child_data_->selection_default_=Default("\""+type_default+"\""); // default record is closed; other constructor creates the zero item
79  return *this;
80 }
81 
82 
83 
84 const Record & Abstract::get_descendant(const string& name) const
85 {
86  ASSERT(child_data_->selection_of_childs->is_finished()).error();
87  return child_data_->list_of_childs[ child_data_->selection_of_childs->name_to_int(name) ];
88 }
89 
90 
91 
93  if ( have_default_descendant() ) {
94  int sel_val = child_data_->selection_default_.get_storage( child_data_->selection_of_childs )->get_int();
95  return &( get_descendant( child_data_->selection_of_childs->int_to_name(sel_val) ) );
96  }
97  return NULL;
98 }
99 
100 
101 
103 {
104  return * child_data_->selection_of_childs;
105 }
106 
107 
108 unsigned int Abstract::child_size() const {
109  return child_data_->list_of_childs.size();
110 }
111 
112 
113 int Abstract::add_child(const Record &subrec)
114 {
115  ASSERT(child_data_->closed_).error();
116 
117  if (std::find(child_data_->list_of_childs.begin(), child_data_->list_of_childs.end(), subrec) == child_data_->list_of_childs.end()) {
118  child_data_->selection_of_childs->add_value(child_data_->list_of_childs.size(), subrec.type_name());
119  child_data_->list_of_childs.push_back(subrec);
120  }
121 
122  return 1;
123 }
124 
125 
126 bool Abstract::finish(bool is_generic) {
127  if (child_data_->finished_) return true;
128 
129  ASSERT(child_data_->closed_)(this->type_name()).error();
130 
131  child_data_->selection_of_childs->close();
132 
133  child_data_->finished_ = true;
134 
135  for (auto &child : child_data_->list_of_childs) {
136  if (!is_generic && child.is_root_of_generic_subtree()) THROW( ExcGenericWithoutInstance() << EI_Object(child.type_name()) );
137  child.add_parent(*this);
138  child_data_->finished_ = child_data_->finished_ && child.finish(is_generic);
139  }
140 
141  // check validity of possible default value of TYPE key
142  if ( have_default_descendant() ) {
143  try {
144  child_data_->selection_default_.check_validity(child_data_->selection_of_childs);
145  } catch (ExcWrongDefault & e) {
146  e << EI_Desc("Default value for TYPE key do not match any descendant of Abstract.")
147  << EI_DefaultStr( child_data_->selection_default_.value() ) << EI_TypeName(this->type_name());
148  throw;
149  } catch (ExcWrongDefaultJSON & e) {
150  e << EI_DefaultStr( child_data_->selection_default_.value() ) << EI_TypeName(this->type_name());
151  throw;
152  }
153  }
154 
155  return (child_data_->finished_);
156 }
157 
158 
160  child_data_->closed_=true;
162 }
163 
164 
165 bool Abstract::is_finished() const {
166  return child_data_->finished_;
167 }
168 
169 
170 bool Abstract::is_closed() const {
171  return child_data_->closed_;
172 }
173 
174 
175 string Abstract::type_name() const {
176  return child_data_->type_name_;
177 }
178 
179 
181  return child_data_->selection_default_;
182 }
183 
185  // obligatory value if default is not set, see @p selection_default_
186  if ( !child_data_->selection_default_.is_obligatory() ) {
187  if ( child_data_->selection_default_.has_value_at_declaration() ) {
188  return true;
189  }
190  }
191  return false;
192 }
193 
194 
195 
197  Abstract abstract = this->deep_copy();
198  ParameterMap parameter_map;
199 
200 
201  // Set close flag - add_child method required closed child_data
202  abstract.child_data_->closed_ = true;
203  // make instances of all descendant records and add them into instance of abstract
204  for (auto &child : child_data_->list_of_childs) {
205  MakeInstanceReturnType inst = child.make_instance(vec);
206  abstract.add_child( static_cast<Record &>( *(inst.first) ) );
207  ParameterMap other_map = inst.second;
208  parameter_map.insert(other_map.begin(), other_map.end());
209  }
210  // Unset close flag - necessary for set parameters
211  abstract.child_data_->closed_ = false;
212 
213  // Set parameters and generic type as attributes
214  abstract.parameter_map_ = parameter_map;
215  abstract.generic_type_hash_ = this->content_hash();
216  TypeBase::set_generic_attributes(parameter_map);
217 
218  return std::make_pair( std::make_shared<Abstract>(abstract.close()), parameter_map );
219 }
220 
221 
223  Abstract abstract = Abstract();
224  abstract.child_data_ = std::make_shared<Abstract::ChildData>(*this->child_data_);
225  abstract.child_data_->closed_ = false;
226  abstract.child_data_->finished_ = false;
227  abstract.child_data_->list_of_childs.clear();
228  abstract.child_data_->selection_of_childs = std::make_shared<Selection>(this->type_name() + "_TYPE_selection");
229  abstract.copy_attributes(*attributes_);
230 
231  abstract.generic_type_hash_ = this->generic_type_hash_;
232  abstract.parameter_map_ = this->parameter_map_;
233  return abstract;
234 }
235 
236 
239  return *this;
240 }
241 
242 
244  this->add_attribute_(key, value);
245  return *this;
246 }
247 
248 
249 
250 /*Abstract &Abstract::set_generic_content_hash(TypeHash generic_content_hash) {
251  child_data_->generic_content_hash_ = generic_content_hash;
252  return *this;
253 }*/
254 
255 
257  return child_data_->list_of_childs.begin();
258 }
259 
261  return child_data_->list_of_childs.end();
262 }
263 
264 
265 /************************************************
266  * implementation of AdHocAbstract
267  */
268 
270 : Abstract("Derived AdHocAbstract", "This description doesn't have print out."),
271  ancestor_(ancestor)
272 {
273  //test default descendant of ancestor
274  const Record * default_desc = ancestor.get_default_descendant();
275  if (default_desc) {
276  allow_auto_conversion( default_desc->type_name() );
277  }
278 
279  this->close();
280 
281 }
282 
283 
285 {
286  Abstract::add_child(subrec);
287 
288  return *this;
289 }
290 
291 
292 bool AdHocAbstract::finish(bool is_generic)
293 {
294  if (child_data_->finished_) return true;
295 
296  const_cast<Abstract &>(ancestor_).finish(is_generic);
297 
298  //test default descendant of ancestor
299  const Record * default_desc = ancestor_.get_default_descendant();
300  if (default_desc) {
301  allow_auto_conversion( default_desc->type_name() );
302  }
303 
304  for (Abstract::ChildDataIter it = ancestor_.child_data_->list_of_childs.begin(); it != ancestor_.child_data_->list_of_childs.end(); ++it) {
305  child_data_->selection_of_childs->add_value(child_data_->list_of_childs.size(), (*it).type_name());
306  child_data_->list_of_childs.push_back(*it);
307  }
308 
309  return Abstract::finish(is_generic);
310 }
311 
312 
314  TypeHash seed=0;
315  boost::hash_combine(seed, "AdHocAbstract");
316  boost::hash_combine(seed, type_name());
317  boost::hash_combine(seed, child_data_->description_);
318  boost::hash_combine(seed, ancestor_.type_name());
319 
320  return seed;
321 }
322 
323 
324 
325 } // closing namespace Type
326 } // closing namespace Input
327 
328 
virtual bool is_finished() const override
Implements TypeBase::is_finished.
const Record & get_descendant(const string &name) const
Returns reference to the inherited Record with given name.
Base of classes for declaring structure of the input data.
Definition: type_base.hh:79
void set_generic_attributes(ParameterMap param_map)
Definition: type_base.cc:145
Abstract & allow_auto_conversion(const string &type_default)
Allows shorter input of the Abstract providing the default value to the "TYPE" key.
Abstract deep_copy() const
Create deep copy of Abstract (copy all data stored in shared pointers etc.)
bool root_of_generic_subtree_
flag is true if type should be root of generic subtree
Definition: type_base.hh:283
std::vector< Record >::const_iterator ChildDataIter
Public typedef of constant iterator into array of keys.
AdHocAbstract(const Abstract &ancestor)
Constructor.
Abstract()
Default constructor.
virtual MakeInstanceReturnType make_instance(std::vector< ParameterPair > vec=std::vector< ParameterPair >()) override
TypeHash content_hash() const override
Implements TypeBase::content_hash.
Class Input::Type::Default specifies default value of keys of a Input::Type::Record.
Definition: type_record.hh:50
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:97
string type_name() const override
Implements Type::TypeBase::type_name.
Definition: type_record.cc:299
virtual string type_name() const override
Implements Type::TypeBase::type_name.
#define ASSERT(expr)
Allow use shorter versions of macro names if these names is not used with external library...
Definition: asserts.hh:347
Class for declaration of polymorphic Record.
std::shared_ptr< attribute_map > attributes_
map of type attributes (e. g. input_type, name etc.)
Definition: type_base.hh:280
Actual data of the abstract record.
Abstract & close()
Can be used to close the Abstract for further declarations of keys.
Abstract & add_attribute(std::string key, TypeBase::json_string value)
Frontend to TypeBase::add_attribute_.
virtual bool is_closed() const override
Returns true if data_ is closed.
void add_attribute_(std::string name, json_string val)
Add attribute of given name to attribute map.
Definition: type_base.cc:106
const Abstract & ancestor_
Reference to ancestor Abstract.
static TypeRepository & get_instance()
Return singleton instance of class.
ParameterMap parameter_map_
map of parameters if type is part of generic subtree
Definition: type_base.hh:289
unsigned int child_size() const
Returns number of descendants in the child_data_.
AdHocAbstract & add_child(const Record &subrec)
Add inherited Record.
TypeHash content_hash() const override
Implements TypeBase::content_hash.
Default & get_selection_default() const
bool finish(bool is_generic=false) override
Finish declaration of the Abstract type.
bool finish(bool is_generic=false) override
Finish declaration of the AdHocAbstract type.
const Record * get_default_descendant() const
Returns default descendant.
Class for declaration of polymorphic Record.
ChildDataIter begin_child_data() const
Container-like access to the descendants of the Abstract.
const Selection & get_type_selection() const
Returns reference to Selection type of the implicit key TYPE.
int add_child(const Record &subrec)
Add inherited Record.
Abstract & root_of_generic_subtree()
bool have_default_descendant() const
Check if type has set value of default descendants.
TypeHash generic_type_hash_
hash string of generic type if type is derived, or empty string
Definition: type_base.hh:286
ChildDataIter end_child_data() const
Container-like access to the descendants of the Abstract.
std::string json_string
String stored in JSON format.
Definition: type_base.hh:85
Record type proxy class.
Definition: type_record.hh:171
std::shared_ptr< ChildData > child_data_
Actual data of the Abstract.
boost::shared_ptr< T > add_type(const T &type)
Add type to TypeRepository if doesn&#39;t exist there or get existing type with same TypeHash.
std::size_t TypeHash
Type returned by content_hash methods.
Definition: type_base.hh:82
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.
Definition: exceptions.hh:45
Template for classes storing finite set of named values.