Flow123d  last_with_con_2.0.0-663-gd0e2296
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(!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 
84 
85 
87 {
88  std::size_t seed=0;
89  boost::hash_combine(seed, "Selection");
90  boost::hash_combine(seed, type_name());
91  boost::hash_combine(seed, data_->description_);
92  for( Key &key : data_->keys_) {
93  boost::hash_combine(seed, key.key_);
94  boost::hash_combine(seed, key.description_);
95  boost::hash_combine(seed, key.value);
96  }
97  return seed;
98 }
99 
100 
101 
103  return data_->finish_status_;
104 }
105 
106 
108  return data_->finish_status_ != FinishStatus::none_;
109 }
110 
111 
112 bool Selection::is_closed() const {
113  return data_->closed_;
114 }
115 
116 string Selection::type_name() const {
117  return data_->type_name_;
118 }
119 
120 
121 
122 bool Selection::operator==(const TypeBase &other) const {
123  return typeid(*this) == typeid(other) && (type_name() == static_cast<const Selection *>(&other)->type_name());
124 }
125 
126 
127 
128 int Selection::name_to_int(const string &key) const {
129  finished_check();
130  KeyHash key_h = key_hash(key);
131  SelectionData::key_to_index_const_iter it = data_->key_to_index_.find(key_h);
132  if (it != data_->key_to_index_.end())
133  return (data_->keys_[it->second].value);
134  else
135  throw ExcSelectionKeyNotFound() << EI_KeyName(key) << EI_Selection(*this);
136 }
137 
138 
139 string Selection::int_to_name(const int &val) const {
140  finished_check();
141  auto it = data_->value_to_index_.find(val);
142  if (it != data_->value_to_index_.end())
143  return data_->keys_[it->second].key_;
144  else
145  throw ExcSelectionValueNotFound() << EI_Value(val) << EI_Selection(*this);
146 }
147 
148 
150 {
151  for (auto it = sel.begin(); it != sel.end(); ++it)
152  {
153  int value = it->value;
154  while (data_->value_to_index_.find(value) != data_->value_to_index_.end()) value++;
155  add_value(value, it->key_, it->description_);
156  }
157 
158  return *this;
159 }
160 
161 
162 
163 string Selection::key_list() const {
164  ostringstream os;
165  for(unsigned int i=0; i<size(); i++) os << "'" <<data_->keys_[i].key_ << "' ";
166  return os.str();
167 }
168 
169 
170 
171 // Implements @p TypeBase::make_instance.
173  return std::make_pair( std::make_shared<Selection>(*this), ParameterMap() );
174 }
175 
176 
177 
178 void Selection::SelectionData::add_value(const int value, const std::string &key,
179  const std::string &description, TypeBase::attribute_map attributes) {
180  KeyHash key_h = TypeBase::key_hash(key);
181  ASSERT(key_to_index_.find(key_h) == key_to_index_.end())(key)(type_name_).error("Key already exists in Selection.");
182  value_to_index_const_iter it = value_to_index_.find(value);
183  const std::string &existing_key = keys_[it->second].key_;
184  ASSERT(it == value_to_index_.end())(value)(key)(existing_key)(type_name_)
185  .error("Value of new key conflicts with value of existing key in Selection");
186 
187  unsigned int new_idx = key_to_index_.size();
188  key_to_index_.insert(std::make_pair(key_h, new_idx));
189  value_to_index_.insert(std::make_pair(value, new_idx));
190 
191  Key tmp_key = { new_idx, key, description, value, attributes };
192  keys_.push_back(tmp_key);
193 }
194 
195 
196 
197 
198 
199 } // closing namespace Type
200 } // close 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.
std::map< int, unsigned int >::const_iterator value_to_index_const_iter
Container-like access to the map of valid value to index.
Base of classes for declaring structure of the input data.
Definition: type_base.hh:93
unsigned int size() const
Returns number of values in the Selection.
string int_to_name(const int &value) const
Returns value name for the given value.
string desc() const
Returns string with Type extensive documentation.
Definition: type_base.cc:75
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
int name_to_int(const string &key) const
Converts given value name key to the value.
Selection & copy_values(const Selection &sel)
Copy all keys and values from sel.
Selection & add_attribute(std::string key, TypeBase::json_string value)
std::shared_ptr< SelectionData > data_
Handle to actual Selection data.
FinishStatus finish_status() const override
Implements TypeBase::finish_status.
Base of all scalar types.
Definition: type_base.hh:455
static KeyHash key_hash(const string &str)
Hash function.
Definition: type_base.hh:270
#define ASSERT(expr)
Allow use shorter versions of macro names if these names is not used with external library...
Definition: asserts.hh:347
void add_attribute_(std::string name, json_string val)
Add attribute of given name to attribute map.
Definition: type_base.cc:110
Selection()
Default constructor. Empty selection.
static constexpr bool value
Definition: json.hpp:87
static TypeRepository & get_instance()
Return singleton instance of class.
string KeyHash
The type of hash values used in associative array that translates key names to indices in Record and ...
Definition: type_base.hh:267
Structure for description of one key in selection.
std::map< std::string, TypeHash > ParameterMap
Defines map of used parameters.
Definition: type_base.hh:109
FinishStatus finish(FinishStatus finish_type=FinishStatus::regular_) override
Finish declaration of the Selection type.
string type_name() const override
Implements Type::TypeBase::type_name.
bool is_finished() const override
Implements TypeBase::is_finished.
void add_value(const int value, const std::string &key, const std::string &description, TypeBase::attribute_map attributes)
Inster new value to the Selection.
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.
bool is_closed() const override
Implements TypeBase::is_closed.
keys_const_iterator end() const
Container-like access to the keys of the Selection.
TypeHash content_hash() const override
Implements TypeBase::content_hash.
const Selection & close() const
Close the Selection, no more values can be added.
std::map< KeyHash, unsigned int >::const_iterator key_to_index_const_iter
Container-like access to the map of valid value name to index.
std::string json_string
String stored in JSON format.
Definition: type_base.hh:99
bool operator==(const TypeBase &other) const override
Implements TypeBase::operator== compare also Selection names.
void finished_check() const
Assertion for finished Selection (methods are called in correct order).
keys_const_iterator begin() const
Container-like access to the keys of the Selection.
string key_list() const
Create string from values of keys.
std::size_t TypeHash
Type returned by content_hash methods.
Definition: type_base.hh:96
MakeInstanceReturnType make_instance(std::vector< ParameterPair > vec=std::vector< ParameterPair >()) override
Template for classes storing finite set of named values.