Flow123d  JS_before_hm-1804-gf2ad740aa
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
Armor::vec
ArmaVec< double, N > vec
Definition: armor.hh:885
Input::Type::FinishStatus
FinishStatus
Definition: type_base.hh:74
Input::Type::Instance::content_hash
TypeHash content_hash() const override
Implements TypeBase::content_hash.
Definition: type_generic.cc:95
Input
Abstract linear system class.
Definition: balance.hh:40
Input::Type::Instance::close
const Instance & close() const
Used for set Instance to TypeRepository.
Definition: type_generic.cc:108
Input::Type::Instance::finish
FinishStatus finish(FinishStatus finish_type=FinishStatus::regular_) override
Finish declaration of the Instance type. Call finish of stored generic_type_.
Definition: type_generic.cc:113
ASSERT
#define ASSERT(expr)
Allow use shorter versions of macro names if these names is not used with external library.
Definition: asserts.hh:347
Input::Type::Instance::generic_type_
TypeBase & generic_type_
Reference to generic types (contains some descendants of type Parameter).
Definition: type_generic.hh:118
Input::Type::Instance::Instance
Instance(TypeBase &generic_type, std::vector< TypeBase::ParameterPair > parameters)
Constructor.
Definition: type_generic.cc:91
asserts.hh
Definitions of ASSERTS.
Input::Type::TypeBase::MakeInstanceReturnType
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
Input::TypeRepository::get_instance
static TypeRepository & get_instance()
Return singleton instance of class.
Definition: type_repository.hh:59
ASSERT_DBG
#define ASSERT_DBG(expr)
Definition: include_fadbad.hh:28
Input::Type::none_
@ none_
Definition: type_base.hh:75
THROW
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.
Definition: exceptions.hh:53
std::vector
Definition: doxy_dummy_defs.hh:7
Input::Type::Parameter::Parameter
Parameter(const string &parameter_name)
Constructor.
Definition: type_generic.cc:36
Input::Type::TypeBase::finish
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
Input::Type::Instance::created_instance_
MakeInstanceReturnType created_instance_
Stores returned type created in first call of make_instance method.
Definition: type_generic.hh:128
Input::Type::Parameter::class_name
string class_name() const override
Override Type::TypeBase::class_name.
Definition: type_generic.cc:49
Input::Type::TypeBase::content_hash
virtual TypeHash content_hash() const =0
Hash of the type specification.
attribute_lib.hh
type_generic.hh
Input::Type::TypeBase::make_instance
virtual MakeInstanceReturnType make_instance(ParameterVector vec=ParameterVector())=0
Input::Type::regular_
@ regular_
Definition: type_base.hh:77
Input::Type::Instance::make_instance
MakeInstanceReturnType make_instance(std::vector< ParameterPair > vec=std::vector< ParameterPair >()) override
Implements TypeBase::make_instance.
Definition: type_generic.cc:122
Input::Type::Instance
Helper class that stores data of generic types.
Definition: type_generic.hh:89
Input::Type::Parameter::make_instance
MakeInstanceReturnType make_instance(std::vector< ParameterPair > vec=std::vector< ParameterPair >()) override
Implements TypeBase::make_instance.
Definition: type_generic.cc:63
type_repository.hh
std::map< std::string, TypeHash >
Input::Type::Parameter::content_hash
TypeHash content_hash() const override
Implements TypeBase::content_hash.
Definition: type_generic.cc:54
Input::Type
Definition: balance.hh:41
Input::Type::TypeBase::ParameterPair
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
Input::Type::Parameter::finish
FinishStatus finish(FinishStatus finish_type=FinishStatus::regular_) override
Implements TypeBase::finish.
Definition: type_generic.cc:79
Input::Type::TypeBase::type_name
virtual string type_name() const
Returns an identification of the type. Useful for error messages.
Definition: type_base.cc:206
Input::Type::Parameter::name_
const string name_
name of parameter
Definition: type_generic.hh:81
Input::Type::Parameter::type_name
string type_name() const override
Implements Type::TypeBase::type_name.
Definition: type_generic.cc:44
Input::TypeRepository::add_type
std::shared_ptr< T > add_type(const T &type)
Add type to TypeRepository if doesn't exist there or get existing type with same TypeHash.
Definition: type_repository.hh:109
Input::Type::Parameter
Class for representing parametric types in IST.
Definition: type_generic.hh:53
Input::Type::TypeBase
Base of classes for declaring structure of the input data.
Definition: type_base.hh:92
Input::Type::Instance::parameters_
std::vector< TypeBase::ParameterPair > parameters_
Stores pairs of (name, Input::Type), that are used for replace of parameters in generic types.
Definition: type_generic.hh:121
Input::Type::TypeBase::print_parameter_map_keys_to_json
json_string print_parameter_map_keys_to_json(ParameterMap param_map) const
Definition: type_base.cc:162
Input::Type::TypeBase::TypeHash
std::size_t TypeHash
Type returned by content_hash methods.
Definition: type_base.hh:95