porymap/include/core/history.h

60 lines
1,023 B
C
Raw Normal View History

2020-09-13 23:37:55 +01:00
#pragma once
2018-09-24 16:48:36 +01:00
#ifndef HISTORY_H
#define HISTORY_H
#include <QList>
2021-02-18 00:20:14 +00:00
template <typename T> class History {
2018-09-24 16:48:36 +01:00
public:
2021-02-18 00:20:14 +00:00
History() {
}
2018-09-24 16:48:36 +01:00
T back() {
if (head > 0) {
return history.at(--head);
}
return NULL;
}
T next() {
if (head + 1 < history.length()) {
return history.at(++head);
}
return NULL;
}
void push(T commit) {
while (head + 1 < history.length()) {
T item = history.last();
history.removeLast();
delete item;
}
if (saved > head) {
saved = -1;
}
history.append(commit);
head++;
}
T current() {
if (head < 0 || history.length() == 0) {
return NULL;
}
return history.at(head);
}
void save() {
saved = head;
}
bool isSaved() {
return saved == head;
}
private:
QList<T> history;
int head = -1;
int saved = -1;
};
#endif // HISTORY_H