Open Lighting Architecture  Latest Git
MultiCallback.h
Go to the documentation of this file.
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  * MultiCallback.h
17  * A callback which can be executed multiple times. When a pre-defined limit
18  * is reached, then the underlying callback is executed.
19  * Copyright (C) 2011 Simon Newton
20  */
21 
50 #ifndef INCLUDE_OLA_MULTICALLBACK_H_
51 #define INCLUDE_OLA_MULTICALLBACK_H_
52 
53 #include <ola/Callback.h>
54 
55 namespace ola {
56 
71 class MultiCallback: public BaseCallback0<void> {
72  public:
79  MultiCallback(unsigned int limit,
80  BaseCallback0<void> *callback)
81  : m_count(0),
82  m_limit(limit),
83  m_callback(callback) {
84  if (!limit) {
85  callback->Run();
86  delete this;
87  }
88  }
89 
94  void Run() {
95  if (++m_count == m_limit) {
96  m_callback->Run();
97  delete this;
98  }
99  }
100 
101  private:
102  unsigned int m_count;
103  unsigned int m_limit;
104  BaseCallback0<void> *m_callback;
105 };
106 
114  unsigned int limit,
115  BaseCallback0<void> *callback) {
116  return new MultiCallback(limit, callback);
117 }
119 } // namespace ola
120 #endif // INCLUDE_OLA_MULTICALLBACK_H_
MultiCallback(unsigned int limit, BaseCallback0< void > *callback)
Constructor.
Definition: MultiCallback.h:79
The base class for all 0 argument callbacks.
Definition: Callback.h:119
The MultiCallback class takes a limit & a callback. When the Run() method is called limit times...
Definition: MultiCallback.h:71
BaseCallback0< void > * NewMultiCallback(unsigned int limit, BaseCallback0< void > *callback)
A helper function to create a new MultiCallback.
Definition: MultiCallback.h:113
void Run()
Executes the callback passed in during creation after limit calls. Then MultiCallback deletes itself...
Definition: MultiCallback.h:94
The namespace containing all OLA symbols.
Definition: Credentials.cpp:44