-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActionStack.cpp
More file actions
145 lines (134 loc) · 3.79 KB
/
ActionStack.cpp
File metadata and controls
145 lines (134 loc) · 3.79 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* ActionStack.cpp
* CS 15 Project 2: typewriter
* Author: Adetoye Adebayo
* Date: 7th October, 2025
* Implements the ActionStack for the Typewriter by providing a simple LIFO
* stack of user Action records to support the undo/redo behavior
* of the typewriter.
*
*/
#include "ActionStack.h"
/*
name: ActionStack::ActionStack()
purpose: construct a new ActionStack for the Typewriter that starts empty
and is ready to record user actions for undo/redo
arguments: none
returns: none
effects: initializes an empty stack
note: none
*/
ActionStack::ActionStack() {
data = std::vector<Action>(); // Initialize the vector
}
/*
name: ActionStack::~ActionStack()
purpose: destroy an ActionStack when it goes out of scope or is deleted,
releasing any resources owned by the stack
arguments: none
returns: none
effects: none
note: none
*/
ActionStack::~ActionStack() {
// No dynamic memory to free, vector will clean up itself
}
/*
name: bool ActionStack::isEmpty() const
purpose: report whether the ActionStack currently contains zero actions
arguments: none
returns: true if the stack has no actions and false otherwise
effects: none
note: none
*/
bool ActionStack::isEmpty() const {
return data.empty();
}
/*
name: int ActionStack::size() const
purpose: provide the current number of actions stored in the ActionStack
arguments: none
returns: the count of actions currently on the stack
effects: none
note: none
*/
int ActionStack::size() const {
return int(data.size());
}
/*
name: ActionStack::Action ActionStack::top() const
purpose: access the most recently recorded action so clients
can inspect it for undo/redo purposes
arguments: none
returns: the action at the top of the stack
effects: none
note: throws std::runtime_error("empty_stack") if the stack is empty
*/
ActionStack::Action ActionStack::top() const {
if (data.empty()) {
throw std::runtime_error("empty_stack"); // throws if empty
}
return data.back();
}
/*
name: void ActionStack::pop()
purpose: remove the most recently recorded action from the stack
after it has been handled by the client
arguments: none
returns: none
effects: decreases the stack size by one and discards the previous top action
note: throws std::runtime_error("empty_stack") if the stack is empty
*/
void ActionStack::pop() {
if (data.empty()) {
throw std::runtime_error("empty_stack"); // throws if empty
}
data.pop_back();
}
/*
name: void ActionStack::push(Action elem)
purpose: add a new action to the top of the stack using an already
constructed Action record
arguments: elem — an Action describing a user edit.
returns: none
effects: increases the stack size by one and the provided Action is copied
onto the stack
note: none
*/
void ActionStack::push(Action elem) {
data.push_back(elem);
}
/*
name: void ActionStack::push(char c, bool was_delete, std::size_t line,
std::size_t column)
purpose: add a new action to the top of the stack by specifying its
fields directly
arguments: c — the character involved; was_delete — whether the action
deleted (true) or inserted (false) the character; line/column — the
location of the edit in the document
returns: none
effects: increases the stack size by one and constructs and stores an
Action with the given data
note: none
*/
void ActionStack::push(char c, bool was_delete, std::size_t line,
std::size_t column) {
Action newAction;
newAction.character = c;
newAction.deleted = was_delete;
newAction.line = line;
newAction.column = column;
data.push_back(newAction);
}
/*
name: void ActionStack::clear()
purpose: remove all actions from the stack to reset undo/redo history
arguments: none
returns: none
effects: reduces the stack size to zero and previously stored actions
become inaccessible
note: none
*/
void ActionStack::clear() {
data.clear();
}