Flow123d  release_2.1.0-84-g6a13a75
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 
48 string Parameter::class_name() const {
49  return "Parameter";
50 }
51 
52 
54  TypeHash seed=0;
55  boost::hash_combine(seed, "Parameter");
56  boost::hash_combine(seed, type_name());
57 
58  return seed;
59 }
60 
61 
63 
64  // Find the parameter value in the incoming vector.
65  auto parameter_iter = std::find_if(vec.begin(), vec.end(),
66  [this](const ParameterPair & item) -> bool { return item.first == this->name_; });
67  if (parameter_iter != vec.end()) {
68  ParameterMap parameter_map;
69  parameter_map[parameter_iter->first] = parameter_iter->second->content_hash();
70  return std::make_pair( parameter_iter->second, parameter_map );
71  } else {
72  // throw if the parameter value is missing
73  THROW( ExcParamaterNotSubsituted() << EI_Object(this->name_));
74  }
75 }
76 
77 
79  ASSERT(finish_type != FinishStatus::none_).error();
80 
81  if (finish_type == FinishStatus::regular_) THROW( ExcParamaterInIst() << EI_Object(this->name_));
82  return finish_type;
83 }
84 
85 
86 /*******************************************************************
87  * implementation of Instance
88  */
89 
91 : generic_type_(generic_type), parameters_(parameters) {}
92 
93 
95  TypeHash seed=0;
96  boost::hash_combine(seed, "Instance");
97  boost::hash_combine(seed, generic_type_.content_hash() );
99  boost::hash_combine(seed, (*it).first );
100  boost::hash_combine(seed, (*it).second->content_hash() );
101  }
102 
103  return seed;
104 }
105 
106 
107 const Instance &Instance::close() const {
109 }
110 
111 
113  return generic_type_.finish(finish_type);
114 }
115 
116 
117 /// Print parameter vector to formatted string.
118 
119 
120 // Implements @p TypeBase::make_instance.
122  // check if instance is created
123  if (created_instance_.first) {
124  return created_instance_;
125  }
126 
127  try {
129  } catch (ExcParamaterNotSubsituted &e) {
130 
131  ParameterMap aux_map;
132  for(auto &item : vec) aux_map[item.first]=0;
133  e << EI_ParameterList( TypeBase::print_parameter_map_keys_to_json(aux_map) );
134  throw;
135  }
136 
137 
138 
139 
140 #ifdef FLOW123D_DEBUG_ASSERTS
141  for (std::vector<TypeBase::ParameterPair>::const_iterator vec_it = parameters_.begin(); vec_it!=parameters_.end(); vec_it++) {
142  ParameterMap::iterator map_it = created_instance_.second.find( vec_it->first );
143 
144  ParameterMap aux_map;
145  for(auto &item : vec) aux_map[item.first]=0;
146 
147  ASSERT_DBG(map_it != created_instance_.second.end())(vec_it->first)(generic_type_.type_name())
148  .error("Unused parameter in input type instance");
149  }
150 #endif
151  return created_instance_;
152 }
153 
154 } // closing namespace Type
155 } // 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:78
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:48
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:192
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:62
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:90
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
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:53
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
virtual string type_name() const
Returns an identification of the type. Useful for error messages.
Definition: type_base.cc:182
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:94
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