Serialbox  2.2.0
Data serialization library and tools for C/C++, Python and Fortran
SavepointVector.h
Go to the documentation of this file.
1 //===-- serialbox/core/SavepointVector.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 //
14 //===------------------------------------------------------------------------------------------===//
15 
16 #ifndef SERIALBOX_CORE_SAVEPOINTVECTOR_H
17 #define SERIALBOX_CORE_SAVEPOINTVECTOR_H
18 
19 #include "serialbox/core/FieldID.h"
20 #include "serialbox/core/Json.h"
22 #include <iosfwd>
23 #include <unordered_map>
24 #include <vector>
25 
26 namespace serialbox {
27 
30 
36  using index_type = std::unordered_map<SavepointImpl, int>;
37 
38 public:
40  using savepoint_vector_type = std::vector<std::shared_ptr<SavepointImpl>>;
41 
43  using fields_per_savepoint_type = std::unordered_map<std::string, unsigned int>;
44 
46  using fields_per_savepoint_vector_type = std::vector<fields_per_savepoint_type>;
47 
49  using iterator = savepoint_vector_type::iterator;
50 
52  using const_iterator = savepoint_vector_type::const_iterator;
53 
55  SavepointVector() : index_(), savepoints_(), fields_(){};
56 
58  SavepointVector(const SavepointVector&) = default;
59 
61  SavepointVector(SavepointVector&&) = default;
62 
64  explicit SavepointVector(const json::json& jsonNode) { fromJSON(jsonNode); }
65 
67  SavepointVector& operator=(const SavepointVector&) = default;
68 
71 
75  bool exists(const SavepointImpl& savepoint) const noexcept;
76 
80  int find(const SavepointImpl& savepoint) const noexcept;
81 
85  int insert(const SavepointImpl& savepoint) noexcept;
86 
90  bool addField(const SavepointImpl& savepoint, const FieldID& fieldID) noexcept;
91 
95  bool addField(int idx, const FieldID& fieldID) noexcept;
96 
100  bool hasField(const SavepointImpl& savepoint, const std::string& field) noexcept;
101 
105  bool hasField(int idx, const std::string& field) noexcept;
106 
110  FieldID getFieldID(const SavepointImpl& savepoint, const std::string& field) const;
111 
115  FieldID getFieldID(int idx, const std::string& field) const;
116 
120  const fields_per_savepoint_type& fieldsOf(const SavepointImpl& savepoint) const;
121 
123  const fields_per_savepoint_type& fieldsOf(int idx) const noexcept;
124 
126  bool empty() const noexcept { return index_.empty(); }
127 
129  std::size_t size() const noexcept { return savepoints_.size(); }
130 
133  void clear() noexcept;
134 
136  void swap(SavepointVector& other) noexcept;
137 
139  iterator begin() noexcept { return savepoints_.begin(); }
140  const_iterator begin() const noexcept { return savepoints_.begin(); }
141 
143  iterator end() noexcept { return savepoints_.end(); }
144  const_iterator end() const noexcept { return savepoints_.end(); }
145 
147  SavepointImpl& operator[](int idx) noexcept { return *savepoints_[idx]; }
148  const SavepointImpl& operator[](int idx) const noexcept { return *savepoints_[idx]; }
149 
151  std::shared_ptr<SavepointImpl>& back() noexcept { return savepoints_.back(); }
152  const std::shared_ptr<SavepointImpl>& back() const noexcept { return savepoints_.back(); }
153 
155  const savepoint_vector_type& savepoints() const noexcept { return savepoints_; }
156  savepoint_vector_type& savepoints() noexcept { return savepoints_; }
157 
159  json::json toJSON() const;
160 
164  void fromJSON(const json::json& jsonNode);
165 
167  friend std::ostream& operator<<(std::ostream& stream, const SavepointVector& s);
168 
169 private:
170  index_type index_;
171  savepoint_vector_type savepoints_;
173 };
174 
176 
177 } // namespace serialbox
178 
179 #endif
std::vector< fields_per_savepoint_type > fields_per_savepoint_vector_type
Vector of map of fields per savepoint.
void fromJSON(const json::json &jsonNode)
Construct from JSON node.
bool hasField(const SavepointImpl &savepoint, const std::string &field) noexcept
Check if savepoint has field field
SavepointVector(const json::json &jsonNode)
Construct from JSON.
FieldID getFieldID(const SavepointImpl &savepoint, const std::string &field) const
Get the FielID of field field at savepoint savepoint
std::vector< std::shared_ptr< SavepointImpl > > savepoint_vector_type
Vector of savepoints.
bool empty() const noexcept
Returns a bool value indicating whether the savepoint vector is empty.
int find(const SavepointImpl &savepoint) const noexcept
Find savepoint.
SavepointVector()
Default constructor (empty)
SavepointVector & operator=(const SavepointVector &)=default
Copy assignment.
const fields_per_savepoint_type & fieldsOf(const SavepointImpl &savepoint) const
Access fields of savepoint.
Namespace of the serialbox library.
Definition: Archive.h:25
SavepointImpl & operator[](int idx) noexcept
Get savepoint.
Uniquely identifiy a field.
Definition: FieldID.h:27
const savepoint_vector_type & savepoints() const noexcept
Access the savepoints.
json::json toJSON() const
Convert to JSON.
std::unordered_map< std::string, unsigned int > fields_per_savepoint_type
Map of fields per savepoint.
void swap(SavepointVector &other) noexcept
Swap with other.
savepoint_vector_type::const_iterator const_iterator
A random access iterator to const std::shared_ptr<Savepoint>
The SavepointVector manages the registered savepoints and their mapping to the stored fields...
int insert(const SavepointImpl &savepoint) noexcept
Insert savepoint in savepoint vector.
std::shared_ptr< SavepointImpl > & back() noexcept
Returns a reference to the last element in the savepoint vector.
std::size_t size() const noexcept
Returns the number of savepoints in the vector.
bool exists(const SavepointImpl &savepoint) const noexcept
Check if savepoint exists.
iterator end() noexcept
Returns an iterator pointing to the past-the-end savepoint in the vector.
bool addField(const SavepointImpl &savepoint, const FieldID &fieldID) noexcept
Add a field to the savepoint.
friend std::ostream & operator<<(std::ostream &stream, const SavepointVector &s)
Convert to stream.
iterator begin() noexcept
Returns an iterator pointing to the first savepoint in the vector.
Shared implementation of the Savepoint.
Definition: SavepointImpl.h:38
void clear() noexcept
All the elements Savepoints are dropped: their destructors are called, and they are removed from the ...
savepoint_vector_type::iterator iterator
A random access iterator to std::shared_ptr<Savepoint>