Public Types | Public Member Functions | Related Functions

TRTK::Clock Class Reference

A simple clock class. More...

#include <Clock.hpp>

List of all members.

Public Types

enum  Error { UNKNOWN_ERROR, UNKNOWN_STATE }

Public Member Functions

 Clock ()
 Constructs a Clock object.
double elapsed_time () const
 Returns the elapsed time in seconds.
void pause ()
 Pauses the time measurement.
void reset ()
 Resets the time measurement.
void resume ()
 Resumes the time measurement.
void wait_milliseconds (double time) const
 Waits for a defined period of time.
void wait_seconds (double time) const
 Waits for a defined period of time.

Related Functions

(Note that these are not member functions.)


std::ostream & operator<< (std::ostream &output, Clock &clock)
 Writes the elapsed time of a Clock object to the given stream.

Detailed Description

A simple clock class.

This class provides means for time measurement and some functionality for waiting for a defined period of time. The time measurement can be paused and resumed.

Please be aware of the fact, that the wait_seconds() and wait_milliseconds() functions do not idle, i.e., they might be computationally expensive!

Here is an example which shows the Clock class in action:

 #include <iostream>
 #include <TRTK/Clock.hpp>

 using namespace TRTK;
 using std::cout;

 int main()
 {
     Clock clock;

     clock.wait_seconds(1);
     cout << clock;

     clock.wait_seconds(2.123);
     cout << clock;

     clock.reset();
     clock.wait_milliseconds(60);
     cout << clock;

     clock.reset();
     cout << clock;

     Clock().wait_seconds(0.5);
     cout << clock;

     clock.pause();
     Clock().wait_seconds(0.5);
     cout << clock;

     clock.resume();
     Clock().wait_seconds(0.5);
     cout << clock;

     return 0;
 }

This is, what the output might look like:

 Elapsed time: 1 seconds.
 Elapsed time: 3.124 seconds.
 Elapsed time: 0.06 seconds.
 Elapsed time: 0 seconds.
 Elapsed time: 0.501 seconds.
 Elapsed time: 0.501 seconds.
 Elapsed time: 1.001 seconds.
Author:
Christoph Haenisch
Version:
0.1.3
Date:
last changed on 2013-08-20

Definition at line 102 of file Clock.hpp.


Member Function Documentation

double TRTK::Clock::elapsed_time (  ) const

Returns the elapsed time in seconds.

The elapsed time is the time interval between the point in time of the creation (and resetting, respectively) of this object and the point in time of calling this function.

Note:
If the pause() function is called, the time measurement pauses until the resume() function is called.
Returns:
Elapsed time in seconds.
Exceptions:
ErrorObjIn case of an unexpected error, an error object is thrown and its error code is set to UNKNOWN_STATE.
See also:
reset(), pause(), and resume()

Definition at line 55 of file Clock.cpp.

void TRTK::Clock::pause (  )

Pauses the time measurement.

If called, this function pauses the time measurement. The time measurement can be continued by calling the resume() function.

See also:
reset() and resume()

Definition at line 85 of file Clock.cpp.

void TRTK::Clock::reset (  )

Resets the time measurement.

See also:
elapsed_time(), pause(), and resume()

Definition at line 106 of file Clock.cpp.

void TRTK::Clock::resume (  )

Resumes the time measurement.

If called, this function resumes the time measurement. The time measurement can be stopped by calling the pause() function.

See also:
pause() and reset()

Definition at line 121 of file Clock.cpp.

void TRTK::Clock::wait_milliseconds ( double  time ) const

Waits for a defined period of time.

Parameters:
[in]timetime in milliseconds

This function waits for time milliseconds. This is done by continuously checking if the period of time has already elapsed. Thus, be aware, that this function might be computationally expensive!

Note:
Depending on the underlying operating system, the function might be inaccurate!
See also:
wait_seconds()

Definition at line 151 of file Clock.cpp.

void TRTK::Clock::wait_seconds ( double  time ) const

Waits for a defined period of time.

Parameters:
[in]timetime in seconds

This function waits for time seconds. This is done by continuously checking if the period of time has already elapsed. Thus, be aware, that this function might be computationally expensive!

Note:
Depending on the underlying operating system, the function might be inaccurate!
See also:
wait_seconds()

Definition at line 176 of file Clock.cpp.


Friends And Related Function Documentation

std::ostream & operator<< ( std::ostream &  output,
Clock clock 
) [related]

Writes the elapsed time of a Clock object to the given stream.

Here is an example of how to use the stream operator:

 Clock clock;
 // ...
 std::cout << clock;

Output:

 Elapsed time: 1.32 seconds.

Definition at line 209 of file Clock.cpp.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines