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