feisty meow concerns codebase  2.140
timer_driver.h
Go to the documentation of this file.
1 #ifndef TIMER_DRIVER_CLASS
2 #define TIMER_DRIVER_CLASS
3 
4 /*****************************************************************************\
5 * *
6 * Name : timer_driver *
7 * Author : Chris Koeritz *
8 * *
9 *******************************************************************************
10 * Copyright (c) 2003-$now By Author. This program is free software; you can *
11 * redistribute it and/or modify it under the terms of the GNU General Public *
12 * License as published by the Free Software Foundation; either version 2 of *
13 * the License or (at your option) any later version. This is online at: *
14 * http://www.fsf.org/copyleft/gpl.html *
15 * Please send any updates to: fred@gruntose.com *
16 \*****************************************************************************/
17 
18 #include <basis/astring.h>
19 #include <basis/definitions.h>
20 #include <basis/mutex.h>
21 
22 namespace timely {
23 
24 // forward.
25 class driven_objects_list;
26 class signalling_thread;
27 
29 
31 
32 class timeable : public virtual basis::root_object
33 {
34 public:
36  virtual void handle_timer_callback() = 0;
38 };
39 
41 
43 
50 class timer_driver : public virtual basis::root_object
51 {
52 public:
53  timer_driver();
54  virtual ~timer_driver();
55 
56  DEFINE_CLASS_NAME("timer_driver");
57 
58  // main methods for controlling timeables.
59 
60  bool set_timer(int duration, timeable *to_invoke);
62 
65  bool zap_timer(timeable *to_drop);
67 
70  // internal methods.
71 
72 /*
73 #if defined(_MSC_VER)
74  basis::un_int *real_timer_id();
76 #endif
77 */
78 
79  void handle_system_timer();
81 
84 
85 private:
86  driven_objects_list *_timers;
87  basis::mutex *_lock;
88 #if defined(__UNIX__) || defined(__GNU_WINDOWS__)
89  signalling_thread *_prompter;
90 #else
91  basis::un_int *_real_timer_id;
92 #endif
93  bool _in_timer;
94 
95  // these do the low-level system magic required to get a function hooked
96  // to a timer.
97  void hookup_OS_timer(int duration);
99  void reset_OS_timer(int next_hit);
101 
102  void unhook_OS_timer();
104 };
105 
107 
108 #define program_wide_timer() timer_driver::global_timer_driver()
110 
113 } //namespace.
114 
115 #endif
116 
timeable is the base for objects that can be hooked into timer events.
Definition: timer_driver.h:33
virtual void handle_timer_callback()=0
this method is invoked when the timer period elapses for this object.
Provides platform-independent timer support.
Definition: timer_driver.h:51
static timer_driver & global_timer_driver()
the first time this is invoked, it creates a program-wide timer driver.
bool set_timer(int duration, timeable *to_invoke)
sets a timer to call "to_invoke" every "duration" milliseconds.
DEFINE_CLASS_NAME("timer_driver")
void handle_system_timer()
invoked by the OS timer support and must be called by main thread.
bool zap_timer(timeable *to_drop)
removes the timer that was established for "to_drop".
Constants and objects used throughout HOOPLE.
unsigned int un_int
Abbreviated name for unsigned integers.
Definition: definitions.h:62
#include <time.h>
Definition: earth_time.cpp:37