Flow123d  last_with_con_2.0.0-663-gd0e2296
type_selection.hh
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.hh
15  * @brief
16  */
17 
18 #ifndef TYPE_SELECTION_HH_
19 #define TYPE_SELECTION_HH_
20 
21 #include "system/exceptions.hh"
22 
23 #include "system/system.hh"
24 #include "type_base.hh"
25 
26 namespace Input {
27 namespace Type {
28 
29 using std::string;
30 
31 
32 
33 /**
34  * @brief Template for classes storing finite set of named values.
35  *
36  * The primary purpose of this class is initialization of enum variables. Since C++ provides no reflection,
37  * in particular no access to enum identifiers as strings, you has to construct the Selection object consistent
38  * with an enum you want to initialize.
39  *
40  * Similarly to Type::Record and Type::Abstract the Selection class is only proxy to the actual data.
41  *
42  * Usage:
43  @code
44  enum Colors { blue, white };
45 
46  const Selection & SomeClass::get_selection_input_type() {
47  return Selection("Colors", "Set of color.")
48  .add_value(blue, "blue")
49  .add_value(white,"white","White color") // with optional item description
50  .close();
51  }
52  @endcode
53  *
54  *
55  * TODO: We can not guarantee full compatibility of the Selection with corresponding Enum type
56  * the Selection can have fewer values since we can not get number of values in the Enum.
57  * Therefore we either have to move under C++11, where enum classes may provide elementary
58  * reflection or have Selection of simple ints.
59  *
60  * @ingroup input_types
61  */
62 
63 class Selection : public Scalar {
64  friend class OutputBase;
65 
66 public:
67  /*
68  * Exceptions specific to this class.
69  */
70  TYPEDEF_ERR_INFO( EI_Selection, const Selection );
71  TYPEDEF_ERR_INFO( EI_Value, const int);
72  DECLARE_EXCEPTION( ExcSelectionKeyNotFound,
73  << "Key " << EI_KeyName::qval <<" not found in Selection:\n" << EI_Selection::val );
74  DECLARE_EXCEPTION( ExcSelectionValueNotFound,
75  << "Value " << EI_Value::val <<" not found in Selection:\n" << EI_Selection::val );
76 
77  /// Structure for description of one key in selection
78  struct Key {
79  unsigned int key_index;
80  string key_;
81  string description_;
82  int value;
84  };
85 
86  /// Public typedef of constant iterator into array of keys
88 
89  /// Default constructor. Empty selection.
90  Selection();
91 
92 
93  /// Copy constructor.
94  Selection(const Selection& other);
95 
96  /// Creates a handle pointing to the new SelectionData.
97  Selection(const string &name, const std::string &description = "");
98 
99  /**
100  * @brief Adds one new @p value with name given by @p key to the Selection.
101  *
102  * The @p description of meaning of the value could be provided.
103  */
104  Selection &add_value(const int value, const std::string &key,
105  const std::string &description = "", TypeBase::attribute_map attributes = TypeBase::attribute_map() );
106 
107  Selection &add_attribute(std::string key, TypeBase::json_string value);
108 
109  /// Close the Selection, no more values can be added.
110  const Selection &close() const;
111 
112 
113  /**
114  * @brief Implements @p TypeBase::content_hash.
115  *
116  * Hash is calculated by type name, description, hash of keys and attributes.
117  */
118  TypeHash content_hash() const override;
119 
120 
121  /// Implements @p TypeBase::finish_status.
122  FinishStatus finish_status() const override;
123 
124  /// Implements \p TypeBase::is_finished
125  bool is_finished() const override;
126 
127  /**
128  * @brief Implements @p Type::TypeBase::type_name.
129  *
130  * Name corresponds to @p data->type_name_.
131  */
132  string type_name() const override;
133  /// Override @p Type::TypeBase::class_name.
134  string class_name() const override { return "Selection"; }
135 
136  /// Implements \p TypeBase::operator== compare also Selection names.
137  bool operator==(const TypeBase &other) const override;
138 
139  /**
140  * @brief Container-like access to the keys of the Selection.
141  *
142  * Returns iterator to the first key.
143  */
144  inline keys_const_iterator begin() const;
145 
146  /**
147  * @brief Container-like access to the keys of the Selection.
148  *
149  * Returns iterator to the last key.
150  */
151  inline keys_const_iterator end() const;
152 
153  /// Returns iterator to the key struct for given key string.
154  inline keys_const_iterator key_iterator(const string& key) const;
155 
156  /**
157  * @brief Converts given value name \p key to the value.
158  *
159  * Throws exception if the value name does not exist.
160  */
161  int name_to_int(const string &key) const;
162 
163  /**
164  * @brief Returns value name for the given \p value.
165  *
166  * Throws exception if the value does not exist.
167  */
168  string int_to_name(const int &value) const;
169 
170  /// Copy all keys and values from @p sel
171  Selection &copy_values(const Selection &sel);
172 
173  /// Just check if there is a particular name in the Selection.
174  inline bool has_name(const string &key) const;
175 
176  /// Check if there is a particular value in the Selection.
177  inline bool has_value(const int &val) const;
178 
179  /// Returns number of values in the Selection.
180  inline unsigned int size() const;
181 
182  /// Finish declaration of the Selection type.
183  FinishStatus finish(FinishStatus finish_type = FinishStatus::regular_) override;
184 
185 
186  /// Implements \p TypeBase::is_closed
187  bool is_closed() const override;
188 
189 
190  // Implements @p TypeBase::make_instance.
192 private:
193 
194  /// Assertion for finished Selection (methods are called in correct order).
195  void finished_check() const;
196 
197  /**
198  * @brief Create string from values of keys.
199  *
200  * Used in error messaged, where we can not use desc(), which can lead to infinite loop due to TYPE selection of Abstract.
201  */
202  string key_list() const;
203 
204  /**
205  * @brief Internal data class.
206  *
207  * Stores data of the Selection.
208  */
210  public:
211 
212  /// Constructor.
213  SelectionData(const string &name)
214  : type_name_(name), closed_(false), finish_status_(FinishStatus::none_)
215  {}
216 
217  /// Inster new value to the Selection
218  void add_value(const int value, const std::string &key,
219  const std::string &description, TypeBase::attribute_map attributes);
220 
221 
222  /// Name of the Selection.
223  string type_name_;
224 
225  /// Map : valid value name to index.
227  /// Container-like access to the map of valid value name to index
229 
230  /// Map : valid value to index.
232  /// Container-like access to the map of valid value to index
234 
235  /// Vector of values of the Selection
237 
238  /// Text description of the whole Selection object.
239  std::string description_;
240 
241  /// Indicator of closed Selection.
242  mutable bool closed_;
243 
244  /// Indicator of finished Selection.
246  };
247 
248  /// Handle to actual Selection data.
249  std::shared_ptr<SelectionData> data_;
250 
251 };
252 
253 
254 
255 
256 
257 /******************************************************************************
258  * Implementation of inline methods.
259  */
260 
261 inline bool Selection::has_name(const string &key) const {
262  finished_check();
263  KeyHash key_h = key_hash(key);
264  return (data_->key_to_index_.find(key_h) != data_->key_to_index_.end());
265 }
266 
267 
268 
269 inline bool Selection::has_value(const int &val) const {
270  finished_check();
271  return (data_->value_to_index_.find(val) != data_->value_to_index_.end());
272 }
273 
274 
275 
276 inline unsigned int Selection::size() const {
277  finished_check();
278  ASSERT_EQ(data_->keys_.size(), data_->key_to_index_.size()).error();
279  return data_->keys_.size();
280 }
281 
282 
283 
284 
285 inline void Selection::finished_check() const {
286  ASSERT(data_->closed_)(this->type_name()).error();
287 }
288 
289 
290 
292 {
293  finished_check();
294  return data_->keys_.begin();
295 }
296 
297 
298 
300 {
301  finished_check();
302  return data_->keys_.end();
303 }
304 
305 
307 {
308  finished_check();
309  return begin() + name_to_int(key);
310 }
311 
312 
313 } // closing namespace Type
314 } // closing namespace Input
315 
316 #endif /* TYPE_SELECTION_HH_ */
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.
SelectionData(const string &name)
Constructor.
std::vector< Key > keys_
Vector of values of the Selection.
bool closed_
Indicator of closed Selection.
string int_to_name(const int &value) const
Returns value name for the given value.
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.
Base abstract class for output description of the Input::Type tree.
Definition: type_output.hh:58
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
bool has_name(const string &key) const
Just check if there is a particular name in the Selection.
Selection()
Default constructor. Empty selection.
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.
keys_const_iterator key_iterator(const string &key) const
Returns iterator to the key struct for given key string.
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.
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.
string class_name() const override
Override Type::TypeBase::class_name.
TypeBase::attribute_map attributes_
TYPEDEF_ERR_INFO(EI_Selection, const Selection)
keys_const_iterator end() const
Container-like access to the keys of the Selection.
TypeHash content_hash() const override
Implements TypeBase::content_hash.
std::string description_
Text description of the whole Selection object.
std::map< int, unsigned int > value_to_index_
Map : valid value to index.
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.
DECLARE_EXCEPTION(ExcSelectionKeyNotFound,<< "Key "<< EI_KeyName::qval<<" not found in Selection:\n"<< EI_Selection::val)
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).
std::map< KeyHash, unsigned int > key_to_index_
Map : valid value name to index.
std::vector< struct Key >::const_iterator keys_const_iterator
Public typedef of constant iterator into array of keys.
string type_name_
Name of the Selection.
keys_const_iterator begin() const
Container-like access to the keys of the Selection.
string key_list() const
Create string from values of keys.
FinishStatus finish_status_
Indicator of finished Selection.
std::size_t TypeHash
Type returned by content_hash methods.
Definition: type_base.hh:96
bool has_value(const int &val) const
Check if there is a particular value in the Selection.
MakeInstanceReturnType make_instance(std::vector< ParameterPair > vec=std::vector< ParameterPair >()) override
Template for classes storing finite set of named values.
#define ASSERT_EQ(a, b)
Definition of comparative assert macro (EQual)
Definition: asserts.hh:328