1 | // Aseprite |
2 | // Copyright (C) 2001-2017 David Capello |
3 | // |
4 | // This program is distributed under the terms of |
5 | // the End-User License Agreement for Aseprite. |
6 | |
7 | #ifdef HAVE_CONFIG_H |
8 | #include "config.h" |
9 | #endif |
10 | |
11 | #include "app/cmd.h" |
12 | #include "base/debug.h" |
13 | #include "base/mem_utils.h" |
14 | |
15 | #include <typeinfo> |
16 | |
17 | #define CMD_TRACE(...) |
18 | |
19 | namespace app { |
20 | |
21 | Cmd::Cmd() |
22 | #if _DEBUG |
23 | : m_state(State::NotExecuted) |
24 | #endif |
25 | { |
26 | } |
27 | |
28 | Cmd::~Cmd() |
29 | { |
30 | } |
31 | |
32 | void Cmd::execute(Context* ctx) |
33 | { |
34 | CMD_TRACE("CMD: Executing cmd '%s'\n" , typeid(*this).name()); |
35 | ASSERT(m_state == State::NotExecuted); |
36 | |
37 | m_ctx = ctx; |
38 | |
39 | onExecute(); |
40 | onFireNotifications(); |
41 | |
42 | #if _DEBUG |
43 | m_state = State::Executed; |
44 | #endif |
45 | } |
46 | |
47 | void Cmd::undo() |
48 | { |
49 | CMD_TRACE("CMD: Undo cmd '%s'\n" , typeid(*this).name()); |
50 | ASSERT(m_state == State::Executed || m_state == State::Redone); |
51 | |
52 | onUndo(); |
53 | onFireNotifications(); |
54 | |
55 | #if _DEBUG |
56 | m_state = State::Undone; |
57 | #endif |
58 | } |
59 | |
60 | void Cmd::redo() |
61 | { |
62 | CMD_TRACE("CMD: Redo cmd '%s'\n" , typeid(*this).name()); |
63 | ASSERT(m_state == State::Undone); |
64 | |
65 | onRedo(); |
66 | onFireNotifications(); |
67 | |
68 | #if _DEBUG |
69 | m_state = State::Redone; |
70 | #endif |
71 | } |
72 | |
73 | void Cmd::dispose() |
74 | { |
75 | CMD_TRACE("CMD: Deleting '%s' (%s)\n" , |
76 | typeid(*this).name(), |
77 | base::get_pretty_memory_size(memSize()).c_str()); |
78 | |
79 | delete this; |
80 | } |
81 | |
82 | std::string Cmd::label() const |
83 | { |
84 | return onLabel(); |
85 | } |
86 | |
87 | size_t Cmd::memSize() const |
88 | { |
89 | return onMemSize(); |
90 | } |
91 | |
92 | void Cmd::onExecute() |
93 | { |
94 | // Do nothing |
95 | } |
96 | |
97 | void Cmd::onUndo() |
98 | { |
99 | // Do nothing |
100 | } |
101 | |
102 | void Cmd::onRedo() |
103 | { |
104 | // By default onRedo() uses onExecute() implementation |
105 | onExecute(); |
106 | } |
107 | |
108 | void Cmd::onFireNotifications() |
109 | { |
110 | // Do nothing |
111 | } |
112 | |
113 | std::string Cmd::onLabel() const |
114 | { |
115 | return "" ; |
116 | } |
117 | |
118 | size_t Cmd::onMemSize() const { |
119 | return sizeof(*this); |
120 | } |
121 | |
122 | } // namespace app |
123 | |