2016-03-25 13:28:07 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
#include "Library/Chrono/Timer.hpp"
|
|
|
|
#include "Library/Chrono/Timestamp.hpp"
|
|
|
|
#include "Base/Shared.hpp"
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
namespace SqMod {
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
Timer::Timer()
|
2016-06-04 18:17:42 +02:00
|
|
|
: m_Timestamp(Chrono::GetCurrentSysTime())
|
2016-03-25 13:28:07 +01:00
|
|
|
{
|
|
|
|
/* ... */
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
Int32 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
CSStr Timer::ToString() const
|
|
|
|
{
|
|
|
|
return ToStrF("%lld", m_Timestamp);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void Timer::Reset()
|
|
|
|
{
|
2016-06-04 18:17:42 +02:00
|
|
|
m_Timestamp = Chrono::GetCurrentSysTime();
|
2016-03-25 13:28:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
Timestamp Timer::Restart()
|
|
|
|
{
|
2016-06-04 18:17:42 +02:00
|
|
|
const Int64 now = Chrono::GetCurrentSysTime(), elapsed = now - m_Timestamp;
|
2016-03-25 13:28:07 +01:00
|
|
|
m_Timestamp = now;
|
|
|
|
return Timestamp(elapsed);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
Int64 Timer::RestartRaw()
|
|
|
|
{
|
2016-06-04 18:17:42 +02:00
|
|
|
const Int64 now = Chrono::GetCurrentSysTime(), elapsed = now - m_Timestamp;
|
2016-03-25 13:28:07 +01:00
|
|
|
m_Timestamp = now;
|
|
|
|
return elapsed;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
Timestamp Timer::GetElapsedTime() const
|
|
|
|
{
|
2016-06-04 18:17:42 +02:00
|
|
|
return Timestamp(Chrono::GetCurrentSysTime() - m_Timestamp);
|
2016-03-25 13:28:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
Int64 Timer::GetElapsedTimeRaw() const
|
|
|
|
{
|
2016-06-04 18:17:42 +02:00
|
|
|
return (Chrono::GetCurrentSysTime() - m_Timestamp);
|
2016-03-25 13:28:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ================================================================================================
|
2016-03-26 17:18:41 +01:00
|
|
|
void Register_ChronoTimer(HSQUIRRELVM vm, Table & /*cns*/)
|
2016-03-25 13:28:07 +01:00
|
|
|
{
|
|
|
|
RootTable(vm).Bind(_SC("SqTimer"), Class< Timer >(vm, _SC("SqChronoTimer"))
|
|
|
|
// Constructors
|
|
|
|
.Ctor()
|
|
|
|
.Ctor< const Timer & >()
|
2016-06-03 20:26:19 +02:00
|
|
|
// Core Meta-methods
|
2016-03-25 13:28:07 +01:00
|
|
|
.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
|