1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2024-11-08 16:57:16 +01:00
SqMod/module/Library/Chrono/Timer.cpp
Sandu Liviu Catalin 4a6bfc086c Major plugin refactor and cleanup.
Switched to POCO library for unified platform/library interface.
Deprecated the external module API. It was creating more problems than solving.
Removed most built-in libraries in favor of system libraries for easier maintenance.
Cleaned and secured code with help from static analyzers.
2021-01-30 08:51:39 +02:00

92 lines
3.0 KiB
C++

// ------------------------------------------------------------------------------------------------
#include "Library/Chrono/Timer.hpp"
#include "Library/Chrono/Timestamp.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
SQMOD_DECL_TYPENAME(Typename, _SC("SqTimer"))
// ------------------------------------------------------------------------------------------------
Timer::Timer()
: m_Timestamp(Chrono::GetCurrentSysTime())
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
int32_t Timer::Cmp(const Timer & o) const
{
if (m_Timestamp == o.m_Timestamp)
return 0;
else if (m_Timestamp > o.m_Timestamp)
return 1;
else
return -1;
}
// ------------------------------------------------------------------------------------------------
String Timer::ToString() const
{
return fmt::format("{}", m_Timestamp);
}
// ------------------------------------------------------------------------------------------------
void Timer::Reset()
{
m_Timestamp = Chrono::GetCurrentSysTime();
}
// ------------------------------------------------------------------------------------------------
Timestamp Timer::Restart()
{
const int64_t now = Chrono::GetCurrentSysTime(), elapsed = now - m_Timestamp;
m_Timestamp = now;
return Timestamp(elapsed);
}
// ------------------------------------------------------------------------------------------------
int64_t Timer::RestartRaw()
{
const int64_t now = Chrono::GetCurrentSysTime(), elapsed = now - m_Timestamp;
m_Timestamp = now;
return elapsed;
}
// ------------------------------------------------------------------------------------------------
Timestamp Timer::GetElapsedTime() const
{
return Timestamp(Chrono::GetCurrentSysTime() - m_Timestamp);
}
// ------------------------------------------------------------------------------------------------
int64_t Timer::GetElapsedTimeRaw() const
{
return (Chrono::GetCurrentSysTime() - m_Timestamp);
}
// ================================================================================================
void Register_ChronoTimer(HSQUIRRELVM vm, Table & /*cns*/)
{
RootTable(vm).Bind(Typename::Str,
Class< Timer >(vm, Typename::Str)
// Constructors
.Ctor()
.Ctor< const Timer & >()
// Core Meta-methods
.SquirrelFunc(_SC("_typename"), &Typename::Fn)
.Func(_SC("_tostring"), &Timer::ToString)
.Func(_SC("cmp"), &Timer::Cmp)
// Properties
.Prop(_SC("Elapsed"), &Timer::GetElapsedTime)
.Prop(_SC("ElapsedRaw"), &Timer::GetElapsedTimeRaw)
// Functions
.Func(_SC("Reset"), &Timer::Reset)
.Func(_SC("Restart"), &Timer::Restart)
.Func(_SC("RestartRaw"), &Timer::RestartRaw)
);
}
} // Namespace:: SqMod