Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / Xt_Stopwatch / timer.cpp
blob06050a3169a359834232c565fb59e40b5ba8c649
1 #include "timer.h"
4 Timer_imp::Timer_imp (XtAppContext &app,
5 CORBA::Long interval,
6 Stopwatch_display *stopwatch)
7 :stopwatch_ (stopwatch),
8 counter_ (0),
9 interval_ (interval),
10 id_ (0),
11 app_ (app)
15 Timer_imp::~Timer_imp ()
19 void
20 Timer_imp::start (void)
22 // Reset the elapsed time
23 this->counter_ = 0;
25 // If a previous callback is still in effect, remove it
26 if (this->id_)
28 XtRemoveTimeOut (this->id_);
29 this->id_ = 0;
32 // Register a function to be called in interval_ milliseconds
33 this->id_ = XtAppAddTimeOut (this->app_,
34 this->interval_,
35 &Timer_imp::tick_callback,
36 (XtPointer) this );
39 void
40 Timer_imp::stop (void)
42 // Remove the current timeout function, if any
43 if (this->id_)
44 XtRemoveTimeOut (this->id_);
46 this->id_ = 0;
49 CORBA::Float
50 Timer_imp::elapsed_time(void)
52 return ((CORBA::Float) counter_ * interval_ / 1000.0 );
55 void
56 Timer_imp::tick_callback (XtPointer client_data,
57 XtIntervalId * )
59 // Get the object pointer and call the corresponding tick function
60 Timer_imp *obj = static_cast<Timer_imp *> (client_data);
61 obj->tick ();
65 void
66 Timer_imp::tick (void)
68 // Increment a counter for each tick
69 counter_++;
71 // Call derived class function to report time
72 this->report_time (this->elapsed_time ());
74 // Reinstall the timeout callback
75 this->id_ = XtAppAddTimeOut (app_,
76 interval_,
77 &Timer_imp::tick_callback,
78 (XtPointer) this);
81 void
82 Timer_imp::report_time (CORBA::Float time)
84 stopwatch_->set_time (time);