Spinning Topp Logo BlackTopp Studios
inc
timer.h
1 // © Copyright 2010 - 2016 BlackTopp Studios Inc.
2 /* This file is part of The Mezzanine Engine.
3 
4  The Mezzanine Engine is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  The Mezzanine Engine is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with The Mezzanine Engine. If not, see <http://www.gnu.org/licenses/>.
16 */
17 /* The original authors have included a copy of the license specified above in the
18  'Docs' folder. See 'gpl.txt'
19 */
20 /* We welcome the use of the Mezzanine engine to anyone, including companies who wish to
21  Build professional software and charge for their product.
22 
23  However there are some practical restrictions, so if your project involves
24  any of the following you should contact us and we will try to work something
25  out:
26  - DRM or Copy Protection of any kind(except Copyrights)
27  - Software Patents You Do Not Wish to Freely License
28  - Any Kind of Linking to Non-GPL licensed Works
29  - Are Currently In Violation of Another Copyright Holder's GPL License
30  - If You want to change our code and not add a few hundred MB of stuff to
31  your distribution
32 
33  These and other limitations could cause serious legal problems if you ignore
34  them, so it is best to simply contact us or the Free Software Foundation, if
35  you have any questions.
36 
37  Joseph Toppi - toppij@gmail.com
38  John Blackwood - makoenergy02@gmail.com
39 */
40 #ifndef _timer_h
41 #define _timer_h
42 
43 #include "datatypes.h"
44 
45 namespace Mezzanine
46 {
47  /// @brief An enum describing how the text output of a timer should be formatted.
49  {
50  TF_RawMicro = 1, ///< Outputs the current time in microseconds.
51  TF_RawMilli = 2, ///< Outputs the current time in milliseconds.
52  TF_Seconds = 3, ///< Outputs the current time in seconds.
53  TF_SecondsMilli = 4, ///< Outputs the current time in "seconds.milliseconds". Milliseconds are out to 3 digits.
54  TF_MinutesSeconds = 5 ///< Outputs the current time in "minutes:seconds".
55  };
56 
57  /// @brief A simple enum describing how the timer should increment time.
58  enum CountMode
59  {
60  CM_CountUp = 1, ///< The timer counts up, meaning the Timer current time is showing time elapsed.
61  CM_CountDown = 2 ///< The timer counts down, meaning the Timer current time is showing remaining time.
62  };
63 
64  ///////////////////////////////////////////////////////////////////////////////
65  /// @brief A basic timer class to assist in timed operations.
66  ///////////////////////////////////////
68  {
69  protected:
70  /// @internal
71  /// @brief Convenience type for processing the current time.
72  typedef MaxInt(*CountFunct)(const MaxInt, const MaxInt);
73 
74  /// @internal
75  /// @brief The time stamp from when the last time the Timer was updated.
76  MaxInt LastStamp;
77  /// @internal
78  /// @brief The current amount of microseconds that has elapsed since starting to track time.
79  MaxInt CurrentTime;
80  /// @internal
81  /// @brief A pointer to the function currently doing the counting for this Timer.
82  CountFunct TimerCounter;
83 
84  /// @internal
85  /// @brief Updates the current time being tracked by this timer.
86  /// @return Returns the updated current time.
87  MaxInt UpdateTime();
88  public:
89  /// @brief Class Constructor.
90  Timer();
91  /// @brief Class Destructor.
92  ~Timer();
93 
94  ///////////////////////////////////////////////////////////////////////////////
95  // Utility
96 
97  /// @brief Sets the current time in Microseconds.
98  /// @return Returns a reference to this Timer.
99  /// @param Current The value to set as current time in Microseconds.
100  void SetCurrentTime(const Whole Current);
101  /// @brief Sets the current time in Milliseconds. The time that resetting sets the Timer to.
102  /// @return Returns a reference to this Timer.
103  /// @param Current The value to set as current time in Milliseconds.
104  void SetCurrentTimeInMilliseconds(const Whole Current);
105  /// @brief Gets the Current time in Microseconds.
106  /// @return Returns a Whole representing the current time in Microseconds.
107  Whole GetCurrentTime();
108  /// @brief Gets the Current time in Milliseconds.
109  /// @return Returns a Whole representing the current time in Milliseconds.
110  Whole GetCurrentTimeInMilliseconds();
111 
112  /// @brief Sets the mode the timer should use to increment time.
113  /// @param Mode The mode to be set, see the CountMode enum for more details.
114  void SetCountMode(const Mezzanine::CountMode Mode);
115  /// @brief Gets the mode the timer is using to increment time.
116  /// @return Returns a CountMode enum value determining if this timer is counting up or down from it's current time.
117  Mezzanine::CountMode GetCountMode() const;
118 
119  /// @brief Activates the Timer.
120  void Start();
121  /// @brief Deactivates the Timer.
122  void Stop();
123  /// @brief Sets the current time to an initial value and stops the Timer if it is running.
124  /// @param StartTime The time in microseconds to be begin the Timer at the next time it is started.
125  void Reset(const Whole StartTime = 0);
126  /// @brief Gets whether or not this Timer is currently running.
127  /// @return Returns true if this Timer is active, false if it is stopped.
128  Boole IsTicking();
129  /// @brief Gets whether or not this Timer is currently running.
130  /// @return Returns true if this Timer is not currently active, false otherwise.
131  Boole IsStopped();
132 
133  /// @brief Gets the current time of this Timer as a string.
134  /// @param Format A TimeFormat enum value representing how the current time should be presented.
135  /// @return Returns a string containing a description of the current time in the specified format.
136  String GetTimeAsText(const Mezzanine::TimeFormat Format);
137  };//Timer
138 }//Mezzanine
139 
140 #endif
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
MaxInt CurrentTime
The current amount of microseconds that has elapsed since starting to track time. ...
Definition: timer.h:79
All the definitions for datatypes as well as some basic conversion functions are defined here...
CountMode
A simple enum describing how the timer should increment time.
Definition: timer.h:58
Outputs the current time in "minutes:seconds".
Definition: timer.h:54
The timer counts up, meaning the Timer current time is showing time elapsed.
Definition: timer.h:60
CountFunct TimerCounter
A pointer to the function currently doing the counting for this Timer.
Definition: timer.h:82
Outputs the current time in "seconds.milliseconds". Milliseconds are out to 3 digits.
Definition: timer.h:53
Outputs the current time in seconds.
Definition: timer.h:52
MaxInt LastStamp
The time stamp from when the last time the Timer was updated.
Definition: timer.h:76
Outputs the current time in microseconds.
Definition: timer.h:50
The timer counts down, meaning the Timer current time is showing remaining time.
Definition: timer.h:61
A basic timer class to assist in timed operations.
Definition: timer.h:67
#define MEZZ_LIB
Some platforms require special decorations to denote what is exported/imported in a share library...
long long MaxInt
A large integer type suitable for compile time math and long term microsecond time keeping...
Definition: datatypes.h:190
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
unsigned long Whole
Whole is an unsigned integer, it will be at least 32bits in size.
Definition: datatypes.h:151
Outputs the current time in milliseconds.
Definition: timer.h:51
TimeFormat
An enum describing how the text output of a timer should be formatted.
Definition: timer.h:48
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159