-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommandQueue.cpp
More file actions
115 lines (101 loc) · 2.74 KB
/
Copy pathcommandQueue.cpp
File metadata and controls
115 lines (101 loc) · 2.74 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
/**
* @file commandQueue.h
* @author Alex Lambert
*
* Description:
* -Stores a queue of commands, allowing them to be added or executed
* -Allows detection if the queue is empty using isEmpty
*
* Assumptions/Implementation:
* -Uses a linked list implementation to store the queue
* -When this queue is deleted, all nodes and commands are deleted with it
* -Can handle executing invalid inputs or conditions smoothly
*/
#include "commandQueue.h"
using namespace std;
//---------------------------------------------------------------------------
/** CommandQueue()
* Default Constructor
*
* Creates an empty CommandQueue
* @pre None
* @post front and back are set to nullptr
*/
CommandQueue::CommandQueue()
{
front = nullptr;
back = nullptr;
}
//---------------------------------------------------------------------------
/** ~CommandQueue()
* Destructor Destructor
*
* Destroys the Command Queue and all commands within it
* @pre None
* @post Deallocates all memory for the Command Queue,
* and all Linked List Nodes, and Library Command objects contained
*/
CommandQueue::~CommandQueue()
{
if (front)
delete front;
front = nullptr;
back = nullptr;
}
//---------------------------------------------------------------------------
/** enqueue()
* Enqueue Command
*
* Inserts the given command into the back of the queue
* @param command Command to be inserted into the queue
* @pre command isn't a nullptr
* @post Command is added to the back of the queue, if successful
* @return Returns true if command was successfully enqueued
*/
bool CommandQueue::enqueue(LibraryCommand* command)
{
if (!command)
return false;
LinkedListNode* newNode = new LinkedListNode(command);
if (!back) {
front = newNode;
back = newNode;
} else {
back->next = newNode;
back = newNode;
}
return true;
}
//---------------------------------------------------------------------------
/** executeNext()
* Execute Next Command
*
* Executes and deletes the next command in the queue
* @pre The Command Queue is not empty
* @post Executes and deletes the frontmost command
* @return Returns true if the command was successfully executed/removed
*/
bool CommandQueue::executeNext()
{
if (isEmpty())
return false;
front->execute();
LinkedListNode* nextNode = front->next;
front->next = nullptr;
delete front;
front = nextNode;
return true;
}
//---------------------------------------------------------------------------
/** isEmpty()
* Check if Empty
*
* Returns if the Command Queue is currently empty
* @pre None
* @post No changes made.
* @return Returns if the queue is empty
*/
bool CommandQueue::isEmpty() const
{
return !front;
}