Flow123d  last_with_con_2.0.0-663-gd0e2296
type_tuple.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_tuple.cc
15  * @brief
16  */
17 
18 #include "input_type.hh"
19 #include "type_repository.hh"
20 
21 #include <boost/functional/hash.hpp>
22 
23 
24 namespace Input {
25 namespace Type {
26 
27 
28 
30 : Record() {}
31 
32 
33 Tuple::Tuple(const Tuple & other)
34 : Record(other) {}
35 
36 
37 Tuple::Tuple(const string & type_name_in, const string & description)
38 {
39  this->data_ = std::make_shared<RecordData>(type_name_in, description);
40 }
41 
42 
44 {
45  TypeHash seed=0;
46  boost::hash_combine(seed, "Tuple");
47  data_->content_hash(seed);
48  return seed;
49 }
50 
51 
52 Tuple & Tuple::allow_auto_conversion(const string &from_key)
53 {
54  ASSERT(false)(this->type_name()).error("Call of allow_auto_conversion method is forbidden for type Tuple");
55  return *this;
56 }
57 
58 
59 bool Tuple::operator==(const TypeBase &other) const
60 { return typeid(*this) == typeid(other) &&
61  (type_name() == static_cast<const Tuple *>(&other)->type_name() );
62 }
63 
64 
65 const Tuple &Tuple::close() const {
66  data_->closed_=true;
68 }
69 
70 
72 {
73  ASSERT(finish_type != FinishStatus::none_).error();
74  ASSERT(finish_type != FinishStatus::in_perform_).error();
75  ASSERT(data_->finish_status_ != FinishStatus::in_perform_)(this->type_name()).error("Recursion in the IST element of type Tuple.");
76 
77  if (this->is_finished()) return data_->finish_status_;
78 
79  ASSERT(data_->closed_)(this->type_name()).error();
80 
81  data_->finish_status_ = FinishStatus::in_perform_;
82 
83  // iterates through keys
84  bool obligatory_keys = true; // check order of keys (at first obligatory keys are defined, then other keys)
85  bool allow_auto_conversion = true;
86  for (vector<Key>::iterator it=data_->keys.begin(); it!=data_->keys.end(); it++) {
87  // Performs check order of keys and check auto-conversion
88  Default dflt = it->default_;
89  if ( dflt.is_obligatory() ) {
90  if (it != data_->keys.begin()) {
91  allow_auto_conversion = false;
92  }
93  if ( !obligatory_keys ) {
94  THROW( ExcTupleWrongKeysOrder() << EI_TupleName(this->type_name()) );
95  }
96  } else {
97  obligatory_keys = false;
98  }
99 
100  // Performs finish of keys
101  if (typeid( *(it->type_.get()) ) == typeid(Instance)) {
102  it->type_->finish(FinishStatus::generic_); // finish Instance object
103  it->type_ = it->type_->make_instance().first;
104  }
105  if ((finish_type != FinishStatus::generic_) && it->type_->is_root_of_generic_subtree())
106  THROW( ExcGenericWithoutInstance() << EI_Object(it->type_->type_name()) );
107  it->type_->finish(finish_type);
108  ASSERT(it->type_->is_finished()).error();
109  if (finish_type == FinishStatus::delete_) it->type_.reset();
110  }
111 
112  // Add autoconvertibility
113  if (allow_auto_conversion) {
114  data_->auto_conversion_key_idx = 0;
115  data_->auto_conversion_key=data_->keys.begin()->key_;
116  }
117 
118  data_->finish_status_ = finish_type;
119  return (data_->finish_status_);
120 }
121 
122 
124 {
125  ASSERT(false)(this->type_name()).error("Call of derive_from method is forbidden for type Tuple");
126  return *this;
127 }
128 
129 
130 unsigned int Tuple::obligatory_keys_count() const {
131  unsigned int obligatory_keys_count=0;
132  for ( KeyIter it= this->begin(); it != this->end(); ++it) {
133  if ( it->default_.is_obligatory() ) ++obligatory_keys_count;
134  }
135  return obligatory_keys_count;
136 }
137 
138 
140  Tuple tuple = this->deep_copy();
141  ParameterMap parameter_map;
142  this->set_instance_data(tuple, parameter_map, vec);
143 
144  return std::make_pair( std::make_shared<Tuple>(tuple.close()), parameter_map );
145 }
146 
147 
149  Tuple tuple = Tuple();
150  tuple.data_ = std::make_shared<Record::RecordData>(*this->data_);
151  tuple.data_->closed_ = false;
152  tuple.data_->finish_status_ = FinishStatus::none_;
154  tuple.parameter_map_ = this->parameter_map_;
156  return tuple;
157 }
158 
159 
162  return *this;
163 }
164 
165 
166 Tuple &Tuple::declare_key(const string &key, std::shared_ptr<TypeBase> type,
167  const Default &default_value, const string &description,
168  TypeBase::attribute_map key_attributes)
169 {
170  Record::declare_key(key, type, default_value, description, key_attributes);
171  return *this;
172 }
173 
174 
175 template <class KeyType>
176 Tuple &Tuple::declare_key(const string &key, const KeyType &type,
177  const Default &default_value, const string &description,
178  TypeBase::attribute_map key_attributes)
179 {
180  Record::declare_key(key, type, default_value, description, key_attributes);
181  return *this;
182 }
183 
184 
185 
186 template <class KeyType>
187 Tuple &Tuple::declare_key(const string &key, const KeyType &type,
188  const string &description,
189  TypeBase::attribute_map key_attributes)
190 {
191  Record::declare_key(key, type, description, key_attributes);
192  return *this;
193 }
194 
195 
196 
197 // explicit instantiation of template methods
198 
199 #define TUPLE_DECLARE_KEY(TYPE) \
200 template Tuple & Tuple::declare_key<TYPE>(const string &key, const TYPE &type, const Default &default_value, const string &description, TypeBase::attribute_map key_attributes); \
201 template Tuple & Tuple::declare_key<TYPE>(const string &key, const TYPE &type, const string &description, TypeBase::attribute_map key_attributes)
202 
203 
204 TUPLE_DECLARE_KEY(String);
205 TUPLE_DECLARE_KEY(Integer);
206 TUPLE_DECLARE_KEY(Double);
207 TUPLE_DECLARE_KEY(Bool);
208 TUPLE_DECLARE_KEY(FileName);
209 TUPLE_DECLARE_KEY(Selection);
214 TUPLE_DECLARE_KEY(Parameter);
215 TUPLE_DECLARE_KEY(Instance);
217 
218 
219 } // closing namespace Type
220 } // closing namespace Input
std::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.
TUPLE_DECLARE_KEY(String)
Base of classes for declaring structure of the input data.
Definition: type_base.hh:93
bool operator==(const TypeBase &other) const override
Class comparison and Tuple type name comparison.
Definition: type_tuple.cc:59
void copy_attributes(attribute_map other_attributes)
Definition: type_base.cc:158
std::vector< struct Key >::const_iterator KeyIter
Public typedef of constant iterator into array of keys.
Definition: type_record.hh:206
Tuple & declare_key(const string &key, std::shared_ptr< TypeBase > type, const Default &default_value, const string &description, TypeBase::attribute_map key_attributes=TypeBase::attribute_map())
Overrides Record::declare_key(const string &, std::shared_ptr<TypeBase>, const Default &...
Definition: type_tuple.cc:166
bool root_of_generic_subtree_
flag is true if type should be root of generic subtree
Definition: type_base.hh:310
string type_name() const override
Implements Type::TypeBase::type_name.
Definition: type_record.cc:315
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:111
Tuple deep_copy() const
Overrides Record::deep_copy.
Definition: type_tuple.cc:148
const Tuple & close() const
Close the Tuple for further declarations of keys.
Definition: type_tuple.cc:65
Helper class that stores data of generic types.
Definition: type_generic.hh:88
TypeHash content_hash() const override
Definition: type_tuple.cc:43
FinishStatus finish(FinishStatus finish_type=FinishStatus::regular_) override
Finish declaration of the Tuple type.
Definition: type_tuple.cc:71
#define ASSERT(expr)
Allow use shorter versions of macro names if these names is not used with external library...
Definition: asserts.hh:347
std::shared_ptr< attribute_map > attributes_
map of type attributes (e. g. input_type, name etc.)
Definition: type_base.hh:307
Tuple & allow_auto_conversion(const string &from_key) override
Override Record::allow_auto_conversion.
Definition: type_tuple.cc:52
KeyIter begin() const
Container-like access to the keys of the Record.
Definition: type_record.hh:565
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:316
Record()
Default constructor. Empty handle.
Definition: type_record.cc:99
Tuple & derive_from(Abstract &parent) override
Override Record::derive_from.
Definition: type_tuple.cc:123
MakeInstanceReturnType make_instance(std::vector< ParameterPair > vec=std::vector< ParameterPair >()) override
Implements TypeBase::make_instance.
Definition: type_tuple.cc:139
Record & declare_key(const string &key, std::shared_ptr< TypeBase > type, const Default &default_value, const string &description, TypeBase::attribute_map key_attributes=TypeBase::attribute_map())
Declares a new key of the Record.
Definition: type_record.cc:484
Tuple & root_of_generic_subtree() override
Overrides Record::root_of_generic_subtree.
Definition: type_tuple.cc:160
friend class Abstract
Definition: type_record.hh:173
Class for declaration of polymorphic Record.
Tuple type proxy class.
Definition: type_tuple.hh:41
TypeHash generic_type_hash_
hash string of generic type if type is derived, or empty string
Definition: type_base.hh:313
friend class AdHocAbstract
Definition: type_record.hh:174
friend class Array
Definition: type_base.hh:318
KeyIter end() const
Container-like access to the keys of the Record.
Definition: type_record.hh:573
unsigned int obligatory_keys_count() const
Return count of obligatory keys.
Definition: type_tuple.cc:130
Record type proxy class.
Definition: type_record.hh:171
Tuple()
Default constructor. Empty handle.
Definition: type_tuple.cc:29
std::shared_ptr< RecordData > data_
Data handle.
Definition: type_record.hh:517
bool is_obligatory() const
Returns true if the key is obligatory and thus must be specified on input. No default value is given...
Definition: type_record.hh:126
void set_instance_data(Record &rec, ParameterMap &parameter_map, std::vector< ParameterPair > vec)
Declares a TYPE key of the Record.
Definition: type_record.cc:361
std::size_t TypeHash
Type returned by content_hash methods.
Definition: type_base.hh:96
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.
Definition: exceptions.hh:45
bool is_finished() const override
Implements TypeBase::is_finished.
Definition: type_record.cc:230