1 /*
2  * Copyright (C) 2004 Ivo Danihelka (ivo@danihelka.net)
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  */
9 #include "CommandQueue.h"
10 
11 #include "Command.h"
12 
13 //-----------------------------------------------------------------
CommandQueue()14 CommandQueue::CommandQueue()
15 {
16     m_count = 0;
17 }
18 //-----------------------------------------------------------------
19 /**
20  * Remove all commands.
21  */
~CommandQueue()22 CommandQueue::~CommandQueue()
23 {
24     removeAll();
25 }
26 //-----------------------------------------------------------------
27 /**
28  * Add new command at the end of queue.
29  */
30 void
planCommand(Command * new_command)31 CommandQueue::planCommand(Command *new_command)
32 {
33     m_commands.push_back(new_command);
34 }
35 //-----------------------------------------------------------------
36 /**
37  * Execute first command.
38  * Execute none command when queue is empty.
39  * If the command returns true, remove him from queue.
40  * @return true when a command was executed
41  */
42 bool
executeFirst()43 CommandQueue::executeFirst()
44 {
45     bool result = false;
46     if (!m_commands.empty()) {
47         Command *command = m_commands.front();
48         if (command->finish(m_count)) {
49             m_commands.pop_front();
50             m_count = 0;
51             delete command;
52         }
53         else {
54             m_count++;
55         }
56         result = true;
57     }
58 
59     return result;
60 }
61 //-----------------------------------------------------------------
62 /**
63  * Remove all commands.
64  */
65 void
removeAll()66 CommandQueue::removeAll()
67 {
68     t_commands::iterator end = m_commands.end();
69     for (t_commands::iterator i = m_commands.begin(); i != end; ++i) {
70         delete (*i);
71     }
72     m_commands.clear();
73     m_count = 0;
74 }
75 
76