]> git.mxchange.org Git - flightgear.git/blob - src/Time/FGEventMgr.cxx
Recover this file. We are not able to use boost as a replacement because
[flightgear.git] / src / Time / FGEventMgr.cxx
1 #ifdef HAVE_CONFIG_H
2 #  include <config.h>
3 #endif
4
5 #include <simgear/compiler.h>
6 #include <simgear/debug/logstream.hxx>
7 #include <functional>
8 #include <boost/bind.hpp>
9
10 #include "FGEventMgr.hxx"
11
12 FGEventMgr::FGEvent::FGEvent()
13     : status(FG_EVENT_SUSP),
14       interval_ms(0),
15       ms_to_go(0),
16       cum_time(0),
17       min_time(100000),
18       max_time(0),
19       count(0)
20 {
21 }
22
23 FGEventMgr::FGEvent::FGEvent( const string& desc,
24                               callback_type cb,
25                               EventState status_,
26                               interval_type ms )
27     : description(desc),
28       callback(cb),
29       status(status_),
30       interval_ms(ms),
31       ms_to_go(ms),
32       cum_time(0),
33       min_time(100000),
34       max_time(0),
35       count(0)
36 {
37 }
38
39
40 FGEventMgr::FGEvent::~FGEvent()
41 {
42 }
43
44 void
45 FGEventMgr::FGEvent::run()
46 {
47     SGTimeStamp start_time;
48     SGTimeStamp finish_time;
49
50     start_time.stamp();
51
52     // run the event
53     this->callback();
54
55     finish_time.stamp();
56
57     ++count;
58
59     unsigned long duration = finish_time - start_time;
60
61     cum_time += duration;
62
63     if ( duration < min_time ) {
64         min_time = duration;
65     }
66
67     if ( duration > max_time ) {
68         max_time = duration;
69     }
70
71     reset();
72 }
73
74 void
75 FGEventMgr::FGEvent::print_stats() const
76 {
77     SG_LOG( SG_EVENT, SG_INFO, 
78             "  " << description 
79             << " int=" << interval_ms / 1000.0
80             << " cum=" << cum_time
81             << " min=" << min_time
82             << " max=" <<  max_time
83             << " count=" << count
84             << " ave=" << cum_time / (double)count );
85 }
86
87 FGEventMgr::FGEventMgr()
88 {
89 }
90
91 FGEventMgr::~FGEventMgr()
92 {
93 }
94
95 void
96 FGEventMgr::init()
97 {
98     SG_LOG( SG_EVENT, SG_INFO, "Initializing event manager" );
99
100     event_table.clear();
101 }
102
103 void
104 FGEventMgr::bind()
105 {
106 }
107
108 void
109 FGEventMgr::unbind()
110 {
111 }
112
113 void
114 FGEventMgr::update( int dt )
115 {
116     // Scan all events.  Execute any whose interval has expired.
117     event_container_type::iterator first = event_table.begin();
118     event_container_type::iterator last = event_table.end();
119     for(; first != last; ++first)
120     {
121         if (first->update( dt ))
122         {
123             first->run();
124         }
125     }
126 }
127
128 void
129 FGEventMgr::Register( const string& desc,
130                       callback_type cb,
131                       EventState state,
132                       interval_type ms )
133 {
134     FGEvent event( desc, cb, state, ms );
135     if (state == FG_EVENT_READY)
136         event.run();
137     event_table.push_back( event );
138
139     SG_LOG( SG_EVENT, SG_INFO, "Registered event " << desc
140             << " to run every " << ms << "ms" );
141 }
142
143 void
144 FGEventMgr::print_stats() const
145 {
146     SG_LOG( SG_EVENT, SG_INFO, "" );
147     SG_LOG( SG_EVENT, SG_INFO, "Event Stats" );
148     SG_LOG( SG_EVENT, SG_INFO, "-----------" );
149
150     std::for_each( event_table.begin(), event_table.end(),
151                    boost::bind( &FGEvent::print_stats, _1 ) );
152
153     SG_LOG( SG_EVENT, SG_INFO, "" );
154 }