Serialbox  2.2.0
Data serialization library and tools for C/C++, Python and Fortran
MetainfoMap.h
Go to the documentation of this file.
1 //===-- serialbox/core/frontend/gridtools/MetainfoMapImpl.h -------------------------*- C++ -*-===//
2 //
3 // S E R I A L B O X
4 //
5 // This file is distributed under terms of BSD license.
6 // See LICENSE.txt for more information
7 //
8 //===------------------------------------------------------------------------------------------===//
9 //
13 //===------------------------------------------------------------------------------------------===//
14 
15 #ifndef SERIALBOX_CORE_FRONTEND_GRIDTOOLS_META_INFO_MAP_H
16 #define SERIALBOX_CORE_FRONTEND_GRIDTOOLS_META_INFO_MAP_H
17 
21 #include <memory>
22 
23 namespace serialbox {
24 
25 namespace gridtools {
26 
35 public:
38 
40  using value_type = map_type::value_type;
41 
43  using key_type = map_type::key_type;
44 
46  using mapped_type = map_type::mapped_type;
47 
49  using size_type = map_type::size_type;
50 
52  using iterator = map_type::iterator;
53 
55  using const_iterator = map_type::const_iterator;
56 
58  meta_info_map() : map_impl_(std::make_shared<MetainfoMapImpl>()) {}
59 
66  meta_info_map(std::initializer_list<value_type> list)
67  : map_impl_(std::make_shared<MetainfoMapImpl>(list)) {}
68 
86  meta_info_map(const meta_info_map&) = default;
87 
89  meta_info_map(meta_info_map&&) = default;
90 
108  meta_info_map& operator=(const meta_info_map&) = default;
109 
111  meta_info_map& operator=(meta_info_map&&) = default;
112 
119  meta_info_map& operator=(std::initializer_list<value_type> list) {
120  map_impl_ = std::make_shared<MetainfoMapImpl>(list);
121  return (*this);
122  }
123 
136  return meta_info_map(std::make_shared<MetainfoMapImpl>(*map_impl_));
137  }
138 
140  explicit meta_info_map(const std::shared_ptr<MetainfoMapImpl>& map) { map_impl_ = map; }
141 
147  template <class StringType>
148  bool has_key(StringType&& key) const noexcept {
149  return map_impl_->hasKey(std::forward<StringType>(key));
150  }
151 
160  std::vector<std::string> keys() const { return map_impl_->keys(); }
161 
169  std::vector<TypeID> types() const { return map_impl_->types(); }
170 
179  template <class StringType>
180  iterator find(StringType&& key) noexcept {
181  return map_impl_->find(std::forward<StringType>(key));
182  }
183  template <class StringType>
184  const_iterator find(StringType&& key) const noexcept {
185  return map_impl_->find(std::forward<StringType>(key));
186  }
188 
198  template <class KeyType, class ValueType>
199  bool insert(KeyType&& key, ValueType&& value) noexcept {
200  return map_impl_->insert(std::forward<KeyType>(key), std::forward<ValueType>(value));
201  }
202 
213  template <class ValueType, class KeyType>
214  ValueType as(KeyType&& key) const {
215  return map_impl_->at(std::forward<KeyType>(key)).template as<ValueType>();
216  }
217 
221  iterator erase(const_iterator position) { return map_impl_->erase(position); }
222  size_type erase(const key_type& key) { return map_impl_->erase(key); }
224  return map_impl_->erase(first, last);
225  }
227 
230  mapped_type& operator[](const key_type& key) noexcept { return map_impl_->operator[](key); }
231  mapped_type& operator[](key_type&& key) noexcept { return map_impl_->operator[](key); }
233 
238  mapped_type& at(const key_type& key) { return map_impl_->at(key); }
239  const mapped_type& at(const key_type& key) const { return map_impl_->at(key); }
241 
243  std::size_t size() const noexcept { return map_impl_->size(); }
244 
247  bool empty() const noexcept { return map_impl_->empty(); }
248 
251  void clear() noexcept { map_impl_->clear(); }
252 
254  iterator begin() noexcept { return map_impl_->begin(); }
255  const_iterator begin() const noexcept { return map_impl_->begin(); }
256 
258  iterator end() noexcept { return map_impl_->end(); }
259  const_iterator end() const noexcept { return map_impl_->end(); }
260 
262  void swap(meta_info_map& other) noexcept { map_impl_->swap(*other.map_impl_); }
263 
265  bool operator==(const meta_info_map& right) const noexcept {
266  return (*map_impl_ == *right.map_impl_);
267  }
268 
270  bool operator!=(const meta_info_map& right) const noexcept { return (!(*this == right)); }
271 
273  friend std::ostream& operator<<(std::ostream& stream, const meta_info_map& s) {
274  return (stream << *s.map_impl_);
275  }
276 
278  const std::shared_ptr<MetainfoMapImpl>& impl() const { return map_impl_; }
279 
280 private:
281  std::shared_ptr<MetainfoMapImpl> map_impl_;
282 };
283 
284 } // namespace gridtools
285 
286 } // namespace serialbox
287 
288 #endif
meta_info_map()
Default constructor (empty map)
Definition: MetainfoMap.h:58
meta_info_map(const std::shared_ptr< MetainfoMapImpl > &map)
Construct with MetainfoMapImpl (internal use)
Definition: MetainfoMap.h:140
const std::shared_ptr< MetainfoMapImpl > & impl() const
Get implementation pointer.
Definition: MetainfoMap.h:278
MetainfoMapImpl::map_type map_type
Type of the underlying hash-map.
Definition: MetainfoMap.h:37
bool operator==(const meta_info_map &right) const noexcept
Test for equality.
Definition: MetainfoMap.h:265
std::unordered_map< std::string, MetainfoValueImpl > map_type
Type of the underlying hash-map.
map_type::value_type value_type
Type of an entry of the MetainfoMapImpl (std::pair<std::string, meta_info_value>) ...
Definition: MetainfoMap.h:40
bool insert(KeyType &&key, ValueType &&value) noexcept
Insert a new element in the map.
Definition: MetainfoMap.h:199
std::vector< TypeID > types() const
Get vector of all available keys.
Definition: MetainfoMap.h:169
bool empty() const noexcept
Returns a bool value indicating whether the meta_info_map is empty, i.e. whether its size is 0...
Definition: MetainfoMap.h:247
Definition: json.hpp:10517
std::size_t size() const noexcept
Returns the number of elements in the meta_info_map.
Definition: MetainfoMap.h:243
map_type::size_type size_type
An unsigned integral type (std::size_t)
Definition: MetainfoMap.h:49
void swap(meta_info_map &other) noexcept
Swap with other.
Definition: MetainfoMap.h:262
bool has_key(StringType &&key) const noexcept
Check if key exists in the map.
Definition: MetainfoMap.h:148
Namespace of the serialbox library.
Definition: Archive.h:25
meta_info_map & operator=(std::initializer_list< value_type > list)
Construct from initalizer-list.
Definition: MetainfoMap.h:119
ValueType as(KeyType &&key) const
Convert value of element with key to type T
Definition: MetainfoMap.h:214
map_type::const_iterator const_iterator
A forward iterator to const value_type
Definition: MetainfoMap.h:55
Hash-map of meta-information of the form key = value pair or key = {value1, ..., valueN} ...
Definition: MetainfoMap.h:34
map_type::key_type key_type
Type of the key (std::string)
Definition: MetainfoMap.h:43
meta_info_map(std::initializer_list< value_type > list)
Construct from initalizer-list.
Definition: MetainfoMap.h:66
iterator find(StringType &&key) noexcept
Search the container for an element with a key equivalent to key and return an iterator to it if foun...
Definition: MetainfoMap.h:180
Namespace of the gridtools frontend.
Hash-map of meta-information of the form key = value pair or key = {value1, ..., valueN} ...
void clear() noexcept
All the elements in the meta_info_map are dropped: their destructors are called, and they are removed...
Definition: MetainfoMap.h:251
bool operator!=(const meta_info_map &right) const noexcept
Test for inequality.
Definition: MetainfoMap.h:270
iterator end() noexcept
Returns an iterator pointing to the past-the-end element in the meta_info_map.
Definition: MetainfoMap.h:258
iterator begin() noexcept
Returns an iterator pointing to the first element in the meta_info_map.
Definition: MetainfoMap.h:254
std::vector< std::string > keys() const
Get vector of strings of all available keys.
Definition: MetainfoMap.h:160
mapped_type & at(const key_type &key)
Return a reference to mapped value given by key.
Definition: MetainfoMap.h:238
meta_info_map & operator=(const meta_info_map &)=default
Copy assignment.
meta_info_map clone() const
Clone the current meta_info_map object by performing a deep copy.
Definition: MetainfoMap.h:135
friend std::ostream & operator<<(std::ostream &stream, const meta_info_map &s)
Convert to stream.
Definition: MetainfoMap.h:273
map_type::iterator iterator
A forward iterator to value_type
Definition: MetainfoMap.h:52
mapped_type & operator[](const key_type &key) noexcept
Return a reference to mapped value given by key.
Definition: MetainfoMap.h:230
map_type::mapped_type mapped_type
Type of the value (MetainfoMapImpl::Value)
Definition: MetainfoMap.h:46
iterator erase(const_iterator position)
Removes from the MetainfoMapImpl either a single element or a range of elements [first,last)
Definition: MetainfoMap.h:221