Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / TAO / tests / Xt_Stopwatch / timer.cpp
blob35b54df65f46020c1e6be128acf50c488e2331a2
1 #include "timer.h"
3 Timer_imp::Timer_imp (XtAppContext &app,
4 CORBA::Long interval,
5 Stopwatch_display *stopwatch)
6 :stopwatch_ (stopwatch),
7 counter_ (0),
8 interval_ (interval),
9 id_ (0),
10 app_ (app)
14 Timer_imp::~Timer_imp ()
18 void
19 Timer_imp::start ()
21 // Reset the elapsed time
22 this->counter_ = 0;
24 // If a previous callback is still in effect, remove it
25 if (this->id_)
27 XtRemoveTimeOut (this->id_);
28 this->id_ = 0;
31 // Register a function to be called in interval_ milliseconds
32 this->id_ = XtAppAddTimeOut (this->app_,
33 this->interval_,
34 &Timer_imp::tick_callback,
35 (XtPointer) this);
38 void
39 Timer_imp::stop ()
41 // Remove the current timeout function, if any
42 if (this->id_)
43 XtRemoveTimeOut (this->id_);
45 this->id_ = 0;
48 CORBA::Float
49 Timer_imp::elapsed_time()
51 return ((CORBA::Float) counter_ * interval_ / 1000.0);
54 void
55 Timer_imp::tick_callback (XtPointer client_data,
56 XtIntervalId * )
58 // Get the object pointer and call the corresponding tick function
59 Timer_imp *obj = static_cast<Timer_imp *> (client_data);
60 obj->tick ();
63 void
64 Timer_imp::tick ()
66 // Increment a counter for each tick
67 counter_++;
69 // Call derived class function to report time
70 this->report_time (this->elapsed_time ());
72 // Reinstall the timeout callback
73 this->id_ = XtAppAddTimeOut (app_,
74 interval_,
75 &Timer_imp::tick_callback,
76 (XtPointer) this);
79 void
80 Timer_imp::report_time (CORBA::Float time)
82 stopwatch_->set_time (time);