source: molecuilder/src/Actions/ActionHistory.cpp@ 7ac765

Last change on this file since 7ac765 was 90c4460, checked in by Tillmann Crueger <crueger@…>, 15 years ago

Added a small memory tracker to the programm.

  • Property mode set to 100644
File size: 2.9 KB
Line 
1/*
2 * ActionHistory.cpp
3 *
4 * Created on: Mar 25, 2010
5 * Author: crueger
6 */
7
8#include "ActionHistory.hpp"
9
10#include <iostream>
11
12#include "Patterns/Singleton_impl.hpp"
13#include "Helpers/Assert.hpp"
14#include "Helpers/MemDebug.hpp"
15
16using namespace std;
17
18ActionHistory::ActionHistory()
19{}
20
21ActionHistory::~ActionHistory()
22{}
23
24void ActionHistory::undoLast(){
25 ASSERT(history.size(),"Undo performed when the undo-queue was empty");
26 HistoryElement elem = history.back();
27 history.pop_back();
28 Action::state_ptr newState = elem.action->undo(elem.state);
29 yrotsih.push_back(HistoryElement(elem.action,newState));
30}
31void ActionHistory::redoLast(){
32 ASSERT(yrotsih.size(),"Redo performed when the redo-queue was empty");
33 HistoryElement elem = yrotsih.back();
34 yrotsih.pop_back();
35 Action::state_ptr oldState = elem.action->redo(elem.state);
36 history.push_back(HistoryElement(elem.action,oldState));
37}
38
39bool ActionHistory::hasUndo(){
40 return history.size()>0;
41}
42
43bool ActionHistory::hasRedo(){
44 return yrotsih.size()>0;
45}
46
47void ActionHistory::addElement(Action* action,Action::state_ptr state){
48 yrotsih.clear();
49 history.push_back(HistoryElement(action,state));
50}
51
52void ActionHistory::clear(){
53 history.clear();
54 yrotsih.clear();
55}
56
57void ActionHistory::init(){
58 ActionHistory *hist = new ActionHistory();
59 setInstance(hist);
60 new UndoAction(hist);
61 new RedoAction(hist);
62}
63
64CONSTRUCT_SINGLETON(ActionHistory)
65
66/****************** Contained actions *******************/
67ActionHistory::UndoAction::UndoAction(ActionHistory *_hist) :
68 Action("Undo"),
69 hist(_hist)
70{}
71
72ActionHistory::UndoAction::~UndoAction(){}
73
74bool ActionHistory::UndoAction::canUndo(){
75 return false;
76}
77
78bool ActionHistory::UndoAction::shouldUndo(){
79 return false;
80}
81
82bool ActionHistory::UndoAction::isActive(){
83 return hist->hasUndo();
84}
85
86Action::state_ptr ActionHistory::UndoAction::performCall(){
87 hist->undoLast();
88 return Action::success;
89}
90
91Action::state_ptr ActionHistory::UndoAction::performUndo(Action::state_ptr){
92 ASSERT(0,"Cannot undo an undo (should use redo for this");
93 return Action::success;
94}
95
96Action::state_ptr ActionHistory::UndoAction::performRedo(Action::state_ptr){
97 ASSERT(0,"Cannot redo an undo");
98 return Action::success;
99}
100
101ActionHistory::RedoAction::RedoAction(ActionHistory *_hist) :
102 Action("Redo"),
103 hist(_hist)
104{}
105
106ActionHistory::RedoAction::~RedoAction(){}
107
108bool ActionHistory::RedoAction::canUndo(){
109 return false;
110}
111
112bool ActionHistory::RedoAction::shouldUndo(){
113 return false;
114}
115
116bool ActionHistory::RedoAction::isActive(){
117 return hist->hasRedo();
118}
119
120Action::state_ptr ActionHistory::RedoAction::performCall(){
121 hist->redoLast();
122 return Action::success;
123}
124
125Action::state_ptr ActionHistory::RedoAction::performUndo(Action::state_ptr){
126 ASSERT(0,"Cannot undo a redo (should use undo for this");
127 return Action::success;
128}
129
130Action::state_ptr ActionHistory::RedoAction::performRedo(Action::state_ptr){
131 ASSERT(0,"Cannot redo a redo");
132 return Action::success;
133}
Note: See TracBrowser for help on using the repository browser.