From 799e1579969e6dbd0d69f40727ddbc172c3ca3bb Mon Sep 17 00:00:00 2001 From: Matthew Ellison Date: Fri, 8 Feb 2019 01:24:36 -0500 Subject: [PATCH] Reworked get(Time) function to for easier upkeep --- Stopwatch.hpp | 98 +++++++++++++++++++++++++++------------------------ 1 file changed, 51 insertions(+), 47 deletions(-) diff --git a/Stopwatch.hpp b/Stopwatch.hpp index c5a6153..bbb7841 100644 --- a/Stopwatch.hpp +++ b/Stopwatch.hpp @@ -34,6 +34,38 @@ private: std::chrono::high_resolution_clock::time_point endTime; //The time the stop function was called bool hasStarted; //A flag to show that start() has been called bool hasStopped; //A flag to show that stop() has been called + enum TIME_RESOLUTION {HOUR, MINUTE, SECOND, MILLISECOND, MICROSECOND, NANOSECOND, DEFAULT}; + //Return the duration in the default time period for the high_resolution_clock + double getTime(TIME_RESOLUTION timeResolution){ + double timePassed = 0; //Holds the amount of time that has passed + //If the timer hasn't been stopped then record the time right now. This will simulate looping at the stopwatch while it is still running + //I put this at the beginning to get the timestamp at close to the calling of the function as possible + if(!hasStopped){ + endTime = std::chrono::high_resolution_clock::now(); + } + //If the timer hasn't been started return a -1 as an error + if(!hasStarted){ + return -1; + } + //Decide what resolution to make the duration + switch(timeResolution){ + case HOUR: std::chrono::duration> dur = (endTime - startTime); + timePassed = dur.count(); break; + case MINUTE: std::chrono::duration> dur = (endTime - startTime); + timePassed = dur.count(); break; + case SECOND: std::chrono::duration dur = (endTime - startTime); + timePassed = dur.count(); break; + case MILLISECOND: std::chrono::duration dur = (endTime - startTime); + timePassed = dur.count(); break; + case MICROSECOND: std::chrono::duration dur = (endTime - startTime); + timePassed = dur.count(); break; + case NANOSECOND: std::chrono::duration dur = (endTime - startTime); + timePassed = dur.count(); break; + case DEFAULT: std::chrono::high_resolution_clock::duration dur = (endTime - startTime); + timePassed = dur.count(); break; + } + return timePassed; + } public: Stopwatch(){ //Make sure the flags are set to false to show nothing has been called yet @@ -54,66 +86,38 @@ public: 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(); - 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(){ - //If the stopwatch has started and stopped return the difference in the 2 times as the duration - if(hasStarted && hasStopped){ - return (endTime - startTime).count(); - } - //Otherwise return -1 as an error message - else{ - return -1; + //Make sure the stopwatch has started before you say it has stopped + if(hasStarted){ + hasStopped = true; //Show that the stop function has been called } } //Return the duration in nanoseconds double getNano(){ - //If the stopwatch has started and stopped return the difference in the 2 times in nanoseconds - if(hasStarted && hasStopped){ - std::chrono::duration dur = endTime - startTime; - return dur.count(); - } - //Otherwise return -1 as an error message - else{ - return -1; - } + return getTime(NANOSECOND); } //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 dur = endTime - startTime; - return dur.count(); - } - //Otherwise return -1 as an error message - else{ - return -1; - } + return getTime(MICROSECOND); } //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 dur = endTime - startTime; - return dur.count(); - } - //Otherwise return -1 as an error message - else{ - return -1; - } + return getTime(MILLISECOND); } //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 dur = endTime - startTime; - return dur.count(); - } - //Otherwise return -1 as an error message - else{ - return -1; - } + return getTime(SECOND); + } + //Return the duration in minutes + double getMinutes(){ + return getTime(MINUTE); + } + //Return the duration in hours + double getHours(){ + return getTime(HOUR); + } + //Return the duration in the default resolution of high_resolution_clock + double getTime(){ + return getTime(DEFAULT); } //This function resets all the variables so that it can be run again void reset(){