Flow123d  last_with_con_2.0.0-4-g42e6930
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 : Record(type_name_in, description) {}
39 
40 
42 {
43  TypeHash seed=0;
44  boost::hash_combine(seed, "Tuple");
45  data_->content_hash(seed);
46  return seed;
47 }
48 
49 
50 Tuple & Tuple::allow_auto_conversion(const string &from_key)
51 {
52  ASSERT(false)(this->type_name()).error("Call of allow_auto_conversion method is forbidden for type Tuple");
53  return *this;
54 }
55 
56 
57 bool Tuple::operator==(const TypeBase &other) const
58 { return typeid(*this) == typeid(other) &&
59  (type_name() == static_cast<const Tuple *>(&other)->type_name() );
60 }
61 
62 
63 const Tuple &Tuple::close() const {
64  data_->closed_=true;
66 }
67 
68 
69 bool Tuple::finish(bool is_generic)
70 {
71  if (data_->finished) return true;
72 
73  ASSERT(data_->closed_)(this->type_name()).error();
74 
75  data_->finished = true;
76 
77  // iterates through keys
78  bool obligatory_keys = true; // check order of keys (at first obligatory keys are defined, then other keys)
79  bool allow_auto_conversion = true;
80  for (vector<Key>::iterator it=data_->keys.begin(); it!=data_->keys.end(); it++) {
81  // Performs check order of keys and check auto-conversion
82  Default dflt = it->default_;
83  if ( dflt.is_obligatory() ) {
84  if (it != data_->keys.begin()) {
85  allow_auto_conversion = false;
86  }
87  if ( !obligatory_keys ) {
88  THROW( ExcTupleWrongKeysOrder() << EI_TupleName(this->type_name()) );
89  }
90  } else {
91  obligatory_keys = false;
92  }
93 
94  // Performs finish of keys
95  if (typeid( *(it->type_.get()) ) == typeid(Instance)) it->type_ = it->type_->make_instance().first;
96  if (!is_generic && it->type_->is_root_of_generic_subtree()) THROW( ExcGenericWithoutInstance() << EI_Object(it->type_->type_name()) );
97  data_->finished = data_->finished && it->type_->finish(is_generic);
98  }
99 
100  // Add autoconvertibility
101  if (allow_auto_conversion) {
102  data_->auto_conversion_key_idx = 0;
103  data_->auto_conversion_key=data_->keys.begin()->key_;
104  }
105 
106  return (data_->finished);
107 }
108 
109 
111 {
112  ASSERT(false)(this->type_name()).error("Call of derive_from method is forbidden for type Tuple");
113  return *this;
114 }
115 
116 
117 unsigned int Tuple::obligatory_keys_count() const {
118  unsigned int obligatory_keys_count=0;
119  for ( KeyIter it= this->begin(); it != this->end(); ++it) {
120  if ( it->default_.is_obligatory() ) ++obligatory_keys_count;
121  }
122  return obligatory_keys_count;
123 }
124 
125 
127  Tuple tuple = this->deep_copy();
128  ParameterMap parameter_map;
129  this->set_instance_data(tuple, parameter_map, vec);
130 
131  return std::make_pair( std::make_shared<Tuple>(tuple.close()), parameter_map );
132 }
133 
134 
136  Tuple tuple = Tuple();
137  tuple.data_ = std::make_shared<Record::RecordData>(*this->data_);
138  tuple.data_->closed_ = false;
139  tuple.data_->finished = false;
141  tuple.parameter_map_ = this->parameter_map_;
143  return tuple;
144 }
145 
146 
149  return *this;
150 }
151 
152 
153 Tuple &Tuple::declare_key(const string &key, std::shared_ptr<TypeBase> type,
154  const Default &default_value, const string &description,
155  TypeBase::attribute_map key_attributes)
156 {
157  Record::declare_key(key, type, default_value, description, key_attributes);
158  return *this;
159 }
160 
161 
162 template <class KeyType>
163 Tuple &Tuple::declare_key(const string &key, const KeyType &type,
164  const Default &default_value, const string &description,
165  TypeBase::attribute_map key_attributes)
166 {
167  Record::declare_key(key, type, default_value, description, key_attributes);
168  return *this;
169 }
170 
171 
172 
173 template <class KeyType>
174 Tuple &Tuple::declare_key(const string &key, const KeyType &type,
175  const string &description,
176  TypeBase::attribute_map key_attributes)
177 {
178  Record::declare_key(key, type, description, key_attributes);
179  return *this;
180 }
181 
182 
183 
184 // explicit instantiation of template methods
185 
186 #define TUPLE_DECLARE_KEY(TYPE) \
187 template Tuple & Tuple::declare_key<TYPE>(const string &key, const TYPE &type, const Default &default_value, const string &description, TypeBase::attribute_map key_attributes); \
188 template Tuple & Tuple::declare_key<TYPE>(const string &key, const TYPE &type, const string &description, TypeBase::attribute_map key_attributes)
189 
190 
191 TUPLE_DECLARE_KEY(String);
192 TUPLE_DECLARE_KEY(Integer);
193 TUPLE_DECLARE_KEY(Double);
194 TUPLE_DECLARE_KEY(Bool);
195 TUPLE_DECLARE_KEY(FileName);
196 TUPLE_DECLARE_KEY(Selection);
201 TUPLE_DECLARE_KEY(Parameter);
202 TUPLE_DECLARE_KEY(Instance);
204 
205 
206 } // closing namespace Type
207 } // closing namespace Input
TUPLE_DECLARE_KEY(String)
Base of classes for declaring structure of the input data.
Definition: type_base.hh:79
void copy_attributes(attribute_map other_attributes)
Definition: type_base.cc:154
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:153
bool root_of_generic_subtree_
flag is true if type should be root of generic subtree
Definition: type_base.hh:283
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:97
Tuple deep_copy() const
Overrides Record::deep_copy.
Definition: type_tuple.cc:135
string type_name() const override
Implements Type::TypeBase::type_name.
Definition: type_record.cc:299
const Tuple & close() const
Close the Tuple for further declarations of keys.
Definition: type_tuple.cc:63
Helper class that stores data of generic types.
Definition: type_generic.hh:88
#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:280
Tuple & allow_auto_conversion(const string &from_key) override
Override Record::allow_auto_conversion.
Definition: type_tuple.cc:50
KeyIter begin() const
Container-like access to the keys of the Record.
Definition: type_record.hh:559
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:289
Record()
Default constructor. Empty handle.
Definition: type_record.cc:101
MakeInstanceReturnType make_instance(std::vector< ParameterPair > vec=std::vector< ParameterPair >()) override
Implements TypeBase::make_instance.
Definition: type_tuple.cc:126
TypeHash content_hash() const override
Definition: type_tuple.cc:41
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:468
Tuple & root_of_generic_subtree() override
Overrides Record::root_of_generic_subtree.
Definition: type_tuple.cc:147
friend class Abstract
Definition: type_record.hh:173
Class for declaration of polymorphic Record.
Tuple & derive_from(Abstract &parent)
Override Record::derive_from.
Definition: type_tuple.cc:110
bool finish(bool is_generic=false) override
Finish declaration of the Tuple type.
Definition: type_tuple.cc:69
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:286
friend class AdHocAbstract
Definition: type_record.hh:174
friend class Array
Definition: type_base.hh:291
KeyIter end() const
Container-like access to the keys of the Record.
Definition: type_record.hh:567
unsigned int obligatory_keys_count() const
Return count of obligatory keys.
Definition: type_tuple.cc:117
bool operator==(const TypeBase &other) const
Class comparison and Tuple type name comparison.
Definition: type_tuple.cc:57
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:511
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.
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)
Set data of Instance of generic type.
Definition: type_record.cc:345
std::size_t TypeHash
Type returned by content_hash methods.
Definition: type_base.hh:82
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.
Definition: exceptions.hh:45