Serialbox  2.2.0
Data serialization library and tools for C/C++, Python and Fortran
MetainfoMapImpl.h
Go to the documentation of this file.
1 //===-- serialbox/core/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_METAINFOMAPIMPL_H
16 #define SERIALBOX_CORE_METAINFOMAPIMPL_H
17 
19 #include "serialbox/core/Json.h"
21 #include "serialbox/core/Type.h"
22 #include <iosfwd>
23 #include <string>
24 #include <unordered_map>
25 #include <utility>
26 #include <vector>
27 
28 namespace serialbox {
29 
32 
39 public:
41  using map_type = std::unordered_map<std::string, MetainfoValueImpl>;
42 
44  using value_type = map_type::value_type;
45 
47  using key_type = map_type::key_type;
48 
50  using mapped_type = map_type::mapped_type;
51 
53  using size_type = map_type::size_type;
54 
56  using iterator = map_type::iterator;
57 
59  using const_iterator = map_type::const_iterator;
60 
62  MetainfoMapImpl() : map_(){};
63 
65  explicit MetainfoMapImpl(const json::json& jsonNode) { fromJSON(jsonNode); }
66 
68  explicit MetainfoMapImpl(std::initializer_list<value_type> list) : map_(list){};
69 
71  MetainfoMapImpl(const MetainfoMapImpl&) = default;
72 
74  MetainfoMapImpl(MetainfoMapImpl&&) = default;
75 
77  MetainfoMapImpl& operator=(const MetainfoMapImpl&) = default;
78 
81 
87  template <class StringType>
88  bool hasKey(StringType&& key) const noexcept {
89  return (map_.find(key) != map_.end());
90  }
91 
97  std::vector<std::string> keys() const;
98 
104  std::vector<TypeID> types() const;
105 
113  template <class StringType>
114  iterator find(StringType&& key) noexcept {
115  return map_.find(key);
116  }
117  template <class StringType>
118  const_iterator find(StringType&& key) const noexcept {
119  return map_.find(key);
120  }
121 
131  template <class KeyType, class ValueType>
132  bool insert(KeyType&& key, ValueType&& value) noexcept {
133  return (map_.insert({key, MetainfoValueImpl(std::forward<ValueType>(value))}).second);
134  }
135 
148  template <class ValueType, class KeyType>
149  ValueType as(KeyType&& key) const {
150  return at(key).template as<ValueType>();
151  }
152 
155  iterator erase(const_iterator position) { return map_.erase(position); }
156  size_type erase(const key_type& key) { return map_.erase(key); }
157  iterator erase(const_iterator first, const_iterator last) { return map_.erase(first, last); }
158 
162  mapped_type& operator[](const key_type& key) noexcept { return map_[key]; }
163  mapped_type& operator[](key_type&& key) noexcept { return map_[key]; }
164 
168  mapped_type& at(const key_type& key);
169  const mapped_type& at(const key_type& key) const;
170 
172  std::size_t size() const noexcept { return map_.size(); }
173 
176  bool empty() const noexcept { return map_.empty(); }
177 
180  void clear() noexcept { map_.clear(); }
181 
183  iterator begin() noexcept { return map_.begin(); }
184  const_iterator begin() const noexcept { return map_.begin(); }
185 
187  iterator end() noexcept { return map_.end(); }
188  const_iterator end() const noexcept { return map_.end(); }
189 
191  void swap(MetainfoMapImpl& other) noexcept { map_.swap(other.map_); }
192 
194  bool operator==(const MetainfoMapImpl& right) const noexcept { return (map_ == right.map_); }
195 
197  bool operator!=(const MetainfoMapImpl& right) const noexcept { return (!(*this == right)); }
198 
200  json::json toJSON() const;
201 
205  void fromJSON(const json::json& jsonNode);
206 
208  friend std::ostream& operator<<(std::ostream& stream, const MetainfoMapImpl& s);
209 
210 private:
211  map_type map_;
212 };
213 
215 
216 } // namespace serialbox
217 
218 #endif
void clear() noexcept
All the elements in the MetainfoMapImpl are dropped: their destructors are called, and they are removed from the container, leaving it with a size of 0.
std::unordered_map< std::string, MetainfoValueImpl > map_type
Type of the underlying hash-map.
map_type::mapped_type mapped_type
Type of the value (MetainfoMapImpl::Value)
bool insert(KeyType &&key, ValueType &&value) noexcept
Insert a new element in the map.
map_type::value_type value_type
Type of an entry of the MetainfoMapImpl (std::pair<std::string, MetainfoValueImpl>) ...
json::json toJSON() const
Convert to JSON.
bool hasKey(StringType &&key) const noexcept
Check if key exists in the map.
MetainfoMapImpl & operator=(const MetainfoMapImpl &)=default
Copy assignment.
std::size_t size() const noexcept
Returns the number of elements in the MetainfoMapImpl.
void fromJSON(const json::json &jsonNode)
Construct from JSON node.
MetainfoMapImpl(const json::json &jsonNode)
Construct from json.
std::vector< TypeID > types() const
Get vector of all available keys.
Namespace of the serialbox library.
Definition: Archive.h:25
friend std::ostream & operator<<(std::ostream &stream, const MetainfoMapImpl &s)
Convert to stream.
map_type::key_type key_type
Type of the key (std::string)
mapped_type & operator[](const key_type &key) noexcept
Return a reference to mapped value given by key.
mapped_type & at(const key_type &key)
Return a reference to mapped value given by key.
std::vector< std::string > keys() const
Get vector of strings of all available keys.
iterator begin() noexcept
Returns an iterator pointing to the first element in the MetainfoMapImpl.
bool operator!=(const MetainfoMapImpl &right) const noexcept
Test for inequality.
Hash-map of meta-information of the form key = value pair or key = {value1, ..., valueN} ...
MetainfoMapImpl(std::initializer_list< value_type > list)
Construct from initalizer-list.
bool empty() const noexcept
Returns a bool value indicating whether the MetainfoMapImpl is empty, i.e. whether its size is 0...
ValueType as(KeyType &&key) const
Convert value of element with key key to type T
map_type::size_type size_type
An unsigned integral type (std::size_t)
iterator erase(const_iterator position)
Removes from the MetainfoMapImpl either a single element or a range of elements [first,last)
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...
bool operator==(const MetainfoMapImpl &right) const noexcept
Test for equality.
iterator end() noexcept
Returns an iterator pointing to the past-the-end element in the MetainfoMapImpl.
void swap(MetainfoMapImpl &other) noexcept
Swap with other.
map_type::const_iterator const_iterator
A forward iterator to const value_type
MetainfoMapImpl()
Default constructor (empty map)
map_type::iterator iterator
A forward iterator to value_type