Added documentation and move functions around for better readability

This commit is contained in:
2019-02-07 23:57:21 -05:00
parent 5f7351bf6e
commit c0670a5e03

View File

@@ -1,10 +1,10 @@
//MyClasses/Stopwatch.hpp //MyClasses/Stopwatch.hpp
//Matthew Ellison //Matthew Ellison
// Created: 10-30-2018 // Created: 10-30-2018
//Modified: 12-10-2018 //Modified: 02-07-2019
//This file creates a class that can be used on many programs to time them //This file defines a class that can be used as a simple timer for programs
/* /*
Copyright (C) 2018 Matthew Ellison Copyright (C) 2019 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -20,82 +20,107 @@
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#ifndef STOPWATCH_HPP #ifndef STOPWATCH_HPP
#define STOPWATCH_HPP #define STOPWATCH_HPP
#include <chrono> #include <chrono>
namespace mee{ namespace mee{
class Stopwatch{ class Stopwatch{
private: private:
std::chrono::high_resolution_clock::time_point startTime; std::chrono::high_resolution_clock::time_point startTime; //The time the start function was called
std::chrono::high_resolution_clock::time_point endTime; std::chrono::high_resolution_clock::time_point endTime; //The time the stop function was called
bool hasStarted; bool hasStarted; //A flag to show that start() has been called
bool hasEnded; bool hasStopped; //A flag to show that stop() has been called
public: public:
Stopwatch(){ Stopwatch(){
hasStarted = hasEnded = false; //Make sure the flags are set to false to show nothing has been called yet
hasStarted = hasStopped = false;
startTime = endTime = std::chrono::high_resolution_clock::time_point(); //Set the times with a blank time
} }
~Stopwatch(){ ~Stopwatch(){
} }
//Set the start time and flag and make sure the stop flag is unset
void start(){ void start(){
hasStarted = true; //Show that the stopwatch has been started
hasStopped = false; //Show that the stopwatch is still running. Security in case the Stopwatch is used in multiple places
//Put this last to ensure that the time recorded is as close to the return time as possible
startTime = std::chrono::high_resolution_clock::now(); startTime = std::chrono::high_resolution_clock::now();
hasStarted = true;
} }
//Set the stop time and flag
void stop(){ void stop(){
//Put this first to ensure the time recorded is as close to the call time as possible
endTime = std::chrono::high_resolution_clock::now(); endTime = std::chrono::high_resolution_clock::now();
hasEnded = true; hasStopped = true; //Show that the stop function has been called
} }
//Return the duration in the default time period for the high_resolution_clock
double getTime(){ double getTime(){
if(hasStarted && hasEnded){ //If the stopwatch has started and stopped return the difference in the 2 times as the duration
if(hasStarted && hasStopped){
return (endTime - startTime).count(); return (endTime - startTime).count();
} }
//Otherwise return -1 as an error message
else{ else{
return 0; return -1;
}
}
double getSeconds(){
if(hasStarted && hasEnded){
std::chrono::duration<double> dur = endTime - startTime;
return dur.count();
}
else{
return 0;
}
}
double getMilli(){
if(hasStarted && hasEnded){
std::chrono::duration<double, std::milli> dur = endTime - startTime;
return dur.count();
}
else{
return 0;
}
}
double getMicro(){
if(hasStarted && hasEnded){
std::chrono::duration<double, std::micro> dur = endTime - startTime;
return dur.count();
}
else{
return 0;
} }
} }
//Return the duration in nanoseconds
double getNano(){ double getNano(){
if(hasStarted && hasEnded){ //If the stopwatch has started and stopped return the difference in the 2 times in nanoseconds
if(hasStarted && hasStopped){
std::chrono::duration<double, std::nano> dur = endTime - startTime; std::chrono::duration<double, std::nano> dur = endTime - startTime;
return dur.count(); return dur.count();
} }
//Otherwise return -1 as an error message
else{ else{
return 0; return -1;
} }
} }
//Return the duration in microseconds
double getMicro(){
//If the stopwatch has started and stopped return the difference in the 2 times in microseconds
if(hasStarted && hasStopped){
std::chrono::duration<double, std::micro> dur = endTime - startTime;
return dur.count();
}
//Otherwise return -1 as an error message
else{
return -1;
}
}
//Return the duration in milliseconds
double getMilli(){
//If the stopwatch has started and stopped return the difference in the 2 times in milliseconds
if(hasStarted && hasStopped){
std::chrono::duration<double, std::milli> dur = endTime - startTime;
return dur.count();
}
//Otherwise return -1 as an error message
else{
return -1;
}
}
//Return the duration in seconds
double getSeconds(){
//If the stopwatch has started and stopped return the difference in the 2 times in seconds
if(hasStarted && hasStopped){
std::chrono::duration<double> dur = endTime - startTime;
return dur.count();
}
//Otherwise return -1 as an error message
else{
return -1;
}
}
//This function resets all the variables so that it can be run again
void reset(){ void reset(){
hasStarted = hasEnded = false; hasStarted = hasStopped = false; //Set the flags as though nothing has happened
endTime = startTime = std::chrono::high_resolution_clock::time_point(); endTime = startTime = std::chrono::high_resolution_clock::time_point(); //Set the times with a blank time
}
};
} }
}; //end class Stopwatch
} //end namespace mee
#endif //end STOPWATCH_HPP #endif //end STOPWATCH_HPP