Flow123d  3.9.1-787489b
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_PERMANENT(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_PERMANENT(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);
217 TUPLE_DECLARE_KEY(Array);
218 TUPLE_DECLARE_KEY(Record);
219 TUPLE_DECLARE_KEY(Abstract);
220 TUPLE_DECLARE_KEY(AdHocAbstract);
221 TUPLE_DECLARE_KEY(Parameter);
222 TUPLE_DECLARE_KEY(Instance);
223 TUPLE_DECLARE_KEY(Tuple);
224 
225 
226 } // closing namespace Type
227 } // closing namespace Input
Input::Type::Tuple::derive_from
Tuple & derive_from(Abstract &parent) override
Override Record::derive_from.
Definition: type_tuple.cc:132
Input::Type::Default::is_obligatory
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
Input::Type::in_perform_
@ in_perform_
Definition: type_base.hh:76
ASSERT_GT
#define ASSERT_GT(a, b)
Definition of comparative assert macro (Greater Than) only for debug mode.
Definition: asserts.hh:317
Armor::vec
ArmaVec< double, N > vec
Definition: armor.hh:885
Input::Type::FinishStatus
FinishStatus
Definition: type_base.hh:74
Input::Type::Tuple::make_instance
MakeInstanceReturnType make_instance(std::vector< ParameterPair > vec=std::vector< ParameterPair >()) override
Implements TypeBase::make_instance.
Definition: type_tuple.cc:148
Input::Type::Record::type_name
string type_name() const override
Implements Type::TypeBase::type_name.
Definition: type_record.cc:317
Input::Type::Record::KeyIter
std::vector< struct Key >::const_iterator KeyIter
Public typedef of constant iterator into array of keys.
Definition: type_record.hh:216
Input::Type::TypeBase::copy_attributes
void copy_attributes(attribute_map other_attributes)
Definition: type_base.cc:182
Input
Abstract linear system class.
Definition: balance.hh:40
ASSERT
#define ASSERT(expr)
Definition: asserts.hh:351
Input::Type::Tuple::root_of_generic_subtree
Tuple & root_of_generic_subtree() override
Overrides Record::root_of_generic_subtree.
Definition: type_tuple.cc:167
Input::Type::TypeBase::MakeInstanceReturnType
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
Input::TypeRepository::get_instance
static TypeRepository & get_instance()
Return singleton instance of class.
Definition: type_repository.hh:59
Input::Type::Tuple::deep_copy
Tuple deep_copy() const
Overrides Record::deep_copy.
Definition: type_tuple.cc:155
Input::Type::none_
@ none_
Definition: type_base.hh:75
Input::Type::delete_
@ delete_
Definition: type_base.hh:79
Input::Type::TypeBase::root_of_generic_subtree_
bool root_of_generic_subtree_
flag is true if type should be root of generic subtree
Definition: type_base.hh:301
THROW
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.
Definition: exceptions.hh:53
std::vector
Definition: doxy_dummy_defs.hh:7
Input::Type::Tuple
Tuple type proxy class.
Definition: type_tuple.hh:45
Input::Type::Record::end
KeyIter end() const
Container-like access to the keys of the Record.
Definition: type_record.hh:587
Input::Type::TypeBase::parameter_map_
ParameterMap parameter_map_
map of parameters if type is part of generic subtree
Definition: type_base.hh:307
Input::Type::TypeBase::attributes_
std::shared_ptr< attribute_map > attributes_
map of type attributes (e. g. input_type, name etc.)
Definition: type_base.hh:298
Input::Type::Default
Class Input::Type::Default specifies default value of keys of a Input::Type::Record.
Definition: type_record.hh:61
Input::Type::Tuple::Tuple
Tuple()
Default constructor. Empty handle.
Definition: type_tuple.cc:29
ASSERT_PERMANENT
#define ASSERT_PERMANENT(expr)
Allow use shorter versions of macro names if these names is not used with external library.
Definition: asserts.hh:348
Input::Type::Tuple::close
const Tuple & close() const
Close the Tuple for further declarations of keys.
Definition: type_tuple.cc:70
Input::Type::Abstract
Class for declaration of polymorphic Record.
Definition: type_abstract.hh:62
Input::Type::Tuple::declare_key
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
Input::Type::Instance
Helper class that stores data of generic types.
Definition: type_generic.hh:89
Input::Type::Record::declare_key
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
type_repository.hh
Input::Type::TypeBase::generic_type_hash_
TypeHash generic_type_hash_
hash string of generic type if type is derived, or empty string
Definition: type_base.hh:304
std::map< std::string, json_string >
Input::Type::Tuple::class_name
string class_name() const override
Override Type::TypeBase::class_name.
Definition: type_tuple.cc:52
Input::Type::Record::set_instance_data
ParameterMap set_instance_data(Record &rec, std::vector< ParameterPair > vec)
Declares a TYPE key of the Record.
Definition: type_record.cc:367
Input::Type::Tuple::operator==
bool operator==(const TypeBase &other) const override
Class comparison and Tuple type name comparison.
Definition: type_tuple.cc:64
Input::Type
Definition: balance.hh:41
Input::Type::Record
Record type proxy class.
Definition: type_record.hh:182
input_type.hh
Input::Type::generic_
@ generic_
Definition: type_base.hh:78
Input::Type::Tuple::obligatory_keys_count
unsigned int obligatory_keys_count() const
Return count of obligatory keys.
Definition: type_tuple.cc:139
Input::Type::Record::begin
KeyIter begin() const
Container-like access to the keys of the Record.
Definition: type_record.hh:579
Input::Type::Tuple::content_hash
TypeHash content_hash() const override
Definition: type_tuple.cc:43
Input::Type::Tuple::finish
FinishStatus finish(FinishStatus finish_type=FinishStatus::regular_) override
Finish declaration of the Tuple type.
Definition: type_tuple.cc:90
Input::Type::TUPLE_DECLARE_KEY
TUPLE_DECLARE_KEY(String)
Input::Type::Tuple::allow_auto_conversion
Tuple & allow_auto_conversion(const string &from_key) override
Override Record::allow_auto_conversion.
Definition: type_tuple.cc:57
Input::TypeRepository::add_type
std::shared_ptr< T > add_type(const T &type)
Add type to TypeRepository if doesn't exist there or get existing type with same TypeHash.
Definition: type_repository.hh:109
Input::Type::TypeBase
Base of classes for declaring structure of the input data.
Definition: type_base.hh:92
Input::Type::Record::data_
std::shared_ptr< RecordData > data_
Data handle.
Definition: type_record.hh:531
Input::Type::TypeBase::TypeHash
std::size_t TypeHash
Type returned by content_hash methods.
Definition: type_base.hh:95
Input::Type::Record::is_finished
bool is_finished() const override
Implements TypeBase::is_finished.
Definition: type_record.cc:231