forked from aseprite/aseprite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd.cpp
122 lines (94 loc) · 1.71 KB
/
cmd.cpp
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Aseprite
// Copyright (C) 2001-2017 David Capello
//
// This program is distributed under the terms of
// the End-User License Agreement for Aseprite.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "app/cmd.h"
#include "base/debug.h"
#include "base/mem_utils.h"
#include <typeinfo>
#define CMD_TRACE(...)
namespace app {
Cmd::Cmd()
#if _DEBUG
: m_state(State::NotExecuted)
#endif
{
}
Cmd::~Cmd()
{
}
void Cmd::execute(Context* ctx)
{
CMD_TRACE("CMD: Executing cmd '%s'\n", typeid(*this).name());
ASSERT(m_state == State::NotExecuted);
m_ctx = ctx;
onExecute();
onFireNotifications();
#if _DEBUG
m_state = State::Executed;
#endif
}
void Cmd::undo()
{
CMD_TRACE("CMD: Undo cmd '%s'\n", typeid(*this).name());
ASSERT(m_state == State::Executed || m_state == State::Redone);
onUndo();
onFireNotifications();
#if _DEBUG
m_state = State::Undone;
#endif
}
void Cmd::redo()
{
CMD_TRACE("CMD: Redo cmd '%s'\n", typeid(*this).name());
ASSERT(m_state == State::Undone);
onRedo();
onFireNotifications();
#if _DEBUG
m_state = State::Redone;
#endif
}
void Cmd::dispose()
{
CMD_TRACE("CMD: Deleting '%s' (%s)\n",
typeid(*this).name(),
base::get_pretty_memory_size(memSize()).c_str());
delete this;
}
std::string Cmd::label() const
{
return onLabel();
}
size_t Cmd::memSize() const
{
return onMemSize();
}
void Cmd::onExecute()
{
// Do nothing
}
void Cmd::onUndo()
{
// Do nothing
}
void Cmd::onRedo()
{
// By default onRedo() uses onExecute() implementation
onExecute();
}
void Cmd::onFireNotifications()
{
// Do nothing
}
std::string Cmd::onLabel() const
{
return "";
}
size_t Cmd::onMemSize() const {
return sizeof(*this);
}
} // namespace app