Serialbox  2.2.0
Data serialization library and tools for C/C++, Python and Fortran
FieldMap.h
Go to the documentation of this file.
1 //===-- serialbox/core/FieldMap.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_FIELDMAP_H
16 #define SERIALBOX_CORE_FIELDMAP_H
17 
19 #include "serialbox/core/Json.h"
20 #include <unordered_map>
21 
22 namespace serialbox {
23 
26 
28 class FieldMap {
29 public:
31  using map_type = std::unordered_map<std::string, std::shared_ptr<FieldMetainfoImpl>>;
32 
35  using value_type = map_type::value_type;
36 
38  using key_type = map_type::key_type;
39 
41  using mapped_type = map_type::mapped_type;
42 
44  using size_type = map_type::size_type;
45 
47  using iterator = map_type::iterator;
48 
50  using const_iterator = map_type::const_iterator;
51 
53  FieldMap() : map_(){};
54 
56  explicit FieldMap(const json::json jsonNode) { fromJSON(jsonNode); }
57 
59  FieldMap(const FieldMap&) = default;
60 
62  FieldMap(FieldMap&&) = default;
63 
65  FieldMap& operator=(const FieldMap&) = default;
66 
68  FieldMap& operator=(FieldMap&&) = default;
69 
74  template <class StringType>
75  bool hasField(StringType&& name) const noexcept {
76  return (map_.find(name) != map_.end());
77  }
78 
84  template <class StringType>
85  iterator findField(StringType&& name) noexcept {
86  return (map_.find(name));
87  }
88 
89  template <class StringType>
90  const_iterator findField(StringType&& name) const noexcept {
91  return (map_.find(name));
92  }
93 
98  template <class StringType>
100  return *getFieldMetainfoImplPtrOf(std::forward<StringType>(name));
101  }
102 
103  template <class StringType>
104  const FieldMetainfoImpl& getFieldMetainfoImplOf(StringType&& name) const {
105  return *getFieldMetainfoImplPtrOf(std::forward<StringType>(name));
106  }
107 
112  template <class StringType>
113  std::shared_ptr<FieldMetainfoImpl>& getFieldMetainfoImplPtrOf(StringType&& name) {
114  iterator fieldIt = map_.find(name);
115  if(fieldIt != map_.end())
116  return fieldIt->second;
117  throw Exception("field '%s' does not exist", name);
118  }
119 
120  template <class StringType>
121  const std::shared_ptr<FieldMetainfoImpl>& getFieldMetainfoImplPtrOf(StringType&& name) const {
122  const_iterator fieldIt = map_.find(name);
123  if(fieldIt != map_.end())
124  return fieldIt->second;
125  throw Exception("field '%s' does not exis", name);
126  }
127 
132  template <class StringType>
133  MetainfoMapImpl& getMetainfoOf(StringType&& name) {
134  return getFieldMetainfoImplOf(name).metaInfo();
135  }
136 
137  template <class StringType>
138  const MetainfoMapImpl& getMetainfoOf(StringType&& name) const {
139  return getFieldMetainfoImplOf(name).metaInfo();
140  }
141 
146  template <class StringType>
147  std::vector<int>& getDimsOf(StringType&& name) {
148  iterator fieldIt = map_.find(name);
149  if(fieldIt != map_.end())
150  return fieldIt->second->dims();
151  throw Exception("field '%s' does not exist in FieldMap", name);
152  }
153 
154  template <class StringType>
155  const std::vector<int>& getDimsOf(StringType&& name) const {
156  const_iterator fieldIt = map_.find(name);
157  if(fieldIt != map_.end())
158  return fieldIt->second->dims();
159  throw Exception("field '%s' does not exist in FieldMap", name);
160  }
161 
166  template <class StringType>
167  TypeID getTypeOf(StringType&& name) const {
168  const_iterator fieldIt = map_.find(name);
169  if(fieldIt != map_.end())
170  return fieldIt->second->type();
171  throw Exception("field '%s' does not exist in FieldMap", name);
172  }
173 
182  template <class StringType, typename... Args>
183  bool insert(StringType&& key, Args&&... args) {
184  return map_.insert({key, std::make_shared<FieldMetainfoImpl>(std::forward<Args>(args)...)})
185  .second;
186  }
187 
189  std::size_t size() const noexcept { return map_.size(); }
190 
192  bool empty() const noexcept { return map_.empty(); }
193 
196  void clear() noexcept { map_.clear(); }
197 
199  iterator begin() noexcept { return map_.begin(); }
200  const_iterator begin() const noexcept { return map_.begin(); }
201 
203  iterator end() noexcept { return map_.end(); }
204  const_iterator end() const noexcept { return map_.end(); }
205 
207  void swap(FieldMap& other) noexcept { map_.swap(other.map_); }
208 
210  bool operator==(const FieldMap& right) const noexcept { return (map_ == right.map_); }
211 
213  bool operator!=(const FieldMap& right) const noexcept { return (!(*this == right)); }
214 
216  friend std::ostream& operator<<(std::ostream& stream, const FieldMap& s);
217 
219  json::json toJSON() const;
220 
224  void fromJSON(const json::json& jsonNode);
225 
226 private:
227  map_type map_;
228 };
229 
231 
232 } // namespace serialbox
233 
234 #endif
bool hasField(StringType &&name) const noexcept
Check if field with name name exists in the FieldMap.
Definition: FieldMap.h:75
map_type::key_type key_type
Type of the key (std::string) i.e name of the fields.
Definition: FieldMap.h:38
std::unordered_map< std::string, std::shared_ptr< FieldMetainfoImpl > > map_type
Type of the underlying hash-map.
Definition: FieldMap.h:31
Meta-information of a data field.
std::shared_ptr< FieldMetainfoImpl > & getFieldMetainfoImplPtrOf(StringType &&name)
Get pointer to FieldMetainfoImpl of field name
Definition: FieldMap.h:113
FieldMetainfoImpl & getFieldMetainfoImplOf(StringType &&name)
Get FieldMetainfoImpl of field name
Definition: FieldMap.h:99
bool operator!=(const FieldMap &right) const noexcept
Test for inequality.
Definition: FieldMap.h:213
map_type::const_iterator const_iterator
A forward iterator to const value_type
Definition: FieldMap.h:50
iterator begin() noexcept
Returns an iterator pointing to the first element in the FieldMap.
Definition: FieldMap.h:199
bool empty() const noexcept
Returns a bool value indicating whether the FieldMap is empty.
Definition: FieldMap.h:192
Namespace of the serialbox library.
Definition: Archive.h:25
TypeID
Type-id of types recognized by serialbox.
Definition: Type.h:55
iterator findField(StringType &&name) noexcept
Search the FieldMap for a field with name name and returns an iterator to it if found, otherwise returns an iterator to FieldMap::end.
Definition: FieldMap.h:85
std::vector< int > & getDimsOf(StringType &&name)
Get Dimensions of field with name name
Definition: FieldMap.h:147
void fromJSON(const json::json &jsonNode)
Construct from JSON node.
Definition: FieldMap.cpp:31
map_type::value_type value_type
Type of an entry of the MetainfoMapImpl (std::pair<std::string, std::shared_ptr<FieldMetainfoImpl>>) ...
Definition: FieldMap.h:35
map_type::size_type size_type
An unsigned integral type (std::size_t)
Definition: FieldMap.h:44
std::size_t size() const noexcept
Returns the number of elements in the FieldMap.
Definition: FieldMap.h:189
Hash-map of meta-information of the form key = value pair or key = {value1, ..., valueN} ...
map_type::iterator iterator
A forward iterator to value_type
Definition: FieldMap.h:47
MetainfoMapImpl & metaInfo() noexcept
Access meta-info map.
bool insert(StringType &&key, Args &&... args)
Insert a new field in the map.
Definition: FieldMap.h:183
FieldMap & operator=(const FieldMap &)=default
Copy assignment.
iterator end() noexcept
Returns an iterator pointing to the past-the-end element in the FieldMap.
Definition: FieldMap.h:203
void clear() noexcept
All the elements in the FieldMap are dropped: their destructors are called, and they are removed from...
Definition: FieldMap.h:196
FieldMap(const json::json jsonNode)
Construct from json.
Definition: FieldMap.h:56
json::json toJSON() const
Convert to JSON.
Definition: FieldMap.cpp:19
MetainfoMapImpl & getMetainfoOf(StringType &&name)
Get Metainfo of field with name name
Definition: FieldMap.h:133
void swap(FieldMap &other) noexcept
Swap with other.
Definition: FieldMap.h:207
TypeID getTypeOf(StringType &&name) const
Get Type of field with name name
Definition: FieldMap.h:167
map_type::mapped_type mapped_type
Type of the value (std::shared_ptr<FieldMetainfoImpl>)
Definition: FieldMap.h:41
Exception class which stores a human-readable error description.
Definition: Exception.h:30
Hash-map to query the meta-information of the registered fields.
Definition: FieldMap.h:28
friend std::ostream & operator<<(std::ostream &stream, const FieldMap &s)
Convert to stream.
Definition: FieldMap.cpp:47
bool operator==(const FieldMap &right) const noexcept
Test for equality.
Definition: FieldMap.h:210
FieldMap()
Default constructor (empty table)
Definition: FieldMap.h:53