Flow123d  release_1.8.2-1603-g0109a2b
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  FEAL_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  FEAL_DEBUG_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  FEAL_DEBUG_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  FEAL_DEBUG_ASSERT(child_data_->closed_).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  xprintf(PrgErr, "Default value '%s' for TYPE key do not match any descendant of Abstract '%s'.\n", child_data_->selection_default_.value().c_str(), type_name().c_str());
147  }
148  }
149 
150  return (child_data_->finished_);
151 }
152 
153 
155  child_data_->closed_=true;
157 }
158 
159 
160 bool Abstract::is_finished() const {
161  return child_data_->finished_;
162 }
163 
164 
165 bool Abstract::is_closed() const {
166  return child_data_->closed_;
167 }
168 
169 
170 string Abstract::type_name() const {
171  return child_data_->type_name_;
172 }
173 
174 
176  return child_data_->selection_default_;
177 }
178 
180  // obligatory value if default is not set, see @p selection_default_
181  if ( !child_data_->selection_default_.is_obligatory() ) {
182  if ( child_data_->selection_default_.has_value_at_declaration() ) {
183  return true;
184  }
185  }
186  return false;
187 }
188 
189 
190 
192  Abstract abstract = this->deep_copy();
193  ParameterMap parameter_map;
194 
195 
196  // Set close flag - add_child method required closed child_data
197  abstract.child_data_->closed_ = true;
198  // make instances of all descendant records and add them into instance of abstract
199  for (auto &child : child_data_->list_of_childs) {
200  MakeInstanceReturnType inst = child.make_instance(vec);
201  abstract.add_child( static_cast<Record &>( *(inst.first) ) );
202  ParameterMap other_map = inst.second;
203  parameter_map.insert(other_map.begin(), other_map.end());
204  }
205  // Unset close flag - necessary for set parameters
206  abstract.child_data_->closed_ = false;
207 
208  // Set parameters and generic type as attributes
209  abstract.parameter_map_ = parameter_map;
210  abstract.generic_type_hash_ = this->content_hash();
211  TypeBase::set_generic_attributes(parameter_map);
212 
213  return std::make_pair( std::make_shared<Abstract>(abstract.close()), parameter_map );
214 }
215 
216 
218  Abstract abstract = Abstract();
219  abstract.child_data_ = std::make_shared<Abstract::ChildData>(*this->child_data_);
220  abstract.child_data_->closed_ = false;
221  abstract.child_data_->finished_ = false;
222  abstract.child_data_->list_of_childs.clear();
223  abstract.child_data_->selection_of_childs = std::make_shared<Selection>(this->type_name() + "_TYPE_selection");
224  abstract.copy_attributes(*attributes_);
225 
226  abstract.generic_type_hash_ = this->generic_type_hash_;
227  abstract.parameter_map_ = this->parameter_map_;
228  return abstract;
229 }
230 
231 
234  return *this;
235 }
236 
237 
239  this->add_attribute_(key, value);
240  return *this;
241 }
242 
243 
244 
245 /*Abstract &Abstract::set_generic_content_hash(TypeHash generic_content_hash) {
246  child_data_->generic_content_hash_ = generic_content_hash;
247  return *this;
248 }*/
249 
250 
252  return child_data_->list_of_childs.begin();
253 }
254 
256  return child_data_->list_of_childs.end();
257 }
258 
259 
260 /************************************************
261  * implementation of AdHocAbstract
262  */
263 
265 : Abstract("Derived AdHocAbstract", "This description doesn't have print out."),
266  ancestor_(ancestor)
267 {
268  //test default descendant of ancestor
269  const Record * default_desc = ancestor.get_default_descendant();
270  if (default_desc) {
271  allow_auto_conversion( default_desc->type_name() );
272  }
273 
274  this->close();
275 
276 }
277 
278 
280 {
281  Abstract::add_child(subrec);
282 
283  return *this;
284 }
285 
286 
287 bool AdHocAbstract::finish(bool is_generic)
288 {
289  if (child_data_->finished_) return true;
290 
291  const_cast<Abstract &>(ancestor_).finish(is_generic);
292 
293  //test default descendant of ancestor
294  const Record * default_desc = ancestor_.get_default_descendant();
295  if (default_desc) {
296  allow_auto_conversion( default_desc->type_name() );
297  }
298 
299  for (Abstract::ChildDataIter it = ancestor_.child_data_->list_of_childs.begin(); it != ancestor_.child_data_->list_of_childs.end(); ++it) {
300  child_data_->selection_of_childs->add_value(child_data_->list_of_childs.size(), (*it).type_name());
301  child_data_->list_of_childs.push_back(*it);
302  }
303 
304  return Abstract::finish(is_generic);
305 }
306 
307 
309  TypeHash seed=0;
310  boost::hash_combine(seed, "AdHocAbstract");
311  boost::hash_combine(seed, type_name());
312  boost::hash_combine(seed, child_data_->description_);
313  boost::hash_combine(seed, ancestor_.type_name());
314 
315  return seed;
316 }
317 
318 
319 
320 } // closing namespace Type
321 } // closing namespace Input
322 
323 
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.
#define FEAL_DEBUG_ASSERT(expr)
Definition of assert for debug mode only.
Definition: global_defs.h:186
Base of classes for declaring structure of the input data.
Definition: type_base.hh:74
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:278
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:92
string type_name() const override
Implements Type::TypeBase::type_name.
Definition: type_record.cc:284
#define FEAL_ASSERT(expr)
Definition of assert for debug and release mode.
Definition: global_defs.h:176
virtual string type_name() const override
Implements Type::TypeBase::type_name.
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:275
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:284
unsigned int child_size() const
Returns number of descendants in the child_data_.
AdHocAbstract & add_child(const Record &subrec)
Add inherited Record.
#define xprintf(...)
Definition: system.hh:87
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:281
Definition: system.hh:59
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:80
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:77
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.
Definition: exceptions.hh:44
Template for classes storing finite set of named values.