15 #ifndef SERIALBOX_CORE_FIELDMAP_H 16 #define SERIALBOX_CORE_FIELDMAP_H 20 #include <unordered_map> 31 using map_type = std::unordered_map<std::string, std::shared_ptr<FieldMetainfoImpl>>;
74 template <
class StringType>
75 bool hasField(StringType&& name)
const noexcept {
76 return (map_.find(name) != map_.end());
84 template <
class StringType>
86 return (map_.find(name));
89 template <
class StringType>
91 return (map_.find(name));
98 template <
class StringType>
103 template <
class StringType>
112 template <
class StringType>
115 if(fieldIt != map_.end())
116 return fieldIt->second;
117 throw Exception(
"field '%s' does not exist", name);
120 template <
class StringType>
123 if(fieldIt != map_.end())
124 return fieldIt->second;
125 throw Exception(
"field '%s' does not exis", name);
132 template <
class StringType>
137 template <
class StringType>
146 template <
class StringType>
149 if(fieldIt != map_.end())
150 return fieldIt->second->dims();
151 throw Exception(
"field '%s' does not exist in FieldMap", name);
154 template <
class StringType>
155 const std::vector<int>&
getDimsOf(StringType&& name)
const {
157 if(fieldIt != map_.end())
158 return fieldIt->second->dims();
159 throw Exception(
"field '%s' does not exist in FieldMap", name);
166 template <
class StringType>
169 if(fieldIt != map_.end())
170 return fieldIt->second->type();
171 throw Exception(
"field '%s' does not exist in FieldMap", name);
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)...)})
189 std::size_t
size() const noexcept {
return map_.size(); }
192 bool empty() const noexcept {
return map_.empty(); }
196 void clear() noexcept { map_.clear(); }
219 json::json
toJSON()
const;
224 void fromJSON(
const json::json& jsonNode);
bool hasField(StringType &&name) const noexcept
Check if field with name name exists in the FieldMap.
map_type::key_type key_type
Type of the key (std::string) i.e name of the fields.
std::unordered_map< std::string, std::shared_ptr< FieldMetainfoImpl > > map_type
Type of the underlying hash-map.
std::shared_ptr< FieldMetainfoImpl > & getFieldMetainfoImplPtrOf(StringType &&name)
Get pointer to FieldMetainfoImpl of field name
FieldMetainfoImpl & getFieldMetainfoImplOf(StringType &&name)
Get FieldMetainfoImpl of field name
bool operator!=(const FieldMap &right) const noexcept
Test for inequality.
map_type::const_iterator const_iterator
A forward iterator to const value_type
iterator begin() noexcept
Returns an iterator pointing to the first element in the FieldMap.
bool empty() const noexcept
Returns a bool value indicating whether the FieldMap is empty.
Namespace of the serialbox library.
TypeID
Type-id of types recognized by serialbox.
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.
std::vector< int > & getDimsOf(StringType &&name)
Get Dimensions of field with name name
void fromJSON(const json::json &jsonNode)
Construct from JSON node.
map_type::value_type value_type
Type of an entry of the MetainfoMapImpl (std::pair<std::string, std::shared_ptr<FieldMetainfoImpl>>) ...
map_type::size_type size_type
An unsigned integral type (std::size_t)
std::size_t size() const noexcept
Returns the number of elements in the FieldMap.
map_type::iterator iterator
A forward iterator to value_type
bool insert(StringType &&key, Args &&... args)
Insert a new field in the map.
FieldMap & operator=(const FieldMap &)=default
Copy assignment.
iterator end() noexcept
Returns an iterator pointing to the past-the-end element in the FieldMap.
void clear() noexcept
All the elements in the FieldMap are dropped: their destructors are called, and they are removed from...
FieldMap(const json::json jsonNode)
Construct from json.
json::json toJSON() const
Convert to JSON.
MetainfoMapImpl & getMetainfoOf(StringType &&name)
Get Metainfo of field with name name
void swap(FieldMap &other) noexcept
Swap with other.
TypeID getTypeOf(StringType &&name) const
Get Type of field with name name
map_type::mapped_type mapped_type
Type of the value (std::shared_ptr<FieldMetainfoImpl>)
Exception class which stores a human-readable error description.
Hash-map to query the meta-information of the registered fields.
friend std::ostream & operator<<(std::ostream &stream, const FieldMap &s)
Convert to stream.
bool operator==(const FieldMap &right) const noexcept
Test for equality.
FieldMap()
Default constructor (empty table)