Flow123d  3.9.0-be1f591da
type_selection.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_selection.cc
15  * @brief
16  */
17 
18 #include "input/type_selection.hh"
19 #include "input/type_repository.hh"
20 #include <boost/functional/hash.hpp>
21 
22 namespace Input {
23 namespace Type {
24 
25 using std::string;
26 
28 : data_(std::make_shared<SelectionData>("EmptySelection"))
29 {
30  close();
31  finish();
32 }
33 
34 
35 
37 : Scalar(other), data_(other.data_)
38 { }
39 
40 
41 
42 Selection::Selection(const string &name, const string &desc)
43 : data_(std::make_shared<SelectionData>(name))
44 {
45  data_->description_=desc;
46 }
47 
48 
49 
50 Selection &Selection::add_value(const int value, const std::string &key,
51  const std::string &description, TypeBase::attribute_map attributes)
52 {
53  ASSERT_PERMANENT(!is_closed())(key)(type_name()).error("Declaration of new key in closed Selection.");
54 
55  data_->add_value(value, key, description, attributes);
56  return *this;
57 }
58 
60  this->add_attribute_(key, value);
61  return *this;
62 }
63 
64 
65 const Selection & Selection::close() const {
66  data_->closed_=true;
68 }
69 
70 
72  ASSERT(finish_type != FinishStatus::none_).error();
73 
74  if (this->is_finished()) return data_->finish_status_;
75 
76  ASSERT(data_->closed_)(this->type_name()).error();
77 
78  data_->finish_status_ = finish_type;
79  return data_->finish_status_;
80 }
81 
82 
83 
85 {
86  std::size_t seed=0;
87  boost::hash_combine(seed, "Selection");
88  boost::hash_combine(seed, type_name());
89  boost::hash_combine(seed, data_->description_);
90  for( Key &key : data_->keys_) {
91  boost::hash_combine(seed, key.key_);
92  boost::hash_combine(seed, key.description_);
93  boost::hash_combine(seed, key.value);
94  }
95  return seed;
96 }
97 
98 
99 
101  return data_->finish_status_;
102 }
103 
104 
106  return data_->finish_status_ != FinishStatus::none_;
107 }
108 
109 
110 bool Selection::is_closed() const {
111  return data_->closed_;
112 }
113 
114 string Selection::type_name() const {
115  return data_->type_name_;
116 }
117 
118 
119 string Selection::class_name() const {
120  return "Selection";
121 }
122 
123 
124 bool Selection::operator==(const TypeBase &other) const {
125  return typeid(*this) == typeid(other) && (type_name() == static_cast<const Selection *>(&other)->type_name());
126 }
127 
128 
129 
130 int Selection::name_to_int(const string &key) const {
131  finished_check();
132  KeyHash key_h = key_hash(key);
133  SelectionData::key_to_index_const_iter it = data_->key_to_index_.find(key_h);
134  if (it != data_->key_to_index_.end())
135  return (data_->keys_[it->second].value);
136  else
137  throw ExcSelectionKeyNotFound() << EI_KeyName(key) << EI_Selection(*this);
138 }
139 
140 
141 string Selection::int_to_name(const int &val) const {
142  finished_check();
143  auto it = data_->value_to_index_.find(val);
144  if (it != data_->value_to_index_.end())
145  return data_->keys_[it->second].key_;
146  else
147  throw ExcSelectionValueNotFound() << EI_Value(val) << EI_Selection(*this);
148 }
149 
150 
152 {
153  for (auto it = sel.begin(); it != sel.end(); ++it)
154  {
155  int value = it->value;
156  while (data_->value_to_index_.find(value) != data_->value_to_index_.end()) value++;
157  add_value(value, it->key_, it->description_);
158  }
159 
160  return *this;
161 }
162 
163 
164 
165 string Selection::key_list() const {
166  ostringstream os;
167  for(unsigned int i=0; i<size(); i++) os << "'" <<data_->keys_[i].key_ << "' ";
168  return os.str();
169 }
170 
171 
172 
173 // Implements @p TypeBase::make_instance.
175  return std::make_pair( std::make_shared<Selection>(*this), ParameterMap() );
176 }
177 
178 
179 
181 : type_name_(name), closed_(false), finish_status_(FinishStatus::none_)
182 {}
183 
184 
185 
186 void Selection::SelectionData::add_value(const int value, const std::string &key,
187  const std::string &description, TypeBase::attribute_map attributes) {
188  KeyHash key_h = TypeBase::key_hash(key);
189  ASSERT_PERMANENT(key_to_index_.find(key_h) == key_to_index_.end())(key)(type_name_).error("Key already exists in Selection.");
190  value_to_index_const_iter it = value_to_index_.find(value);
191  const std::string &existing_key = keys_[it->second].key_;
192  ASSERT_PERMANENT(it == value_to_index_.end())(value)(key)(existing_key)(type_name_)
193  .error("Value of new key conflicts with value of existing key in Selection");
194 
195  unsigned int new_idx = key_to_index_.size();
196  key_to_index_.insert(std::make_pair(key_h, new_idx));
197  value_to_index_.insert(std::make_pair(value, new_idx));
198 
199  Key tmp_key = { new_idx, key, description, value, attributes };
200  keys_.push_back(tmp_key);
201 }
202 
203 
204 
205 
206 
207 } // closing namespace Type
208 } // close namespace Input
Armor::vec
ArmaVec< double, N > vec
Definition: armor.hh:885
Input::Type::Selection::begin
keys_const_iterator begin() const
Container-like access to the keys of the Selection.
Definition: type_selection.hh:291
Input::Type::FinishStatus
FinishStatus
Definition: type_base.hh:74
Input::Type::Selection::Key::description_
string description_
Definition: type_selection.hh:83
Input
Abstract linear system class.
Definition: balance.hh:40
Input::Type::Selection::operator==
bool operator==(const TypeBase &other) const override
Implements TypeBase::operator== compare also Selection names.
Definition: type_selection.cc:124
ASSERT
#define ASSERT(expr)
Definition: asserts.hh:351
Input::Type::Selection::is_finished
bool is_finished() const override
Implements TypeBase::is_finished.
Definition: type_selection.cc:105
Input::Type::Selection::close
const Selection & close() const
Close the Selection, no more values can be added.
Definition: type_selection.cc:65
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::Type::TypeBase::add_attribute_
void add_attribute_(std::string name, json_string val)
Add attribute of given name to attribute map.
Definition: type_base.cc:134
Input::TypeRepository::get_instance
static TypeRepository & get_instance()
Return singleton instance of class.
Definition: type_repository.hh:59
value
static constexpr bool value
Definition: json.hpp:87
Input::Type::none_
@ none_
Definition: type_base.hh:75
Input::Type::TypeBase::key_hash
static KeyHash key_hash(const string &str)
Hash function.
Definition: type_base.hh:261
Input::Type::Selection::is_closed
bool is_closed() const override
Implements TypeBase::is_closed.
Definition: type_selection.cc:110
Input::Type::Selection::class_name
string class_name() const override
Override Type::TypeBase::class_name.
Definition: type_selection.cc:119
std::vector
Definition: doxy_dummy_defs.hh:7
Input::Type::Selection::type_name
string type_name() const override
Implements Type::TypeBase::type_name.
Definition: type_selection.cc:114
Input::Type::Scalar
Base of all scalar types.
Definition: type_base.hh:442
Input::Type::Selection::key_list
string key_list() const
Create string from values of keys.
Definition: type_selection.cc:165
type_selection.hh
Input::Type::Selection::SelectionData
Internal data class.
Definition: type_selection.hh:211
Input::Type::Selection::name_to_int
int name_to_int(const string &key) const
Converts given value name key to the value.
Definition: type_selection.cc:130
Input::Type::Selection::Key
Structure for description of one key in selection.
Definition: type_selection.hh:80
Input::Type::Selection::Key::value
int value
Definition: type_selection.hh:84
ASSERT_PERMANENT
#define ASSERT_PERMANENT(expr)
Allow use shorter versions of macro names if these names is not used with external library.
Definition: asserts.hh:348
Input::Type::Selection::finish_status
FinishStatus finish_status() const override
Implements TypeBase::finish_status.
Definition: type_selection.cc:100
Input::Type::Selection::SelectionData::value_to_index_const_iter
std::map< int, unsigned int >::const_iterator value_to_index_const_iter
Container-like access to the map of valid value to index.
Definition: type_selection.hh:233
Input::Type::TypeBase::KeyHash
string KeyHash
The type of hash values used in associative array that translates key names to indices in Record and ...
Definition: type_base.hh:258
Input::Type::Selection::copy_values
Selection & copy_values(const Selection &sel)
Copy all keys and values from sel.
Definition: type_selection.cc:151
type_repository.hh
Input::Type::Selection::end
keys_const_iterator end() const
Container-like access to the keys of the Selection.
Definition: type_selection.hh:299
Input::Type::Selection
Template for classes storing finite set of named values.
Definition: type_selection.hh:65
std::map< std::string, json_string >
Input::Type::Selection::Selection
Selection()
Default constructor. Empty selection.
Definition: type_selection.cc:27
Input::Type::TypeBase::ParameterMap
std::map< std::string, TypeHash > ParameterMap
Defines map of used parameters.
Definition: type_base.hh:109
Input::Type
Definition: balance.hh:41
Input::Type::Selection::Key::key_
string key_
Definition: type_selection.hh:82
Input::Type::Selection::make_instance
MakeInstanceReturnType make_instance(std::vector< ParameterPair > vec=std::vector< ParameterPair >()) override
Definition: type_selection.cc:174
Input::Type::Selection::size
unsigned int size() const
Returns number of values in the Selection.
Definition: type_selection.hh:276
std
Definition: doxy_dummy_defs.hh:5
Input::Type::Selection::finish
FinishStatus finish(FinishStatus finish_type=FinishStatus::regular_) override
Finish declaration of the Selection type.
Definition: type_selection.cc:71
Input::Type::TypeBase::json_string
std::string json_string
String stored in JSON format.
Definition: type_base.hh:99
Input::Type::Selection::SelectionData::key_to_index_const_iter
std::map< KeyHash, unsigned int >::const_iterator key_to_index_const_iter
Container-like access to the map of valid value name to index.
Definition: type_selection.hh:228
Input::Type::TypeBase::desc
string desc() const
Returns string with Type extensive documentation.
Definition: type_base.cc:99
Input::Type::Selection::SelectionData::add_value
void add_value(const int value, const std::string &key, const std::string &description, TypeBase::attribute_map attributes)
Inster new value to the Selection.
Definition: type_selection.cc:186
Input::Type::Selection::finished_check
void finished_check() const
Assertion for finished Selection (methods are called in correct order).
Definition: type_selection.hh:285
Input::Type::Selection::int_to_name
string int_to_name(const int &value) const
Returns value name for the given value.
Definition: type_selection.cc:141
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::TypeBase
Base of classes for declaring structure of the input data.
Definition: type_base.hh:92
Input::Type::Selection::add_value
Selection & add_value(const int value, const std::string &key, const std::string &description="", TypeBase::attribute_map attributes=TypeBase::attribute_map())
Adds one new value with name given by key to the Selection.
Definition: type_selection.cc:50
Input::Type::Selection::SelectionData::SelectionData
SelectionData(const string &name)
Constructor.
Definition: type_selection.cc:180
Input::Type::Selection::content_hash
TypeHash content_hash() const override
Implements TypeBase::content_hash.
Definition: type_selection.cc:84
Input::Type::TypeBase::TypeHash
std::size_t TypeHash
Type returned by content_hash methods.
Definition: type_base.hh:95
Input::Type::Selection::add_attribute
Selection & add_attribute(std::string key, TypeBase::json_string value)
Definition: type_selection.cc:59
Input::Type::Selection::data_
std::shared_ptr< SelectionData > data_
Handle to actual Selection data.
Definition: type_selection.hh:249
FMT_UNUSED
#define FMT_UNUSED
Definition: posix.h:75