Flow123d  last_with_con_2.0.0-4-g42e6930
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 
73 bool Parameter::finish(bool is_generic) {
74  if (!is_generic) THROW( ExcParamaterInIst() << EI_Object(this->name_));
75  return true;
76 }
77 
78 
79 /*******************************************************************
80  * implementation of Instance
81  */
82 
84 : generic_type_(generic_type), parameters_(parameters) {}
85 
86 
88  TypeHash seed=0;
89  boost::hash_combine(seed, "Instance");
90  boost::hash_combine(seed, generic_type_.content_hash() );
92  boost::hash_combine(seed, (*it).first );
93  boost::hash_combine(seed, (*it).second->content_hash() );
94  }
95 
96  return seed;
97 }
98 
99 
100 const Instance &Instance::close() const {
102 }
103 
104 
105 bool Instance::finish(bool is_generic) {
106  return generic_type_.finish(true);
107 }
108 
109 
110 /// Print parameter vector to formatted string.
111 
112 
113 // Implements @p TypeBase::make_instance.
115  // check if instance is created
116  if (created_instance_.first) {
117  return created_instance_;
118  }
119 
120  try {
122  } catch (ExcParamaterNotSubsituted &e) {
123 
124  ParameterMap aux_map;
125  for(auto &item : vec) aux_map[item.first]=0;
126  e << EI_ParameterList( TypeBase::print_parameter_map_keys_to_json(aux_map) );
127  throw;
128  }
129 
130 
131 
132 
133 #ifdef FLOW123D_DEBUG_ASSERTS
134  for (std::vector<TypeBase::ParameterPair>::const_iterator vec_it = parameters_.begin(); vec_it!=parameters_.end(); vec_it++) {
135  ParameterMap::iterator map_it = created_instance_.second.find( vec_it->first );
136 
137  ParameterMap aux_map;
138  for(auto &item : vec) aux_map[item.first]=0;
139 
140  ASSERT_DBG(map_it != created_instance_.second.end())(vec_it->first)(generic_type_.type_name())
141  .error("Unused parameter in input type instance");
142  }
143 #endif
144  return created_instance_;
145 }
146 
147 } // closing namespace Type
148 } // closing namespace Input
Base of classes for declaring structure of the input data.
Definition: type_base.hh:79
virtual bool finish(bool is_generic=false)
Finish method. Finalize construction of "Lazy types": Record, Selection, Abstract and generic type...
Definition: type_base.hh:182
bool finish(bool is_generic=false) 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:97
TypeHash content_hash() const override
Implements TypeBase::content_hash.
Definition: type_generic.cc:87
const Instance & close() const
Used for set Instance to TypeRepository.
virtual TypeHash content_hash() const =0
Hash of the type specification.
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:83
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:116
bool finish(bool is_generic=false) override
Finish declaration of the Instance type. Call finish of stored generic_type_.
Parameter(const string &parameter_name)
Constructor.
Definition: type_generic.cc:35
string type_name() const override
Implements Type::TypeBase::type_name.
Definition: type_generic.cc:43
static TypeRepository & get_instance()
Return singleton instance of class.
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:90
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:48
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.
json_string print_parameter_map_keys_to_json(ParameterMap param_map) const
Definition: type_base.cc:134
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