22 int idx = savepoints_.size();
23 if(index_.insert(
typename index_type::value_type{savepoint, idx}).second) {
24 savepoints_.push_back(std::make_shared<SavepointImpl>(savepoint));
32 int idx = find(savepoint);
34 return addField(idx, fieldID);
39 return fields_[idx].insert({fieldID.name, fieldID.id}).second;
43 int idx = find(savepoint);
45 return hasField(idx, field);
50 return (fields_[idx].find(field) != fields_[idx].end());
54 auto it = fields_[idx].find(field);
55 if(it != fields_[idx].
end())
56 return FieldID{it->first, it->second};
57 throw Exception(
"field '%s' does not exists at savepoint '%s'", field, savepoints_[idx]->name());
61 const std::string& field)
const {
62 int idx =
find(savepoint);
69 index_.swap(other.index_);
70 savepoints_.swap(other.savepoints_);
71 fields_.swap(other.fields_);
75 return (
find(savepoint) != -1);
79 auto it = index_.find(savepoint);
80 return ((it != index_.end()) ? it->second : -1);
90 auto it = index_.find(savepoint);
91 if(it != index_.end())
104 assert(savepoints_.size() == fields_.size());
106 for(std::size_t i = 0; i < savepoints_.size(); ++i)
107 jsonNode[
"savepoints"].push_back(savepoints_[i]->
toJSON());
109 for(std::size_t i = 0; i < fields_.size(); ++i) {
110 const std::string& savepoint = savepoints_[i]->name();
111 json::json fieldNode;
113 if(fields_[i].
empty())
114 fieldNode[savepoint] =
nullptr;
116 for(
auto it = fields_[i].
begin(),
end = fields_[i].
end(); it !=
end; ++it)
117 fieldNode[savepoint][it->first] = it->second;
119 jsonNode[
"fields_per_savepoint"].push_back(fieldNode);
130 if(jsonNode.is_null() || jsonNode.empty())
134 if(jsonNode.count(
"savepoints")) {
135 for(
auto it = jsonNode[
"savepoints"].
begin(),
end = jsonNode[
"savepoints"].end(); it !=
end;
143 if(jsonNode.count(
"fields_per_savepoint") &&
144 jsonNode[
"fields_per_savepoint"].size() != fields_.size())
145 throw Exception(
"inconsistent number of 'fields_per_savepoint' and 'savepoints'");
147 for(std::size_t i = 0; i < fields_.size(); ++i) {
148 const json::json& fieldNode = jsonNode[
"fields_per_savepoint"][i][savepoints_[i]->name()];
151 if(fieldNode.is_null() || fieldNode.empty())
155 for(
auto it = fieldNode.begin(),
end = fieldNode.end(); it !=
end; ++it)
156 fields_[i].
insert({it.key(),
static_cast<unsigned int>(it.value())});
161 stream <<
"SavepointVector = " << s.
toJSON().dump(4);
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
FieldID getFieldID(const SavepointImpl &savepoint, const std::string &field) const
Get the FielID of field field at savepoint savepoint
bool empty() const noexcept
Returns a bool value indicating whether the savepoint vector is empty.
std::string toString() const
Convert savepoint to string.
int find(const SavepointImpl &savepoint) const noexcept
Find savepoint.
const fields_per_savepoint_type & fieldsOf(const SavepointImpl &savepoint) const
Access fields of savepoint.
Namespace of the serialbox library.
Uniquely identifiy a field.
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.
The SavepointVector manages the registered savepoints and their mapping to the stored fields...
int insert(const SavepointImpl &savepoint) noexcept
Insert savepoint in savepoint 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.
Exception class which stores a human-readable error description.
iterator begin() noexcept
Returns an iterator pointing to the first savepoint in the vector.
Shared implementation of the Savepoint.
void clear() noexcept
All the elements Savepoints are dropped: their destructors are called, and they are removed from the ...