summaryrefslogtreecommitdiff
path: root/TAO/tests/Xt_Stopwatch/timer.cpp
blob: ce807616e3e5569eb718e48d56b8ddc7c4e5f9d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//$Id$
#include "timer.h"


Timer_imp::Timer_imp (XtAppContext &app,
                      CORBA::Long interval,
                      Stopwatch_display *stopwatch)
  :stopwatch_ (stopwatch),
   counter_ (0),
   interval_ (interval),
   id_ (0),
   app_ (app)
{
}

void
Timer_imp::start (void)
{
  // Reset the elapsed time
  this->counter_ = 0;

  // If a previous callback is still in effect, remove it
  if (this->id_)
    {
      XtRemoveTimeOut (this->id_);
      this->id_ = 0;
    }

  // Register a function to be called in interval_ milliseconds
  this->id_ = XtAppAddTimeOut (this->app_,
                               this->interval_,
                               &Timer_imp::tick_callback,
                               (XtPointer) this );
}

void
Timer_imp::stop (void)
{
    // Remove the current timeout function, if any
  if (this->id_)
    XtRemoveTimeOut (this->id_);

    this->id_ = 0;
}

CORBA::Float
Timer_imp::elapsed_time(void)
{
    return ((CORBA::Float) counter_ * interval_ / 1000.0 );
}

void
Timer_imp::tick_callback (XtPointer client_data,
                          XtIntervalId * )
{
  // Get the object pointer and call the corresponding tick function
  Timer_imp *obj = static_cast<Timer_imp *> (client_data);
  obj->tick ();
}


void
Timer_imp::tick (void)
{
  // Increment a counter for each tick
  counter_++;

  // Call derived class function to report time
  this->report_time (this->elapsed_time ());

  // Reinstall the timeout callback
  this->id_ = XtAppAddTimeOut (app_,
                               interval_,
                               &Timer_imp::tick_callback,
                               (XtPointer) this);
}

void
Timer_imp::report_time (CORBA::Float time)
{
  stopwatch_->set_time (time);
}