-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommandstack.cpp
More file actions
46 lines (37 loc) · 949 Bytes
/
commandstack.cpp
File metadata and controls
46 lines (37 loc) · 949 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include "commandstack.h"
#include "icommand.h"
CommandStack::~CommandStack()
{
for(auto iter = m_cmdVector.begin(); iter != m_cmdVector.end(); ++iter)
{
delete *iter;
}
m_cmdVector.clear();
}
void CommandStack::push(ICommand *cmd)
{
if(!m_cmdVector.empty() && m_currentIndex != (unsigned)m_cmdVector.size() - 1)
{
unsigned currentPos = m_currentIndex;
m_currentIndex = m_cmdVector.size() - 1;
do
{
delete m_cmdVector[m_currentIndex--];
m_cmdVector.pop_back();
}
while(m_currentIndex != currentPos);
}
m_cmdVector.push_back(cmd);
m_currentIndex++;
cmd->redo();
}
void CommandStack::redo()
{
if(m_cmdVector.empty() || m_currentIndex == (unsigned)m_cmdVector.size() - 1) return;
m_cmdVector[++m_currentIndex]->redo();
}
void CommandStack::undo()
{
if(m_cmdVector.empty() || m_currentIndex < 0) return;
m_cmdVector[m_currentIndex--]->undo();
}