]> git.mxchange.org Git - simgear.git/blob - simgear/structure/event_mgr.hxx
73cac934ddd7e96f6534f2e59b13424363185d37
[simgear.git] / simgear / structure / event_mgr.hxx
1 // eventmMgr.hxx -- periodic event scheduler
2 //
3 // Written by Curtis Olson, started December 1997.
4 // Modified by Bernie Bright, April 2002.
5 //
6 // Copyright (C) 1997  Curtis L. Olson  - curt@infoplane.com
7 //
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 // General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 //
22 // $Id$
23
24 #ifndef SG_EVENT_MGR_HXX
25 #define SG_EVENT_MGR_HXX 1
26
27 #include <simgear/compiler.h>
28
29 #include <simgear/structure/callback.hxx>
30 #include <simgear/structure/subsystem_mgr.hxx>
31 #include <simgear/timing/timestamp.hxx>
32
33 #include <vector>
34 #include <string>
35
36 SG_USING_STD(vector);
37 SG_USING_STD(string);
38
39
40 class SGEvent
41 {
42
43 public:
44
45     typedef int interval_type;
46
47 private:
48
49     string _name;
50     SGCallback* _callback;
51     SGSubsystem* _subsystem;
52     interval_type _repeat_value;
53     interval_type _initial_value;
54     int _ms_to_go;
55
56     unsigned long _cum_time;    // cumulative processor time of this event
57     unsigned long _min_time;    // time of quickest execution
58     unsigned long _max_time;    // time of slowest execution
59     unsigned long _count;       // number of times executed
60
61 public:
62
63     /**
64      * 
65      */
66     SGEvent();
67
68     SGEvent( const char* desc,
69              SGCallback* cb,
70              interval_type repeat_value,
71              interval_type initial_value );
72
73     SGEvent( const char* desc,
74              SGSubsystem* subsystem,
75              interval_type repeat_value,
76              interval_type initial_value );
77
78     /**
79      * 
80      */
81     ~SGEvent();
82
83     /**
84      * 
85      */
86     inline void reset()
87     {
88         _ms_to_go = _repeat_value;
89     }
90
91     /**
92      * Execute this event's callback.
93      */
94     void run();
95
96     inline string name() const { return _name; }
97     inline interval_type repeat_value() const { return _repeat_value; }
98     inline int value() const { return _ms_to_go; }
99
100     /**
101      * Display event statistics.
102      */
103     void print_stats() const;
104
105     /**
106      * Update the elapsed time for this event.
107      * @param dt_ms elapsed time in milliseconds.
108      * @return true if elapsed time has expired.
109      */
110     inline bool update( int dt_ms )
111     {
112         return (_ms_to_go -= dt_ms) <= 0;
113     }
114 };
115
116
117 class SGEventMgr : public SGSubsystem
118 {
119 private:
120
121     typedef SGEvent::interval_type interval_type;
122     typedef vector< SGEvent > event_container_type;
123
124     void add( const SGEvent& event );
125
126     // registered events.
127     event_container_type event_table;
128
129
130 public:
131     SGEventMgr();
132     ~SGEventMgr();
133
134     /**
135      * Initialize the scheduling subsystem.
136      */
137     void init();
138     void reinit();
139     void bind();
140     void unbind();
141
142     /*
143      * Update the elapsed time for all events.
144      * @param dt elapsed time in seconds.
145      */
146     void update( double dt );
147
148     /**
149      * register a free standing function to be executed some time in the future.
150      * @param desc A brief description of this callback for logging.
151      * @param cb The callback function to be executed.
152      * @param repeat_value repetition rate in milliseconds.
153      * @param initial_value initial delay value in milliseconds.  A value of
154      * -1 means run immediately.
155      */
156     template< typename Fun >
157     inline void add( const char* name,
158                      const Fun& f,
159                      interval_type repeat_value,
160                      interval_type initial_value = -1 )
161     {
162         this->add( SGEvent( name,
163                             make_callback(f),
164                             repeat_value,
165                             initial_value ) );
166     }
167
168     /**
169      * register a subsystem of which the update function will be executed some
170      * time in the future.
171      * @param desc A brief description of this callback for logging.
172      * @param subsystem The subsystem of which the update function will be
173      * executed.
174      * @param repeat_value repetition rate in milliseconds.
175      * @param initial_value initial delay value in milliseconds.  A value of
176      * -1 means run immediately.
177      */
178     inline void add( const char* name,
179                      SGSubsystem* subsystem,
180                      interval_type repeat_value,
181                      interval_type initial_value = -1 )
182     {
183         this->add( SGEvent( name,
184                             subsystem,
185                             repeat_value,
186                             initial_value ) );
187     }
188
189     template< class ObjPtr, typename MemFn >
190     inline void add( const char* name,
191                      const ObjPtr& p,
192                      MemFn pmf,
193                      interval_type repeat_value,
194                      interval_type initial_value = -1 )
195     {
196         this->add( SGEvent( name,
197                             make_callback(p,pmf),
198                             repeat_value,
199                             initial_value ) );
200     }
201
202     /**
203      * Display statistics for all registered events.
204      */
205     void print_stats() const;
206 };
207
208
209 #endif //SG_EVENT_MGR_HXX