Flow123d  last_with_con_2.0.0-663-gd0e2296
type_generic.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_generic.cc
15  * @brief
16  */
17 
18 #include <input/type_generic.hh>
19 #include <input/type_repository.hh>
20 #include <input/attribute_lib.hh>
21 
22 #include <boost/functional/hash.hpp>
23 
24 
25 
26 namespace Input {
27 
28 namespace Type {
29 
30 
31 /*******************************************************************
32  * implementation of Parameter
33  */
34 
35 Parameter::Parameter(const string & parameter_name)
36 : name_(parameter_name) {}
37 
38 
40 : name_(other.name_) {}
41 
42 
43 string Parameter::type_name() const {
44  return name_;
45 }
46 
47 
49  TypeHash seed=0;
50  boost::hash_combine(seed, "Parameter");
51  boost::hash_combine(seed, type_name());
52 
53  return seed;
54 }
55 
56 
58 
59  // Find the parameter value in the incoming vector.
60  auto parameter_iter = std::find_if(vec.begin(), vec.end(),
61  [this](const ParameterPair & item) -> bool { return item.first == this->name_; });
62  if (parameter_iter != vec.end()) {
63  ParameterMap parameter_map;
64  parameter_map[parameter_iter->first] = parameter_iter->second->content_hash();
65  return std::make_pair( parameter_iter->second, parameter_map );
66  } else {
67  // throw if the parameter value is missing
68  THROW( ExcParamaterNotSubsituted() << EI_Object(this->name_));
69  }
70 }
71 
72 
74  ASSERT(finish_type != FinishStatus::none_).error();
75 
76  if (finish_type == FinishStatus::regular_) THROW( ExcParamaterInIst() << EI_Object(this->name_));
77  return finish_type;
78 }
79 
80 
81 /*******************************************************************
82  * implementation of Instance
83  */
84 
86 : generic_type_(generic_type), parameters_(parameters) {}
87 
88 
90  TypeHash seed=0;
91  boost::hash_combine(seed, "Instance");
92  boost::hash_combine(seed, generic_type_.content_hash() );
94  boost::hash_combine(seed, (*it).first );
95  boost::hash_combine(seed, (*it).second->content_hash() );
96  }
97 
98  return seed;
99 }
100 
101 
102 const Instance &Instance::close() const {
104 }
105 
106 
108  return generic_type_.finish(finish_type);
109 }
110 
111 
112 /// Print parameter vector to formatted string.
113 
114 
115 // Implements @p TypeBase::make_instance.
117  // check if instance is created
118  if (created_instance_.first) {
119  return created_instance_;
120  }
121 
122  try {
124  } catch (ExcParamaterNotSubsituted &e) {
125 
126  ParameterMap aux_map;
127  for(auto &item : vec) aux_map[item.first]=0;
128  e << EI_ParameterList( TypeBase::print_parameter_map_keys_to_json(aux_map) );
129  throw;
130  }
131 
132 
133 
134 
135 #ifdef FLOW123D_DEBUG_ASSERTS
136  for (std::vector<TypeBase::ParameterPair>::const_iterator vec_it = parameters_.begin(); vec_it!=parameters_.end(); vec_it++) {
137  ParameterMap::iterator map_it = created_instance_.second.find( vec_it->first );
138 
139  ParameterMap aux_map;
140  for(auto &item : vec) aux_map[item.first]=0;
141 
142  ASSERT_DBG(map_it != created_instance_.second.end())(vec_it->first)(generic_type_.type_name())
143  .error("Unused parameter in input type instance");
144  }
145 #endif
146  return created_instance_;
147 }
148 
149 } // closing namespace Type
150 } // 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.
Base of classes for declaring structure of the input data.
Definition: type_base.hh:93
FinishStatus finish(FinishStatus finish_type=FinishStatus::regular_) override
Implements TypeBase::finish.
Definition: type_generic.cc:73
MakeInstanceReturnType make_instance(std::vector< ParameterPair > vec=std::vector< ParameterPair >()) override
Implements TypeBase::make_instance.
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
const Instance & close() const
Used for set Instance to TypeRepository.
MakeInstanceReturnType make_instance(std::vector< ParameterPair > vec=std::vector< ParameterPair >()) override
Implements TypeBase::make_instance.
Definition: type_generic.cc:57
Class for representing parametric types in IST.
Definition: type_generic.hh:52
Instance(TypeBase &generic_type, std::vector< TypeBase::ParameterPair > parameters)
Constructor.
Definition: type_generic.cc:85
virtual FinishStatus finish(FinishStatus finish_type=FinishStatus::regular_)
Finish method. Finalize construction of "Lazy types": Record, Selection, Abstract and generic type...
Definition: type_base.hh:206
Helper class that stores data of generic types.
Definition: type_generic.hh:88
virtual string type_name() const
Returns an identification of the type. Useful for error messages.
Definition: type_base.hh:133
#define ASSERT(expr)
Allow use shorter versions of macro names if these names is not used with external library...
Definition: asserts.hh:347
Parameter(const string &parameter_name)
Constructor.
Definition: type_generic.cc:35
static TypeRepository & get_instance()
Return singleton instance of class.
virtual TypeHash content_hash() const =0
Hash of the type specification.
TypeHash content_hash() const override
Implements TypeBase::content_hash.
Definition: type_generic.cc:48
const string name_
name of parameter
Definition: type_generic.hh:80
virtual MakeInstanceReturnType make_instance(ParameterVector vec=ParameterVector())=0
std::vector< TypeBase::ParameterPair > parameters_
Stores pairs of (name, Input::Type), that are used for replace of parameters in generic types...
std::pair< std::string, std::shared_ptr< TypeBase > > ParameterPair
Defines pairs of (name, Input::Type), that are used for replace of parameters in generic types...
Definition: type_base.hh:104
string type_name() const override
Implements Type::TypeBase::type_name.
Definition: type_generic.cc:43
FinishStatus finish(FinishStatus finish_type=FinishStatus::regular_) override
Finish declaration of the Instance type. Call finish of stored generic_type_.
TypeBase & generic_type_
Reference to generic types (contains some descendants of type Parameter).
#define ASSERT_DBG(expr)
Definition: asserts.hh:350
MakeInstanceReturnType created_instance_
Stores returned type created in first call of make_instance method.
TypeHash content_hash() const override
Implements TypeBase::content_hash.
Definition: type_generic.cc:89
json_string print_parameter_map_keys_to_json(ParameterMap param_map) const
Definition: type_base.cc:138
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