-
Notifications
You must be signed in to change notification settings - Fork 39
/
GoalUndo.cpp
138 lines (127 loc) · 3.1 KB
/
GoalUndo.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include "GoalUndo.h"
#include <string>
#include <iostream>
/* undoGoal
When there are existing goals, it removes the most recently added
one, along with any associated operations
*/
void GoalUndo::undoGoal()
{
if( !goals.empty() )
{
goals.pop();
}
}
/* undoOperation
When there is an existing goal with more than one operation, remove the most
recently added operation. When there is only one operation in the most
recently added goal, it removes both the operation and the goal (i.e. goals
cannot exist without any operations in it).
*/
void GoalUndo::undoOperation()
{
if( !goals.empty() )
{
goals.top().operations.pop_back();
if( goals.top().operations.empty() )
{
undoGoal();
}
}
}
/* undoOperation(undoOp)
Overloaded undoOperation that searches the most recent goal (LIFO order)
for an operation that matches the argument and removes (only) the first
match found. If there are no matches, nothing is removed.
*/
void GoalUndo::undoOperation(std::string undoOp)
{
if( !goals.empty() )
{
std::vector<std::string>::iterator iter;
for(iter=goals.top().operations.end()-1;
iter >= goals.top().operations.begin();
iter-- )
{
if( iter->compare(undoOp) == 0 ) //match!
{
goals.top().operations.erase(iter);
break; //only remove first LIFO match
}
}
if( goals.top().operations.empty() )
{
undoGoal();
}
}
}
/* getGoal
Returns the name of the most recently added goal, or an empty string
if there are no goals.
*/
std::string GoalUndo::getGoal()
{
if( goals.empty() )
return "";
else
return goals.top().name;
}
/* getOperations
Returns the names of all of the operations in the most recently added goal,
with a space between each operation.
*/
std::string GoalUndo::getOperations()
{
if( goals.empty() )
return "";
else
{
std::string allOps = "";
std::vector <std::string> listOps = goals.top().operations;
std::vector<std::string>::iterator iter;
for( iter=goals.top().operations.begin();
iter < goals.top().operations.end();
iter++ )
{
allOps += *iter;
//add space between each operation (but not last)
if( iter < goals.top().operations.cend()-1 )
allOps += " ";
}
return allOps;
}
std::cout<<"Do nothing.\n";
}
/* addOperation(newGoal,newOp)
Adds a new operation (newOp) within a new goal (newGoal). Both have to be
non-empty strings. If either argument is an empty string, nothing is added.
*/
void GoalUndo::addOperation(std::string newGoal, std::string newOp)
{
if(!newGoal.empty() && !newOp.empty())
{
Goal latest;
latest.name = newGoal;
goals.push(latest);
addOperation( newOp );
}
}
/* addOperation(newOp)
Adds a new operation (newOp) within the most recently added goal. If the
argument is an empty string, nothing is added. If no goals exist, it
creates a new goal with the same name as the new operation
*/
void GoalUndo::addOperation(std::string newOp)
{
if(!newOp.empty())
{
if( goals.empty() )
{
addOperation( newOp, newOp );
}
else
{
goals.top().operations.push_back(newOp);
}
}
}