Flow123d  JS_before_hm-1598-g3b021b4
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 
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  ASSERT_GT(data_->keys.size(), 0)(this->type_name()).error("Empty Tuple!\n");
72  data_->closed_=true;
73 
74  bool allow_auto_conversion = true; // if no key or only first key is obligatory, tuple is auto convertible
75  for (vector<Key>::iterator it=data_->keys.begin(); it!=data_->keys.end(); it++)
76  if ( it->default_.is_obligatory() ) {
77  if (it != data_->keys.begin()) {
78  allow_auto_conversion = false;
79  }
80  }
81  if (allow_auto_conversion) { // Add autoconvertibility
82  data_->auto_conversion_key_idx = 0;
83  data_->auto_conversion_key=data_->keys.begin()->key_;
84  }
85 
87 }
88 
89 
91 {
92  ASSERT(finish_type != FinishStatus::none_).error();
93  ASSERT(finish_type != FinishStatus::in_perform_).error();
94  ASSERT(data_->finish_status_ != FinishStatus::in_perform_)(this->type_name()).error("Recursion in the IST element of type Tuple.");
95 
96  if (this->is_finished()) return data_->finish_status_;
97 
98  ASSERT(data_->closed_)(this->type_name()).error();
99 
100  data_->finish_status_ = FinishStatus::in_perform_;
101 
102  // iterates through keys
103  bool obligatory_keys = true; // check order of keys (at first obligatory keys are defined, then other keys)
104  for (vector<Key>::iterator it=data_->keys.begin(); it!=data_->keys.end(); it++) {
105  // Performs check order of keys and check auto-conversion
106  Default dflt = it->default_;
107  if ( dflt.is_obligatory() ) {
108  if ( !obligatory_keys ) {
109  THROW( ExcTupleWrongKeysOrder() << EI_TupleName(this->type_name()) );
110  }
111  } else {
112  obligatory_keys = false;
113  }
114 
115  // Performs finish of keys
116  if (typeid( *(it->type_.get()) ) == typeid(Instance)) {
117  it->type_->finish(FinishStatus::generic_); // finish Instance object
118  it->type_ = it->type_->make_instance().first;
119  }
120  if ((finish_type != FinishStatus::generic_) && it->type_->is_root_of_generic_subtree())
121  THROW( ExcGenericWithoutInstance() << EI_Object(it->type_->type_name()) );
122  it->type_->finish(finish_type);
123  ASSERT(it->type_->is_finished()).error();
124  if (finish_type == FinishStatus::delete_) it->type_.reset();
125  }
126 
127  data_->finish_status_ = finish_type;
128  return (data_->finish_status_);
129 }
130 
131 
133 {
134  ASSERT(false)(this->type_name()).error("Call of derive_from method is forbidden for type Tuple");
135  return *this;
136 }
137 
138 
139 unsigned int Tuple::obligatory_keys_count() const {
140  unsigned int obligatory_keys_count=0;
141  for ( KeyIter it= this->begin(); it != this->end(); ++it) {
142  if ( it->default_.is_obligatory() ) ++obligatory_keys_count;
143  }
144  return obligatory_keys_count;
145 }
146 
147 
149  Tuple instance_tuple = this->deep_copy();
150  auto parameter_map = this->set_instance_data(instance_tuple, vec);
151  return std::make_pair( std::make_shared<Tuple>(instance_tuple.close()), parameter_map );
152 }
153 
154 
156  Tuple tuple = Tuple();
157  tuple.data_ = std::make_shared<Record::RecordData>(*this->data_);
158  tuple.data_->closed_ = false;
159  tuple.data_->finish_status_ = FinishStatus::none_;
161  tuple.parameter_map_ = this->parameter_map_;
163  return tuple;
164 }
165 
166 
169  return *this;
170 }
171 
172 
173 Tuple &Tuple::declare_key(const string &key, std::shared_ptr<TypeBase> type,
174  const Default &default_value, const string &description,
175  TypeBase::attribute_map key_attributes)
176 {
177  Record::declare_key(key, type, default_value, description, key_attributes);
178  return *this;
179 }
180 
181 
182 template <class KeyType>
183 Tuple &Tuple::declare_key(const string &key, const KeyType &type,
184  const Default &default_value, const string &description,
185  TypeBase::attribute_map key_attributes)
186 {
187  Record::declare_key(key, type, default_value, description, key_attributes);
188  return *this;
189 }
190 
191 
192 
193 template <class KeyType>
194 Tuple &Tuple::declare_key(const string &key, const KeyType &type,
195  const string &description,
196  TypeBase::attribute_map key_attributes)
197 {
198  Record::declare_key(key, type, description, key_attributes);
199  return *this;
200 }
201 
202 
203 
204 // explicit instantiation of template methods
205 
206 #define TUPLE_DECLARE_KEY(TYPE) \
207 template Tuple & Tuple::declare_key<TYPE>(const string &key, const TYPE &type, const Default &default_value, const string &description, TypeBase::attribute_map key_attributes); \
208 template Tuple & Tuple::declare_key<TYPE>(const string &key, const TYPE &type, const string &description, TypeBase::attribute_map key_attributes)
209 
210 
211 TUPLE_DECLARE_KEY(String);
212 TUPLE_DECLARE_KEY(Integer);
213 TUPLE_DECLARE_KEY(Double);
214 TUPLE_DECLARE_KEY(Bool);
215 TUPLE_DECLARE_KEY(FileName);
216 TUPLE_DECLARE_KEY(Selection);
220 TUPLE_DECLARE_KEY(AdHocAbstract);
221 TUPLE_DECLARE_KEY(Parameter);
222 TUPLE_DECLARE_KEY(Instance);
224 
225 
226 } // closing namespace Type
227 } // 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:92
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:182
std::vector< struct Key >::const_iterator KeyIter
Public typedef of constant iterator into array of keys.
Definition: type_record.hh:216
ArmaVec< double, N > vec
Definition: armor.hh:885
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:173
bool root_of_generic_subtree_
flag is true if type should be root of generic subtree
Definition: type_base.hh:301
string type_name() const override
Implements Type::TypeBase::type_name.
Definition: type_record.cc:317
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:111
Tuple deep_copy() const
Overrides Record::deep_copy.
Definition: type_tuple.cc:155
Abstract linear system class.
Definition: balance.hh:40
#define ASSERT_GT(a, b)
Definition of comparative assert macro (Greater Than)
Definition: asserts.hh:312
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:90
#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:298
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:579
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:307
Record()
Default constructor. Empty handle.
Definition: type_record.cc:99
Tuple & derive_from(Abstract &parent) override
Override Record::derive_from.
Definition: type_tuple.cc:132
MakeInstanceReturnType make_instance(std::vector< ParameterPair > vec=std::vector< ParameterPair >()) override
Implements TypeBase::make_instance.
Definition: type_tuple.cc:148
ParameterMap set_instance_data(Record &rec, std::vector< ParameterPair > vec)
Declares a TYPE key of the Record.
Definition: type_record.cc:367
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:503
Tuple & root_of_generic_subtree() override
Overrides Record::root_of_generic_subtree.
Definition: type_tuple.cc:167
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:304
friend class Array
Definition: type_base.hh:309
KeyIter end() const
Container-like access to the keys of the Record.
Definition: type_record.hh:587
unsigned int obligatory_keys_count() const
Return count of obligatory keys.
Definition: type_tuple.cc:139
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:531
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
std::size_t TypeHash
Type returned by content_hash methods.
Definition: type_base.hh:95
#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:231