1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2025-02-20 19:57:12 +01:00

Furher implementation and improvement of the Chrono types and also exposed them to the module API.

Tighten the safety of exported functions to avoid exceptions leaking outside the host plugin.
This commit is contained in:
Sandu Liviu Catalin 2016-06-04 22:33:34 +03:00
parent 36c49cd09c
commit 2aa7e8b7c2
10 changed files with 367 additions and 16 deletions

View File

@ -69,15 +69,21 @@ extern "C" {
typedef SQRESULT (*SqEx_LoadScript) (const SQChar * filepath);
//long numbers
typedef SQRESULT (*SqEx_GetSLongValue) (HSQUIRRELVM vm, SQInteger idx, SqInt64 * num);
typedef void (*SqEx_PushSLongObject) (HSQUIRRELVM vm, SqInt64 num);
typedef SQRESULT (*SqEx_PushSLongObject) (HSQUIRRELVM vm, SqInt64 num);
typedef SQRESULT (*SqEx_GetULongValue) (HSQUIRRELVM vm, SQInteger idx, SqUint64 * num);
typedef void (*SqEx_PushULongObject) (HSQUIRRELVM vm, SqUint64 num);
typedef SQRESULT (*SqEx_PushULongObject) (HSQUIRRELVM vm, SqUint64 num);
//time utilities
typedef SqInt64 (*SqEx_GetCurrentSysTime) (void);
typedef SqInt64 (*SqEx_GetEpochTimeMicro) (void);
typedef SqInt64 (*SqEx_GetEpochTimeMilli) (void);
typedef SQRESULT (*SqEx_GetTimestamp) (HSQUIRRELVM vm, SQInteger idx, SqInt64 * num);
typedef void (*SqEx_PushTimestamp) (HSQUIRRELVM vm, SqInt64 num);
typedef SQRESULT (*SqEx_PushTimestamp) (HSQUIRRELVM vm, SqInt64 num);
typedef SQRESULT (*SqEx_GetDate) (HSQUIRRELVM vm, SQInteger idx, int16_t * year, int8_t * month, int8_t * day);
typedef SQRESULT (*SqEx_PushDate) (HSQUIRRELVM vm, int16_t year, int8_t month, int8_t day);
typedef SQRESULT (*SqEx_GetTime) (HSQUIRRELVM vm, SQInteger idx, int8_t * hour, int8_t * minute, int8_t * second, int16_t * millisecond);
typedef SQRESULT (*SqEx_PushTime) (HSQUIRRELVM vm, int8_t hour, int8_t minute, int8_t second, int16_t millisecond);
typedef SQRESULT (*SqEx_GetDatetime) (HSQUIRRELVM vm, SQInteger idx, int16_t * year, int8_t * month, int8_t * day, int8_t * hour, int8_t * minute, int8_t * second, int16_t * millisecond);
typedef SQRESULT (*SqEx_PushDatetime) (HSQUIRRELVM vm, int16_t year, int8_t month, int8_t day, int8_t hour, int8_t minute, int8_t second, int16_t millisecond);
//stack utilities
typedef SQInteger (*SqEx_PopStackInteger) (HSQUIRRELVM vm, SQInteger idx);
typedef SQFloat (*SqEx_PopStackFloat) (HSQUIRRELVM vm, SQInteger idx);
@ -121,6 +127,12 @@ extern "C" {
SqEx_GetEpochTimeMilli GetEpochTimeMilli;
SqEx_GetTimestamp GetTimestamp;
SqEx_PushTimestamp PushTimestamp;
SqEx_GetDate GetDate;
SqEx_PushDate PushDate;
SqEx_GetTime GetTime;
SqEx_PushTime PushTime;
SqEx_GetDatetime GetDatetime;
SqEx_PushDatetime PushDatetime;
//stack utilities
SqEx_PopStackInteger PopStackInteger;
SqEx_PopStackFloat PopStackFloat;

View File

@ -3,7 +3,9 @@
// ------------------------------------------------------------------------------------------------
#include "Library/Numeric.hpp"
#include "Library/Chrono.hpp"
#include "Library/Chrono/Date.hpp"
#include "Library/Chrono/Time.hpp"
#include "Library/Chrono/Datetime.hpp"
#include "Library/Chrono/Timestamp.hpp"
// ------------------------------------------------------------------------------------------------
@ -106,9 +108,20 @@ static SQRESULT SqEx_GetSLongValue(HSQUIRRELVM vm, SQInteger idx, Int64 * num)
}
// ------------------------------------------------------------------------------------------------
static void SqEx_PushSLongObject(HSQUIRRELVM vm, Int64 num)
static SQRESULT SqEx_PushSLongObject(HSQUIRRELVM vm, Int64 num)
{
Var< const SLongInt & >::push(vm, SLongInt(num));
// Attempt to push the requested instance
try
{
Var< const SLongInt & >::push(vm, SLongInt(num));
}
catch (...)
{
// Specify that we failed
return SQ_ERROR;
}
// Specify that we succeeded
return SQ_OK;
}
// ------------------------------------------------------------------------------------------------
@ -169,9 +182,20 @@ static SQRESULT SqEx_GetULongValue(HSQUIRRELVM vm, SQInteger idx, Uint64 * num)
}
// ------------------------------------------------------------------------------------------------
static void SqEx_PushULongObject(HSQUIRRELVM vm, Uint64 num)
static SQRESULT SqEx_PushULongObject(HSQUIRRELVM vm, Uint64 num)
{
Var< const ULongInt & >::push(vm, ULongInt(num));
// Attempt to push the requested instance
try
{
Var< const ULongInt & >::push(vm, ULongInt(num));
}
catch (...)
{
// Specify that we failed
return SQ_ERROR;
}
// Specify that we succeeded
return SQ_OK;
}
// ------------------------------------------------------------------------------------------------
@ -232,9 +256,223 @@ static SQRESULT SqEx_GetTimestamp(HSQUIRRELVM vm, SQInteger idx, Int64 * num)
}
// ------------------------------------------------------------------------------------------------
static void SqEx_PushTimestamp(HSQUIRRELVM vm, Int64 num)
static SQRESULT SqEx_PushTimestamp(HSQUIRRELVM vm, Int64 num)
{
Var< const Timestamp & >::push(vm, Timestamp(num));
// Attempt to push the requested instance
try
{
Var< const Timestamp & >::push(vm, Timestamp(num));
}
catch (...)
{
// Specify that we failed
return SQ_ERROR;
}
// Specify that we succeeded
return SQ_OK;
}
// ------------------------------------------------------------------------------------------------
SQRESULT SqEx_GetDate(HSQUIRRELVM vm, SQInteger idx, int16_t * year, int8_t * month, int8_t * day)
{
// Is this an instance that we can treat as a Date type?
if (sq_gettype(vm, idx) == OT_INSTANCE)
{
// Attempt to obtain the time-stamp and it's value from the stack
try
{
// Attempt to retrieve the instance
Var< Date * > var(vm, idx);
// Assign the year
if (year != nullptr)
{
*year = var.value->GetYear();
}
// Assign the month
if (month != nullptr)
{
*month = var.value->GetMonth();
}
// Assign the day
if (day != nullptr)
{
*day = var.value->GetDay();
}
}
catch (...)
{
return SQ_ERROR; // Unable to obtain the value!
}
}
// Unrecognized value
else
{
return SQ_ERROR;
}
// Value retrieved
return SQ_OK;
}
// ------------------------------------------------------------------------------------------------
SQRESULT SqEx_PushDate(HSQUIRRELVM vm, int16_t year, int8_t month, int8_t day)
{
// Attempt to push the requested instance
try
{
Var< const Date & >::push(vm, Date(year, month, day));
}
catch (...)
{
// Specify that we failed
return SQ_ERROR;
}
// Specify that we succeeded
return SQ_OK;
}
// ------------------------------------------------------------------------------------------------
SQRESULT SqEx_GetTime(HSQUIRRELVM vm, SQInteger idx, int8_t * hour, int8_t * minute, int8_t * second,
int16_t * millisecond)
{
// Is this an instance that we can treat as a Time type?
if (sq_gettype(vm, idx) == OT_INSTANCE)
{
// Attempt to obtain the time-stamp and it's value from the stack
try
{
// Attempt to retrieve the instance
Var< Time * > var(vm, idx);
// Assign the hour
if (hour != nullptr)
{
*hour = var.value->GetHour();
}
// Assign the minute
if (minute != nullptr)
{
*minute = var.value->GetMinute();
}
// Assign the second
if (second != nullptr)
{
*second = var.value->GetSecond();
}
// Assign the millisecond
if (millisecond != nullptr)
{
*millisecond = var.value->GetMillisecond();
}
}
catch (...)
{
return SQ_ERROR; // Unable to obtain the value!
}
}
// Unrecognized value
else
{
return SQ_ERROR;
}
// Value retrieved
return SQ_OK;
}
// ------------------------------------------------------------------------------------------------
SQRESULT SqEx_PushTime(HSQUIRRELVM vm, int8_t hour, int8_t minute, int8_t second,
int16_t millisecond)
{
// Attempt to push the requested instance
try
{
Var< const Time & >::push(vm, Time(hour, minute, second, millisecond));
}
catch (...)
{
// Specify that we failed
return SQ_ERROR;
}
// Specify that we succeeded
return SQ_OK;
}
// ------------------------------------------------------------------------------------------------
SQRESULT SqEx_GetDatetime(HSQUIRRELVM vm, SQInteger idx, int16_t * year, int8_t * month, int8_t * day,
int8_t * hour, int8_t * minute, int8_t * second, int16_t * millisecond)
{
// Is this an instance that we can treat as a Datetime type?
if (sq_gettype(vm, idx) == OT_INSTANCE)
{
// Attempt to obtain the time-stamp and it's value from the stack
try
{
// Attempt to retrieve the instance
Var< Datetime * > var(vm, idx);
// Assign the year
if (year != nullptr)
{
*year = var.value->GetYear();
}
// Assign the month
if (month != nullptr)
{
*month = var.value->GetMonth();
}
// Assign the day
if (day != nullptr)
{
*day = var.value->GetDay();
}
// Assign the hour
if (hour != nullptr)
{
*hour = var.value->GetHour();
}
// Assign the minute
if (minute != nullptr)
{
*minute = var.value->GetMinute();
}
// Assign the second
if (second != nullptr)
{
*second = var.value->GetSecond();
}
// Assign the millisecond
if (millisecond != nullptr)
{
*millisecond = var.value->GetMillisecond();
}
}
catch (...)
{
return SQ_ERROR; // Unable to obtain the value!
}
}
// Unrecognized value
else
{
return SQ_ERROR;
}
// Value retrieved
return SQ_OK;
}
// ------------------------------------------------------------------------------------------------
SQRESULT SqEx_PushDatetime(HSQUIRRELVM vm, int16_t year, int8_t month, int8_t day,
int8_t hour, int8_t minute, int8_t second, int16_t millisecond)
{
// Attempt to push the requested instance
try
{
Var< const Datetime & >::push(vm, Datetime(year, month, day, hour, minute, second, millisecond));
}
catch (...)
{
// Specify that we failed
return SQ_ERROR;
}
// Specify that we succeeded
return SQ_OK;
}
// ------------------------------------------------------------------------------------------------
@ -278,6 +516,12 @@ void InitExports()
g_SqExports.GetEpochTimeMilli = Chrono::GetEpochTimeMilli;
g_SqExports.GetTimestamp = SqEx_GetTimestamp;
g_SqExports.PushTimestamp = SqEx_PushTimestamp;
g_SqExports.GetDate = SqEx_GetDate;
g_SqExports.PushDate = SqEx_PushDate;
g_SqExports.GetTime = SqEx_GetTime;
g_SqExports.PushTime = SqEx_PushTime;
g_SqExports.GetDatetime = SqEx_GetDatetime;
g_SqExports.PushDatetime = SqEx_PushDatetime;
//stack utilities
g_SqExports.PopStackInteger = PopStackInteger;

View File

@ -1,7 +1,8 @@
// ------------------------------------------------------------------------------------------------
#include "Library/Chrono/Date.hpp"
#include "Library/Chrono/Date.hpp"
#include "Library/Chrono/Time.hpp"
#include "Library/Chrono/Datetime.hpp"
#include "Library/Chrono/Timestamp.hpp"
#include "Base/Shared.hpp"
// ------------------------------------------------------------------------------------------------
@ -355,6 +356,20 @@ Date Date::AndDays(Int32 days)
return d;
}
// ------------------------------------------------------------------------------------------------
Timestamp Date::GetTimestamp() const
{
// Calculate the current day of the year
Int32 days = Chrono::DayOfYear(m_Year, m_Month, m_Day);
// Calculate all days till the current year
for (Int32 year = 0; year < m_Year; --year)
{
days += Chrono::DaysInYear(year);
}
// Return the resulted timestamp
return Timestamp(static_cast< Int64 >(days * 86400000000LL));
}
// ================================================================================================
void Register_ChronoDate(HSQUIRRELVM vm, Table & /*cns*/)
{
@ -385,6 +400,7 @@ void Register_ChronoDate(HSQUIRRELVM vm, Table & /*cns*/)
.Prop(_SC("LeapYear"), &Date::IsThisLeapYear)
.Prop(_SC("YearDays"), &Date::GetYearDays)
.Prop(_SC("MonthDays"), &Date::GetMonthDays)
.Prop(_SC("Timestamp"), &Date::GetTimestamp)
// Member Methods
.Func(_SC("AddYears"), &Date::AddYears)
.Func(_SC("AddMonths"), &Date::AddMonths)

View File

@ -351,6 +351,10 @@ public:
return Chrono::DaysInMonth(m_Year, m_Month);
}
/* ------------------------------------------------------------------------------------------------
* Convert this date instance to a time-stamp.
*/
Timestamp GetTimestamp() const;
};
} // Namespace:: SqMod

View File

@ -2,6 +2,7 @@
#include "Library/Chrono/Datetime.hpp"
#include "Library/Chrono/Date.hpp"
#include "Library/Chrono/Time.hpp"
#include "Library/Chrono/Timestamp.hpp"
#include "Base/Shared.hpp"
// ------------------------------------------------------------------------------------------------
@ -716,6 +717,39 @@ Datetime Datetime::AndMilliseconds(Int32 milliseconds)
return dt;
}
// ------------------------------------------------------------------------------------------------
Date Datetime::GetDate() const
{
return Date(m_Year, m_Month, m_Day);
}
// ------------------------------------------------------------------------------------------------
Time Datetime::GetTime() const
{
return Time(m_Hour, m_Minute, m_Second, m_Millisecond);
}
// ------------------------------------------------------------------------------------------------
Timestamp Datetime::GetTimestamp() const
{
// Calculate the current day of the year
Int32 days = Chrono::DayOfYear(m_Year, m_Month, m_Day);
// Calculate all days till the current year
for (Int32 year = 0; year < m_Year; --year)
{
days += Chrono::DaysInYear(year);
}
// Calculate the microseconds in the resulted days
Int64 ms = static_cast< Int64 >(days * 86400000000LL);
// Calculate the microseconds in the current time
ms += static_cast< Int64 >(m_Hour * 3600000000LL);
ms += static_cast< Int64 >(m_Minute * 60000000L);
ms += static_cast< Int64 >(m_Second * 1000000L);
ms += static_cast< Int64 >(m_Millisecond * 1000L);
// Return the resulted timestamp
return Timestamp(ms);
}
// ================================================================================================
void Register_ChronoDatetime(HSQUIRRELVM vm, Table & /*cns*/)
{
@ -758,6 +792,9 @@ void Register_ChronoDatetime(HSQUIRRELVM vm, Table & /*cns*/)
.Prop(_SC("LeapYear"), &Datetime::IsThisLeapYear)
.Prop(_SC("YearDays"), &Datetime::GetYearDays)
.Prop(_SC("MonthDays"), &Datetime::GetMonthDays)
.Prop(_SC("Date"), &Datetime::GetDate)
.Prop(_SC("Time"), &Datetime::GetTime)
.Prop(_SC("Timestamp"), &Datetime::GetTimestamp)
// Member Methods
.Func(_SC("AddYears"), &Datetime::AddYears)
.Func(_SC("AddMonths"), &Datetime::AddMonths)

View File

@ -564,6 +564,20 @@ public:
return Chrono::DaysInMonth(m_Year, m_Month);
}
/* ------------------------------------------------------------------------------------------------
* Retrieve the date from this date-time instance.
*/
Date GetDate() const;
/* ------------------------------------------------------------------------------------------------
* Retrieve the time from this date-time instance.
*/
Time GetTime() const;
/* ------------------------------------------------------------------------------------------------
* Convert this date-time instance to a time-stamp.
*/
Timestamp GetTimestamp() const;
};
} // Namespace:: SqMod

View File

@ -2,6 +2,7 @@
#include "Library/Chrono/Time.hpp"
#include "Library/Chrono/Date.hpp"
#include "Library/Chrono/Datetime.hpp"
#include "Library/Chrono/Timestamp.hpp"
#include "Base/Shared.hpp"
// ------------------------------------------------------------------------------------------------
@ -404,6 +405,18 @@ Time Time::AndMilliseconds(Int32 milliseconds)
return t;
}
// ------------------------------------------------------------------------------------------------
Timestamp Time::GetTimestamp() const
{
// Calculate the microseconds in the current time
Int64 ms = static_cast< Int64 >(m_Hour * 3600000000LL);
ms += static_cast< Int64 >(m_Minute * 60000000L);
ms += static_cast< Int64 >(m_Second * 1000000L);
ms += static_cast< Int64 >(m_Millisecond * 1000L);
// Return the resulted timestamp
return Timestamp(ms);
}
// ================================================================================================
void Register_ChronoTime(HSQUIRRELVM vm, Table & /*cns*/)
{
@ -432,6 +445,7 @@ void Register_ChronoTime(HSQUIRRELVM vm, Table & /*cns*/)
.Prop(_SC("Minute"), &Time::GetMinute, &Time::SetMinute)
.Prop(_SC("Second"), &Time::GetSecond, &Time::SetSecond)
.Prop(_SC("Millisecond"), &Time::GetMillisecond, &Time::SetMillisecond)
.Prop(_SC("Timestamp"), &Time::GetTimestamp)
// Member Methods
.Func(_SC("AddHours"), &Time::AddHours)
.Func(_SC("AddMinutes"), &Time::AddMinutes)

View File

@ -355,6 +355,11 @@ public:
* Add the specified amount of milliseconds to obtain a new time.
*/
Time AndMilliseconds(Int32 milliseconds);
/* ------------------------------------------------------------------------------------------------
* Convert this time instance to a time-stamp.
*/
Timestamp GetTimestamp() const;
};
} // Namespace:: SqMod

View File

@ -7,9 +7,6 @@
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
class Timestamp;
/* ------------------------------------------------------------------------------------------------
*
*/

View File

@ -1,8 +1,10 @@
// ------------------------------------------------------------------------------------------------
#include "Library/Chrono/Timestamp.hpp"
#include "Library/Chrono/Timer.hpp"
#include "Library/Chrono/Date.hpp"
#include "Library/Chrono/Time.hpp"
#include "Library/Chrono/Datetime.hpp"
#include "Library/Numeric.hpp"
#include "Base/Shared.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
@ -18,11 +20,17 @@ Timestamp::Timestamp(const SLongInt & t)
Int32 Timestamp::Cmp(const Timestamp & o) const
{
if (m_Timestamp == o.m_Timestamp)
{
return 0;
}
else if (m_Timestamp > o.m_Timestamp)
{
return 1;
}
else
{
return -1;
}
}
// ------------------------------------------------------------------------------------------------
@ -118,7 +126,7 @@ static Timestamp SqGetYears(SQFloat ammount)
// ================================================================================================
void Register_ChronoTimestamp(HSQUIRRELVM vm, Table & /*cns*/)
{
RootTable(vm).Bind(_SC("SqTimestamp"), Class< Timestamp >(vm, _SC("SqChronoTimestamp"))
RootTable(vm).Bind(_SC("SqTimestamp"), Class< Timestamp >(vm, _SC("SqTimestamp"))
// Constructors
.Ctor()
.Ctor< const Timestamp & >()