parent
baeb89fdb9
commit
ba56dfc489
3 changed files with 48 additions and 1 deletions
@ -0,0 +1,5 @@ |
||||
//
|
||||
// Created by miko on 06.03.17.
|
||||
//
|
||||
|
||||
#include "History.hpp" |
@ -0,0 +1,42 @@ |
||||
#pragma once |
||||
|
||||
#include <functional> |
||||
#include <memory> |
||||
|
||||
namespace groove { |
||||
|
||||
struct HistoryNode { |
||||
std::shared_ptr<HistoryNode> last; |
||||
std::shared_ptr<HistoryNode> next; |
||||
std::function<void()> undo, redo; |
||||
HistoryNode(std::function<void()> undo, std::function<void()> redo, |
||||
std::shared_ptr<HistoryNode> last = nullptr, |
||||
std::shared_ptr<HistoryNode> next = nullptr) : |
||||
undo(undo), redo(redo), last(last), next(next) {} |
||||
}; |
||||
|
||||
class History { |
||||
private: |
||||
std::shared_ptr<HistoryNode> begin; |
||||
std::shared_ptr<HistoryNode> end; |
||||
public: |
||||
History() : begin(nullptr), end(nullptr) {} |
||||
void push(std::function<void()> undo, std::function<void()> redo) { |
||||
this->end->next = std::make_shared<HistoryNode>(undo, redo, this->end); |
||||
} |
||||
|
||||
void pop() { |
||||
this->end = this->end->last; |
||||
} |
||||
|
||||
void undo() { |
||||
this->end->undo(); |
||||
this->end = this->end->last; |
||||
} |
||||
|
||||
void redo() { |
||||
this->end->redo(); |
||||
} |
||||
}; |
||||
|
||||
} |
Loading…
Reference in new issue