Flow123d  JS_before_hm-981-g3f34df1
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 #include "system/asserts.hh" // for Assert, ASSERT
22 
23 #include <boost/functional/hash.hpp>
24 
25 
26 
27 namespace Input {
28 
29 namespace Type {
30 
31 
32 /*******************************************************************
33  * implementation of Parameter
34  */
35 
36 Parameter::Parameter(const string & parameter_name)
37 : name_(parameter_name) {}
38 
39 
41 : TypeBase(other), name_(other.name_) {}
42 
43 
44 string Parameter::type_name() const {
45  return name_;
46 }
47 
48 
49 string Parameter::class_name() const {
50  return "Parameter";
51 }
52 
53 
55  TypeHash seed=0;
56  boost::hash_combine(seed, "Parameter");
57  boost::hash_combine(seed, type_name());
58 
59  return seed;
60 }
61 
62 
64 
65  // Find the parameter value in the incoming vector.
66  auto parameter_iter = std::find_if(vec.begin(), vec.end(),
67  [this](const ParameterPair & item) -> bool { return item.first == this->name_; });
68  if (parameter_iter != vec.end()) {
69  ParameterMap parameter_map;
70  parameter_map[parameter_iter->first] = parameter_iter->second->content_hash();
71  return std::make_pair( parameter_iter->second, parameter_map );
72  } else {
73  // throw if the parameter value is missing
74  THROW( ExcParamaterNotSubsituted() << EI_Object(this->name_));
75  }
76 }
77 
78 
80  ASSERT(finish_type != FinishStatus::none_).error();
81 
82  if (finish_type == FinishStatus::regular_) THROW( ExcParamaterInIst() << EI_Object(this->name_));
83  return finish_type;
84 }
85 
86 
87 /*******************************************************************
88  * implementation of Instance
89  */
90 
92 : generic_type_(generic_type), parameters_(parameters) {}
93 
94 
96  TypeHash seed=0;
97  boost::hash_combine(seed, "Instance");
98  boost::hash_combine(seed, generic_type_.content_hash() );
100  boost::hash_combine(seed, (*it).first );
101  boost::hash_combine(seed, (*it).second->content_hash() );
102  }
103 
104  return seed;
105 }
106 
107 
108 const Instance &Instance::close() const {
110 }
111 
112 
114  return generic_type_.finish(finish_type);
115 }
116 
117 
118 /// Print parameter vector to formatted string.
119 
120 
121 // Implements @p TypeBase::make_instance.
123  // check if instance is created
124  if (created_instance_.first) {
125  return created_instance_;
126  }
127 
128  try {
130  } catch (ExcParamaterNotSubsituted &e) {
131 
132  ParameterMap aux_map;
133  for(auto &item : vec) aux_map[item.first]=0;
134  e << EI_ParameterList( TypeBase::print_parameter_map_keys_to_json(aux_map) );
135  throw;
136  }
137 
138 
139 
140 
141 #ifdef FLOW123D_DEBUG_ASSERTS
142  for (std::vector<TypeBase::ParameterPair>::const_iterator vec_it = parameters_.begin(); vec_it!=parameters_.end(); vec_it++) {
143  ParameterMap::iterator map_it = created_instance_.second.find( vec_it->first );
144 
145  ParameterMap aux_map;
146  for(auto &item : vec) aux_map[item.first]=0;
147 
148  ASSERT_DBG(map_it != created_instance_.second.end())(vec_it->first)(generic_type_.type_name())
149  .error("Unused parameter in input type instance");
150  }
151 #endif
152  return created_instance_;
153 }
154 
155 } // closing namespace Type
156 } // 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:92
FinishStatus finish(FinishStatus finish_type=FinishStatus::regular_) override
Implements TypeBase::finish.
Definition: type_generic.cc:79
ArmaVec< double, N > vec
Definition: armor.hh:861
MakeInstanceReturnType make_instance(std::vector< ParameterPair > vec=std::vector< ParameterPair >()) override
Implements TypeBase::make_instance.
string class_name() const override
Override Type::TypeBase::class_name.
Definition: type_generic.cc:49
virtual FinishStatus finish(FinishStatus finish_type=FinishStatus::regular_)
Finish method. Finalize construction of "Lazy types": Record, Selection, Abstract and generic type...
Definition: type_base.cc:216
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
Abstract linear system class.
Definition: balance.hh:40
const Instance & close() const
Used for set Instance to TypeRepository.
Definitions of ASSERTS.
MakeInstanceReturnType make_instance(std::vector< ParameterPair > vec=std::vector< ParameterPair >()) override
Implements TypeBase::make_instance.
Definition: type_generic.cc:63
Class for representing parametric types in IST.
Definition: type_generic.hh:53
Instance(TypeBase &generic_type, std::vector< TypeBase::ParameterPair > parameters)
Constructor.
Definition: type_generic.cc:91
Helper class that stores data of generic types.
Definition: type_generic.hh:89
#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:36
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:54
const string name_
name of parameter
Definition: type_generic.hh:81
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:44
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)
virtual string type_name() const
Returns an identification of the type. Useful for error messages.
Definition: type_base.cc:206
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:95
json_string print_parameter_map_keys_to_json(ParameterMap param_map) const
Definition: type_base.cc:162
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