All Classes Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
gears::chrono::basic_stopwatch< Clock > Struct Template Reference

A basic stopwatch for measuring time. More...

Public Types

using time_point = typename Clock::time_point
 
using duration = typename Clock::duration
 
using rep = typename Clock::rep
 
using period = typename Clock::period
 

Public Member Functions

void start ()
 Starts the stopwatch.
 
void stop ()
 Stops the stopwatch.
 
void reset ()
 Resets the stopwatch. More...
 
void restart ()
 Restarts the stopwatch. More...
 
template<typename Duration = std::chrono::milliseconds>
rep elapsed ()
 Returns the elapsed time. More...
 
bool is_running () const
 Checks if the stopwatch is running.
 

Detailed Description

template<typename Clock>
struct gears::chrono::basic_stopwatch< Clock >

Template Parameters
ClockThe internal clock to use to calculate time

basic_stopwatch is a class that defines the most basic stopwatch representation. It's underlying clock can be configured through its template parameter to give you the most flexibility desired.

For convenience, a typedef named stopwatch is provided that defaults to std::chrono::high_resolution_clock as the internal clock used.

Example usage:

#include <gears/chrono.hpp>
#include <thread>
#include <iostream>
namespace ch = gears::chrono;
void f() {
using namespace ch::literals;
std::this_thread::sleep_for(1.16_s);
}
int main() {
ch::stopwatch w;
w.start();
f();
w.stop();
std::cout << w.elapsed() << ' ' << w.elapsed<std::chrono::seconds>();
}

Possible Output:

1159 1

Definition at line 30 of file stopwatch.hpp.

Member Function Documentation

template<typename Clock >
template<typename Duration = std::chrono::milliseconds>
rep gears::chrono::basic_stopwatch< Clock >::elapsed ( )
inline

The elapsed time is considered the time since the stopwatch was started via the start member function. Consecutive calls to elapsed would not stop the watch and would simply return the elapsed time since the starting point.

Template Parameters
DurationThe duration to cast the elapsed time to. Defaults to std::chrono::milliseconds.
Returns
Elapsed time since the stopwatch started.

Definition at line 107 of file stopwatch.hpp.

template<typename Clock >
void gears::chrono::basic_stopwatch< Clock >::reset ( )
inline

Resets the stopwatch and pauses it. Meaning that it sets the elapsed time to zero and the post-condition of is_running is false.

See Also
restart

Definition at line 71 of file stopwatch.hpp.

template<typename Clock >
void gears::chrono::basic_stopwatch< Clock >::restart ( )
inline

Restarts the stopwatch, meaning that it sets the elapsed time to zero, and starts the stopwatch again. This is equivalent to doing the following:

my_stopwatch.reset();
my_stopwatch.start();
See Also
reset

Definition at line 89 of file stopwatch.hpp.