From ba56dfc489f6a73f856479c05675cba71639b3ee Mon Sep 17 00:00:00 2001 From: Michael Ochmann Date: Mon, 6 Mar 2017 22:15:48 +0100 Subject: [PATCH] added history draft --- src/Editor.hpp | 2 +- src/History.cpp | 5 +++++ src/History.hpp | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 src/History.cpp create mode 100644 src/History.hpp diff --git a/src/Editor.hpp b/src/Editor.hpp index 301277c..606025a 100644 --- a/src/Editor.hpp +++ b/src/Editor.hpp @@ -60,7 +60,7 @@ namespace groove { return; } if (this->x >= this->buffer->linebuffer().at(this->y).length()) { - int length = this->buffer->linebuffer().at(this->y).length(); + int length = static_cast(this->buffer->linebuffer().at(this->y).length()); this->x = length > 0 ? length - 1 : 0; } this->scrollUp(); diff --git a/src/History.cpp b/src/History.cpp new file mode 100644 index 0000000..1f99d65 --- /dev/null +++ b/src/History.cpp @@ -0,0 +1,5 @@ +// +// Created by miko on 06.03.17. +// + +#include "History.hpp" diff --git a/src/History.hpp b/src/History.hpp new file mode 100644 index 0000000..f871609 --- /dev/null +++ b/src/History.hpp @@ -0,0 +1,42 @@ +#pragma once + +#include +#include + +namespace groove { + + struct HistoryNode { + std::shared_ptr last; + std::shared_ptr next; + std::function undo, redo; + HistoryNode(std::function undo, std::function redo, + std::shared_ptr last = nullptr, + std::shared_ptr next = nullptr) : + undo(undo), redo(redo), last(last), next(next) {} + }; + + class History { + private: + std::shared_ptr begin; + std::shared_ptr end; + public: + History() : begin(nullptr), end(nullptr) {} + void push(std::function undo, std::function redo) { + this->end->next = std::make_shared(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(); + } + }; + +} \ No newline at end of file