porymap/include/core/history.h

73 lines
1.2 KiB
C
Raw Permalink 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>
template <typename T>
class History {
public:
History() { }
2022-07-05 05:47:58 +01:00
~History() {
clear();
}
void clear() {
2022-07-05 05:47:58 +01:00
while (!history.isEmpty()) {
delete history.takeLast();
}
head = -1;
saved = -1;
2022-07-05 05:47:58 +01:00
}
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