Open Lighting Architecture  0.9.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ActionQueue.h
1 /*
2  * This library is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU Lesser General Public
4  * License as published by the Free Software Foundation; either
5  * version 2.1 of the License, or (at your option) any later version.
6  *
7  * This library is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10  * Lesser General Public License for more details.
11  *
12  * You should have received a copy of the GNU Lesser General Public
13  * License along with this library; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15  *
16  * ActionQueue.h
17  * Interface for the ActionQueue class.
18  * Copyright (C) 2005 Simon Newton
19  *
20  * An ActionQueue allows Actions to be queued up and the executed
21  * asynchronously. Perform(on_done) is called on each action in turn and an
22  * action calls on_done when it's completed.
23  */
24 
25 #ifndef INCLUDE_OLA_ACTIONQUEUE_H_
26 #define INCLUDE_OLA_ACTIONQUEUE_H_
27 
28 #include <ola/Callback.h>
29 #include <vector>
30 
31 namespace ola {
32 
33 /*
34  * The base action class
35  */
36 class Action {
37  public:
38  Action() {}
39  virtual ~Action() {}
40 
41  // returns true if the failure of this action causes aborts the entire
42  // chain.
43  virtual bool IsFatal() const = 0;
44  virtual bool Failed() const = 0;
45 
46  virtual void Perform(SingleUseCallback0<void> *on_done) = 0;
47 };
48 
49 
50 /*
51  * An Action Queue. Action objects can be added to the queue and are then run
52  * sequentially.
53  */
54 class ActionQueue {
55  public:
57  m_on_complete(on_complete),
58  m_action_index(-1),
59  m_success(true) {
60  }
61  ~ActionQueue();
62 
63  void AddAction(Action *action);
64  void NextAction();
65  bool WasSuccessful() const { return m_success; }
66 
67  unsigned int ActionCount() const { return m_actions.size(); }
68  Action *GetAction(unsigned int i);
69 
70  private:
72  std::vector<Action*> m_actions;
73  int m_action_index;
74  bool m_success;
75 };
76 } // namespace ola
77 #endif // INCLUDE_OLA_ACTIONQUEUE_H_