uox3/source/EventTimer.hpp

56 lines
1.5 KiB
C++
Raw Permalink Normal View History

2022-02-24 22:48:39 -05:00
#ifndef EventTimer_hpp
#define EventTimer_hpp
#include <chrono>
#include <cstdint>
#include <string>
2022-02-25 09:44:11 -05:00
2022-02-25 09:54:29 -05:00
// A few instrumentation macros
// This estabishs a timer, and state determines if on/off
#define EVENT_TIMER( varname, state ) \
constexpr auto TIMER_##varname = state; \
2022-02-25 21:16:32 -05:00
auto varname = EventTimer();
2022-02-25 10:57:06 -05:00
#if !defined( EVENT_TIMER_ON )
2022-02-25 11:06:05 -05:00
#define EVENT_TIMER_ON 1
#endif
#if !defined( EVENT_TIMER_OFF )
2022-02-25 11:06:05 -05:00
#define EVENT_TIMER_OFF 0
#endif
#if !defined( EVENT_TIMER_CLEAR )
2022-02-25 11:06:05 -05:00
#define EVENT_TIMER_CLEAR 1
#endif
#if !defined( EVENT_TIMER_KEEP )
2022-02-25 11:06:05 -05:00
#define EVENT_TIMER_KEEP 0
#endif
2022-02-25 09:44:11 -05:00
2022-02-25 09:54:29 -05:00
// This prints out the delta time in millisconds for the timer since the last reset.
// The msg variable is the message you want with the time (no quotes around it, just text (so you cant use , in the message)
// reset tells the timer if it should reset the time count, or continue without resetting
#define EVENT_TIMER_NOW( varname, msg, Reset ) \
if constexpr ( TIMER_##varname ) { varname.Output( #msg, Reset ); }
2022-02-25 09:44:11 -05:00
2022-02-25 09:54:29 -05:00
// This resets the timer
#define EVENT_TIMER_RESET( varname ) if constexpr ( TIMER_##varname ) { varname.Elapsed( true ); }
2022-02-25 09:44:11 -05:00
2022-02-25 07:55:28 -05:00
/*
#define EVENT_TIMER_OFF ( #varnam, #msg) \
#if defined(TIMER_ON) \
varname.output("msg"); \
2022-02-25 07:55:28 -05:00
#endif
*/
2022-02-24 22:48:39 -05:00
//=========================================================
class EventTimer
{
2022-02-24 22:48:39 -05:00
private:
std::chrono::high_resolution_clock::time_point _now;
2022-02-24 22:48:39 -05:00
public:
EventTimer();
[[maybe_unused]] auto Elapsed( bool reset = true ) -> long long;
auto Output( const std::string &label, bool reset = true ) -> void;
2022-02-24 22:48:39 -05:00
};
#endif /* EventTimer_hpp */