luna-sysmgr-common
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Timer.h
Go to the documentation of this file.
1 /* @@@LICENSE
2 *
3 * Copyright (c) 2008-2012 Hewlett-Packard Development Company, L.P.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 * LICENSE@@@ */
30 #ifndef TIMER_H
31 #define TIMER_H
32 
33 #include "Common.h"
34 
35 #include <glib.h>
36 #include <stdint.h>
37 
38 class SingletonTimer;
39 struct TimerHandle;
40 
52 class TimerBase {
53 public:
63  TimerBase(SingletonTimer* masterTimer);
64 
76  virtual ~TimerBase();
77 
87  void start(guint intervalInMs, bool singleShot=false);
88 
95  void stop();
96 
109  bool running() const;
110 
111 private:
127  static void callback(void* arg);
128 
140  virtual bool timeout() = 0;
141 
145  SingletonTimer* m_master;
146 
150  TimerHandle* m_handle;
151 
155  bool m_singleShot;
156 
160  uint64_t m_interval;
161 };
162 
171 template <class Target>
172 class Timer : public TimerBase
173 {
174 public:
180  typedef bool (Target::*TimeoutFunction)();
181 
195  Timer(SingletonTimer* masterTimer, Target* target, TimeoutFunction function)
196  : TimerBase(masterTimer)
197  , m_target(target)
198  , m_function(function) {}
199 
200 private:
211  virtual bool timeout() { return (m_target->*m_function)(); }
212 
216  Target* m_target;
217 
221  TimeoutFunction m_function;
222 };
223 
224 #endif /* TIMER_H */