Tempo
Tempo is an app that allows users to create and manage timers, stopwatches, and alarms.
Public Member Functions | Private Attributes
Stopwatch Class Reference

The Stopwatch class provides a simple timer with start, pause, resume, and reset functionalities. More...

#include <Stopwatch.hpp>

Collaboration diagram for Stopwatch:
Collaboration graph
[legend]

Public Member Functions

 Stopwatch ()
 Default constructor initializes a Stopwatch with 0 milliseconds and not running. More...
 
 Stopwatch (int hours, int minutes, int seconds)
 Constructor initializes a Stopwatch with a specified time (in hours, minutes, and seconds). More...
 
void start ()
 Starts the stopwatch. More...
 
void addSplit ()
 Records and prints the current split time in milliseconds. More...
 
void clearSplits ()
 Clears the splits vector. More...
 
vector< int > getSplits ()
 Returns the splits vector. More...
 
void pause ()
 Pauses the stopwatch. More...
 
void resume ()
 Resumes the stopwatch from the last paused time. More...
 
void reset ()
 Resets the stopwatch to 0 milliseconds and stops it. More...
 
bool isRunning ()
 Checks if the stopwatch is currently running. More...
 
int currentMilliseconds ()
 Returns the current elapsed time in milliseconds. More...
 

Private Attributes

int _currMilliseconds
 The current elapsed time in milliseconds. More...
 
bool _running
 Indicates whether the stopwatch is currently running. More...
 
vector< int > _splits
 Vector of all splits for the stopwatch. More...
 
chrono::time_point< chrono::steady_clock > _startTime
 The time point when the stopwatch was started. More...
 
chrono::time_point< chrono::steady_clock > _pauseTime
 The time point when the stopwatch was paused. More...
 

Detailed Description

The Stopwatch class provides a simple timer with start, pause, resume, and reset functionalities.

Definition at line 13 of file Stopwatch.hpp.

Constructor & Destructor Documentation

◆ Stopwatch() [1/2]

Stopwatch::Stopwatch ( )

Default constructor initializes a Stopwatch with 0 milliseconds and not running.

Definition at line 12 of file Stopwatch.cpp.

12  {
13  _running = false;
15  _splits = {};
16 }
int _currMilliseconds
The current elapsed time in milliseconds.
Definition: Stopwatch.hpp:91
vector< int > _splits
Vector of all splits for the stopwatch.
Definition: Stopwatch.hpp:94
bool _running
Indicates whether the stopwatch is currently running.
Definition: Stopwatch.hpp:92

◆ Stopwatch() [2/2]

Stopwatch::Stopwatch ( int  hours,
int  minutes,
int  seconds 
)

Constructor initializes a Stopwatch with a specified time (in hours, minutes, and seconds).

Parameters
hoursThe number of hours to set.
minutesThe number of minutes to set.
secondsThe number of seconds to set.

Definition at line 25 of file Stopwatch.cpp.

25  {
26  int milli = 3600000 * hours + 60000 * minutes + 1000 * seconds;
27  _running = false;
28  _currMilliseconds = milli;
29  start();
30 }
int _currMilliseconds
The current elapsed time in milliseconds.
Definition: Stopwatch.hpp:91
void start()
Starts the stopwatch.
Definition: Stopwatch.cpp:103
bool _running
Indicates whether the stopwatch is currently running.
Definition: Stopwatch.hpp:92

Member Function Documentation

◆ addSplit()

void Stopwatch::addSplit ( )

Records and prints the current split time in milliseconds.

A split time is a snapshot of the time elapsed, which can be useful for time intervals without stopping the stopwatch.

Definition at line 64 of file Stopwatch.cpp.

Referenced by Menu::checkStopwatchInput().

64  {
65  int splitMilliseconds = 0;
66  if (_running) {
67  splitMilliseconds = chrono::duration_cast<chrono::milliseconds>(chrono::steady_clock::now() - _startTime).count();
68  splitMilliseconds += _currMilliseconds;
69  //cout << "Split time: " << _currMilliseconds + splitMilliseconds << " milliseconds" << endl;
70  }
71  else {
72  splitMilliseconds = _currMilliseconds;
73 
74  //cout << "Split time: " << _currMilliseconds << " milliseconds" << endl;
75  }
76  _splits.push_back(splitMilliseconds);
77 }
int _currMilliseconds
The current elapsed time in milliseconds.
Definition: Stopwatch.hpp:91
chrono::time_point< chrono::steady_clock > _startTime
The time point when the stopwatch was started.
Definition: Stopwatch.hpp:96
vector< int > _splits
Vector of all splits for the stopwatch.
Definition: Stopwatch.hpp:94
bool _running
Indicates whether the stopwatch is currently running.
Definition: Stopwatch.hpp:92
Here is the caller graph for this function:

◆ clearSplits()

void Stopwatch::clearSplits ( )

Clears the splits vector.

Definition at line 82 of file Stopwatch.cpp.

82  {
83  _splits = {};
84 }
vector< int > _splits
Vector of all splits for the stopwatch.
Definition: Stopwatch.hpp:94

◆ currentMilliseconds()

int Stopwatch::currentMilliseconds ( )

Returns the current elapsed time in milliseconds.

If the stopwatch is running, this includes the time since it was started; otherwise, it returns the last recorded time.

Returns
int The current elapsed time in milliseconds.

Definition at line 44 of file Stopwatch.cpp.

Referenced by Menu::checkStopwatchInput(), and Display::tickStopwatch().

44  {
45  if(_running){
46  auto now = chrono::steady_clock::now();
47  return _currMilliseconds + chrono::duration_cast<chrono::milliseconds>(now - _startTime).count();
48  }
49  else{
50  return _currMilliseconds;
51  }
52 }
int _currMilliseconds
The current elapsed time in milliseconds.
Definition: Stopwatch.hpp:91
chrono::time_point< chrono::steady_clock > _startTime
The time point when the stopwatch was started.
Definition: Stopwatch.hpp:96
bool _running
Indicates whether the stopwatch is currently running.
Definition: Stopwatch.hpp:92
Here is the caller graph for this function:

◆ getSplits()

vector< int > Stopwatch::getSplits ( )

Returns the splits vector.

Returns
vector<int> _splits, the splits vector.
vector<int> the vector containing split times in milliseconds.

Definition at line 90 of file Stopwatch.cpp.

Referenced by Display::tickStopwatch().

90  {
91  return _splits;
92 }
vector< int > _splits
Vector of all splits for the stopwatch.
Definition: Stopwatch.hpp:94
Here is the caller graph for this function:

◆ isRunning()

bool Stopwatch::isRunning ( )

Checks if the stopwatch is currently running.

Returns
bool True if the stopwatch is running, false otherwise.

Definition at line 163 of file Stopwatch.cpp.

Referenced by Menu::checkStopwatchInput(), and Display::tickStopwatch().

163  {
164  return _running;
165 }
bool _running
Indicates whether the stopwatch is currently running.
Definition: Stopwatch.hpp:92
Here is the caller graph for this function:

◆ pause()

void Stopwatch::pause ( )

Pauses the stopwatch.

Stops the time count until resumed. The current time is recorded.

Definition at line 119 of file Stopwatch.cpp.

Referenced by Menu::checkStopwatchInput().

119  {
120  if (_running) {
121  _pauseTime = chrono::steady_clock::now();
122  _currMilliseconds += chrono::duration_cast<chrono::milliseconds>(_pauseTime - _startTime).count();
123  _running = false;
124  }
125 }
chrono::time_point< chrono::steady_clock > _pauseTime
The time point when the stopwatch was paused.
Definition: Stopwatch.hpp:97
int _currMilliseconds
The current elapsed time in milliseconds.
Definition: Stopwatch.hpp:91
chrono::time_point< chrono::steady_clock > _startTime
The time point when the stopwatch was started.
Definition: Stopwatch.hpp:96
bool _running
Indicates whether the stopwatch is currently running.
Definition: Stopwatch.hpp:92
Here is the caller graph for this function:

◆ reset()

void Stopwatch::reset ( )

Resets the stopwatch to 0 milliseconds and stops it.

Definition at line 148 of file Stopwatch.cpp.

Referenced by Menu::checkStopwatchInput().

148  {
149  _running = false;
150  clearSplits();
151  _currMilliseconds = 0;
152 }
void clearSplits()
Clears the splits vector.
Definition: Stopwatch.cpp:82
int _currMilliseconds
The current elapsed time in milliseconds.
Definition: Stopwatch.hpp:91
bool _running
Indicates whether the stopwatch is currently running.
Definition: Stopwatch.hpp:92
Here is the caller graph for this function:

◆ resume()

void Stopwatch::resume ( )

Resumes the stopwatch from the last paused time.

Definition at line 134 of file Stopwatch.cpp.

Referenced by Menu::checkStopwatchInput().

134  {
135  if (!_running) {
136  _startTime = chrono::steady_clock::now();
137  _running = true;
138  }
139 }
chrono::time_point< chrono::steady_clock > _startTime
The time point when the stopwatch was started.
Definition: Stopwatch.hpp:96
bool _running
Indicates whether the stopwatch is currently running.
Definition: Stopwatch.hpp:92
Here is the caller graph for this function:

◆ start()

void Stopwatch::start ( )

Starts the stopwatch.

If the stopwatch is already running, this function does nothing.

Definition at line 103 of file Stopwatch.cpp.

103  {
104  if (!_running) {
105  _running = true;
106  _startTime = chrono::steady_clock::now();
107  }
108 }
chrono::time_point< chrono::steady_clock > _startTime
The time point when the stopwatch was started.
Definition: Stopwatch.hpp:96
bool _running
Indicates whether the stopwatch is currently running.
Definition: Stopwatch.hpp:92

Field Documentation

◆ _currMilliseconds

int Stopwatch::_currMilliseconds
private

The current elapsed time in milliseconds.

Definition at line 91 of file Stopwatch.hpp.

◆ _pauseTime

chrono::time_point<chrono::steady_clock> Stopwatch::_pauseTime
private

The time point when the stopwatch was paused.

Definition at line 97 of file Stopwatch.hpp.

◆ _running

bool Stopwatch::_running
private

Indicates whether the stopwatch is currently running.

Definition at line 92 of file Stopwatch.hpp.

◆ _splits

vector<int> Stopwatch::_splits
private

Vector of all splits for the stopwatch.

Definition at line 94 of file Stopwatch.hpp.

◆ _startTime

chrono::time_point<chrono::steady_clock> Stopwatch::_startTime
private

The time point when the stopwatch was started.

Definition at line 96 of file Stopwatch.hpp.


The documentation for this class was generated from the following files: