The Sparta Modeling Framework
Loading...
Searching...
No Matches
Checkpoint.hpp
1// <Checkpoint> -*- C++ -*-
2
3#pragma once
4
5#include "sparta/serialization/checkpoint/CheckpointBase.hpp"
6
7namespace sparta::serialization::checkpoint
8{
18 {
19 public:
20
24
26 Checkpoint() = delete;
27
29 Checkpoint(const Checkpoint&) = delete;
30
32 Checkpoint& operator=(const Checkpoint&) = delete;
33
36
39
40 protected:
41
46 tick_t tick,
47 Checkpoint* prev) :
48 CheckpointBase(id, tick),
49 prev_(prev)
50 { }
51
52 public:
53
58 virtual ~Checkpoint() {
59 if(getPrev()){
60 getPrev()->removeNext(this);
61 }
62
63 // Reconnect deltas from this checkpoint to the prev (even if nullptr)
64 for(auto& d : getNexts()){
65 d->setPrev(getPrev());
66 if(getPrev()){
67 getPrev()->addNext(d);
68 }
69 }
70 }
71
74
79 Checkpoint* getPrev() const noexcept {
80 return prev_;
81 }
82
89 void setPrev(Checkpoint* prev) noexcept {
90 prev_ = prev;
91 }
92
97 chkpt_id_t getPrevID() const override {
98 return prev_ ? prev_->getID() : UNIDENTIFIED_CHECKPOINT;
99 }
100
110 void addNext(Checkpoint* next) {
111 if(!next){
112 throw CheckpointError("Cannot specify nullptr in addNext");
113 }
114 if(next->getPrev() != this){
115 throw CheckpointError("Attempting to add a next checkpoint whose previous checkpoint pointer is not 'this'");
116 }
117 if(next->getTick() < getTick()){
118 throw CheckpointError("Attempting to add a next checkpoint whose tick number (")
119 << next->getTick() << " is less than this checkpoint's tick: " << getTick();
120 }
121 if(std::find(nexts_.begin(), nexts_.end(), next) != nexts_.end()){
122 throw CheckpointError("Next argument already present in this checkpoint's nexts_ list. Cannot re-add");
123 }
124 nexts_.push_back(next);
125 }
126
135 void removeNext(Checkpoint* next) {
136 if(!next){
137 throw CheckpointError("Cannot specify nullptr in removeNext");
138 }
139 if(next->getPrev() != this){
140 throw CheckpointError("Attempting to remove a next checkpoint whose previous pointer is not 'this'");
141 }
142 auto itr = std::find(nexts_.begin(), nexts_.end(), next);
143 if(itr == nexts_.end()){
144 throw CheckpointError("Next argument was not present in this checkpoint's nexts_ list. Cannot remove");
145 }
146 nexts_.erase(itr);
147 }
148
156 const std::vector<Checkpoint*>& getNexts() const noexcept { return nexts_; }
157
162 std::vector<chkpt_id_t> getNextIDs() const override {
163 std::vector<chkpt_id_t> next_ids;
164 for (const auto chkpt : getNexts()) {
165 next_ids.push_back(chkpt->getID());
166 }
167 return next_ids;
168 }
169
172
173 private:
174
180 std::vector<Checkpoint*> nexts_;
181
182 Checkpoint* prev_;
183 };
184
185} // namespace sparta::serialization::checkpoint
Single checkpoint object interface with a tick number and an ID unique to the owning Checkpointer ins...
uint64_t chkpt_id_t
tick_t Checkpoint ID type to which checkpoints will refer
chkpt_id_t getID() const noexcept
Returns the ID of this checkpoint.
tick_t getTick() const noexcept
Returns the tick number at which this checkpoint was taken.
static const chkpt_id_t UNIDENTIFIED_CHECKPOINT
Indicates unidentified checkpoint (could mean 'invalid' or 'any') depending on context.
sparta::Scheduler::Tick tick_t
tick_t Tick type to which checkpoints will refer
Indicates that there was an issue operating on checkpoints within the SPARTA framework.
Single checkpoint object interface with a tick number and an ID unique to the owning Checkpointer ins...
Checkpoint()=delete
Not default constructable.
Checkpoint * getPrev() const noexcept
Returns the previous checkpoint. If this checkpoint is a snapshot, it has no previous checkpoint.
Checkpoint(const Checkpoint &)=delete
Not copy constructable.
const std::vector< Checkpoint * > & getNexts() const noexcept
Returns next checkpoint following *this. May be an empty vector if there are no later checkpoints fol...
Checkpoint & operator=(Checkpoint &&)=delete
Not move assignable.
Checkpoint(Checkpoint &&)=delete
Not move constructable.
Checkpoint & operator=(const Checkpoint &)=delete
Non-assignable.
std::vector< chkpt_id_t > getNextIDs() const override
Returns next checkpoint following *this. May be an empty vector if there are no later checkpoints.
void addNext(Checkpoint *next)
Adds another next checkpoint following *this.
chkpt_id_t getPrevID() const override
Get the ID of our previous checkpoint. Returns UNIDENTIFIED_CHECKPOINT only for the head checkpoint.
void setPrev(Checkpoint *prev) noexcept
Sets the previous checkpoint of this checkpoint to prev.
Checkpoint(chkpt_id_t id, tick_t tick, Checkpoint *prev)
void removeNext(Checkpoint *next)
Removes a checkpoint following *this because it was deleted.
virtual ~Checkpoint()
Removes this checkpoint from the chain and patches chain between prev and each item in the nexts list...