1 // Undo Library 2 // Copyright (C) 2015-2017 David Capello 3 // 4 // This file is released under the terms of the MIT license. 5 // Read LICENSE.txt for more information. 6 7 #ifndef UNDO_STATE_H_INCLUDED 8 #define UNDO_STATE_H_INCLUDED 9 #pragma once 10 11 #include "undo_command.h" 12 13 namespace undo { 14 15 class UndoCommand; 16 class UndoHistory; 17 18 // Represents a state that can be undone. If we are in this state, 19 // is because the command was already executed. 20 class UndoState { 21 friend class UndoHistory; 22 public: UndoState(UndoCommand * cmd)23 UndoState(UndoCommand* cmd) 24 : m_prev(nullptr) 25 , m_next(nullptr) 26 , m_parent(nullptr) 27 , m_cmd(cmd) { 28 } 29 ~UndoState()30 ~UndoState() { 31 if (m_cmd) 32 m_cmd->dispose(); 33 34 #ifdef _DEBUG 35 m_prev = nullptr; 36 m_next = nullptr; 37 m_parent = nullptr; 38 m_cmd = nullptr; 39 #endif 40 } 41 prev()42 UndoState* prev() const { return m_prev; } next()43 UndoState* next() const { return m_next; } cmd()44 UndoCommand* cmd() const { return m_cmd; } 45 private: 46 UndoState* m_prev; 47 UndoState* m_next; 48 UndoState* m_parent; // Parent state, after we undo 49 UndoCommand* m_cmd; 50 }; 51 52 } // namespace undo 53 54 #endif // STATE_H_INCLUDED 55