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

Don't use fixed types.

This commit is contained in:
Sandu Liviu Catalin 2021-02-01 00:33:12 +02:00
parent a12ba35d53
commit ef5f1b07c6
2 changed files with 43 additions and 43 deletions

View File

@ -15,28 +15,28 @@ SQMOD_DECL_TYPENAME(TypenameS, _SC("SLongInt"))
SQMOD_DECL_TYPENAME(TypenameU, _SC("ULongInt")) SQMOD_DECL_TYPENAME(TypenameU, _SC("ULongInt"))
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
LongInt< int64_t >::LongInt(const SQChar * text) LongInt< signed long long >::LongInt(const SQChar * text)
: m_Data(0), m_Text() : m_Data(0), m_Text()
{ {
m_Data = std::strtoll(text, nullptr, 10); m_Data = std::strtoll(text, nullptr, 10);
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
LongInt< int64_t >::LongInt(const SQChar * text, uint32_t base) LongInt< signed long long >::LongInt(const SQChar * text, uint32_t base)
: m_Data(0), m_Text() : m_Data(0), m_Text()
{ {
m_Data = std::strtoll(text, nullptr, base); m_Data = std::strtoll(text, nullptr, base);
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
LongInt< int64_t > & LongInt< int64_t >::operator = (const SQChar * text) LongInt< signed long long > & LongInt< signed long long >::operator = (const SQChar * text)
{ {
m_Data = std::strtoll(text, nullptr, 10); m_Data = std::strtoll(text, nullptr, 10);
return *this; return *this;
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
const SQChar * LongInt< int64_t >::ToString() const SQChar * LongInt< signed long long >::ToString()
{ {
if (std::snprintf(m_Text, sizeof(m_Text), "%llu", m_Data) < 0) if (std::snprintf(m_Text, sizeof(m_Text), "%llu", m_Data) < 0)
{ {
@ -47,46 +47,46 @@ const SQChar * LongInt< int64_t >::ToString()
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
void LongInt< int64_t >::Random() void LongInt< signed long long >::Random()
{ {
m_Data = GetRandomInt64(); m_Data = GetRandomInt64();
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
void LongInt< int64_t >::Random(Type n) void LongInt< signed long long >::Random(Type n)
{ {
m_Data = GetRandomInt64(n); m_Data = GetRandomInt64(n);
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
void LongInt< int64_t >::Random(Type m, Type n) void LongInt< signed long long >::Random(Type m, Type n)
{ {
m_Data = GetRandomInt64(m, n); m_Data = GetRandomInt64(m, n);
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
LongInt< uint64_t >::LongInt(const SQChar * text) LongInt< unsigned long long >::LongInt(const SQChar * text)
: m_Data(0), m_Text() : m_Data(0), m_Text()
{ {
m_Data = std::strtoull(text, nullptr, 10); m_Data = std::strtoull(text, nullptr, 10);
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
LongInt< uint64_t >::LongInt(const SQChar * text, uint32_t base) LongInt< unsigned long long >::LongInt(const SQChar * text, uint32_t base)
: m_Data(0), m_Text() : m_Data(0), m_Text()
{ {
m_Data = std::strtoull(text, nullptr, base); m_Data = std::strtoull(text, nullptr, base);
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
LongInt< uint64_t > & LongInt< uint64_t >::operator = (const SQChar * text) LongInt< unsigned long long > & LongInt< unsigned long long >::operator = (const SQChar * text)
{ {
m_Data = std::strtoull(text, nullptr, 10); m_Data = std::strtoull(text, nullptr, 10);
return *this; return *this;
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
const SQChar * LongInt< uint64_t >::ToString() const SQChar * LongInt< unsigned long long >::ToString()
{ {
if (std::snprintf(m_Text, sizeof(m_Text), "%llu", m_Data) < 0) if (std::snprintf(m_Text, sizeof(m_Text), "%llu", m_Data) < 0)
{ {
@ -97,25 +97,25 @@ const SQChar * LongInt< uint64_t >::ToString()
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
void LongInt< uint64_t >::Random() void LongInt< unsigned long long >::Random()
{ {
m_Data = GetRandomUint64(); m_Data = GetRandomUint64();
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
void LongInt< uint64_t >::Random(Type n) void LongInt< unsigned long long >::Random(Type n)
{ {
m_Data = GetRandomUint64(n); m_Data = GetRandomUint64(n);
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
void LongInt< uint64_t >::Random(Type m, Type n) void LongInt< unsigned long long >::Random(Type m, Type n)
{ {
m_Data = GetRandomUint64(m, n); m_Data = GetRandomUint64(m, n);
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
int64_t PopStackSLong(HSQUIRRELVM vm, SQInteger idx) signed long long PopStackSLong(HSQUIRRELVM vm, SQInteger idx)
{ {
// Identify which type must be extracted // Identify which type must be extracted
switch (sq_gettype(vm, idx)) switch (sq_gettype(vm, idx))
@ -124,19 +124,19 @@ int64_t PopStackSLong(HSQUIRRELVM vm, SQInteger idx)
{ {
SQInteger val; SQInteger val;
sq_getinteger(vm, idx, &val); sq_getinteger(vm, idx, &val);
return static_cast< int64_t >(val); return static_cast< signed long long >(val);
} }
case OT_FLOAT: case OT_FLOAT:
{ {
SQFloat val; SQFloat val;
sq_getfloat(vm, idx, &val); sq_getfloat(vm, idx, &val);
return ConvTo< int64_t >::From(val); return ConvTo< signed long long >::From(val);
} }
case OT_BOOL: case OT_BOOL:
{ {
SQBool val; SQBool val;
sq_getbool(vm, idx, &val); sq_getbool(vm, idx, &val);
return static_cast< int64_t >(val); return static_cast< signed long long >(val);
} }
case OT_STRING: case OT_STRING:
{ {
@ -152,7 +152,7 @@ int64_t PopStackSLong(HSQUIRRELVM vm, SQInteger idx)
case OT_CLASS: case OT_CLASS:
case OT_USERDATA: case OT_USERDATA:
{ {
return static_cast< int64_t >(sq_getsize(vm, idx)); return static_cast< signed long long >(sq_getsize(vm, idx));
} }
case OT_INSTANCE: case OT_INSTANCE:
{ {
@ -168,14 +168,14 @@ int64_t PopStackSLong(HSQUIRRELVM vm, SQInteger idx)
// Attempt to treat the value as a unsigned long instance // Attempt to treat the value as a unsigned long instance
try try
{ {
return ConvTo< int64_t >::From(Var< const ULongInt & >(vm, idx).value.GetNum()); return ConvTo< signed long long >::From(Var< const ULongInt & >(vm, idx).value.GetNum());
} }
catch (...) catch (...)
{ {
// Just ignore it... // Just ignore it...
} }
// Attempt to get the size of the instance as a fall back // Attempt to get the size of the instance as a fall back
return static_cast< int64_t >(sq_getsize(vm, idx)); return static_cast< signed long long >(sq_getsize(vm, idx));
} }
default: break; default: break;
} }
@ -184,7 +184,7 @@ int64_t PopStackSLong(HSQUIRRELVM vm, SQInteger idx)
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
uint64_t PopStackULong(HSQUIRRELVM vm, SQInteger idx) unsigned long long PopStackULong(HSQUIRRELVM vm, SQInteger idx)
{ {
// Identify which type must be extracted // Identify which type must be extracted
switch (sq_gettype(vm, idx)) switch (sq_gettype(vm, idx))
@ -193,19 +193,19 @@ uint64_t PopStackULong(HSQUIRRELVM vm, SQInteger idx)
{ {
SQInteger val; SQInteger val;
sq_getinteger(vm, idx, &val); sq_getinteger(vm, idx, &val);
return ConvTo< uint64_t >::From(val); return ConvTo< unsigned long long >::From(val);
} }
case OT_FLOAT: case OT_FLOAT:
{ {
SQFloat val; SQFloat val;
sq_getfloat(vm, idx, &val); sq_getfloat(vm, idx, &val);
return ConvTo< uint64_t >::From(val); return ConvTo< unsigned long long >::From(val);
} }
case OT_BOOL: case OT_BOOL:
{ {
SQBool val; SQBool val;
sq_getbool(vm, idx, &val); sq_getbool(vm, idx, &val);
return ConvTo< uint64_t >::From(val); return ConvTo< unsigned long long >::From(val);
} }
case OT_STRING: case OT_STRING:
{ {
@ -221,14 +221,14 @@ uint64_t PopStackULong(HSQUIRRELVM vm, SQInteger idx)
case OT_CLASS: case OT_CLASS:
case OT_USERDATA: case OT_USERDATA:
{ {
return ConvTo< uint64_t >::From(sq_getsize(vm, idx)); return ConvTo< unsigned long long >::From(sq_getsize(vm, idx));
} }
case OT_INSTANCE: case OT_INSTANCE:
{ {
// Attempt to treat the value as a signed long instance // Attempt to treat the value as a signed long instance
try try
{ {
return ConvTo< uint64_t >::From(Var< const SLongInt & >(vm, idx).value.GetNum()); return ConvTo< unsigned long long >::From(Var< const SLongInt & >(vm, idx).value.GetNum());
} }
catch (...) catch (...)
{ {
@ -244,7 +244,7 @@ uint64_t PopStackULong(HSQUIRRELVM vm, SQInteger idx)
// Just ignore it... // Just ignore it...
} }
// Attempt to get the size of the instance as a fall back // Attempt to get the size of the instance as a fall back
return ConvTo< uint64_t >::From(sq_getsize(vm, idx)); return ConvTo< unsigned long long >::From(sq_getsize(vm, idx));
} }
default: break; default: break;
} }
@ -260,7 +260,7 @@ const SLongInt & GetSLongInt()
return l; return l;
} }
const SLongInt & GetSLongInt(int64_t n) const SLongInt & GetSLongInt(signed long long n)
{ {
static SLongInt l; static SLongInt l;
l.SetNum(n); l.SetNum(n);
@ -281,7 +281,7 @@ const ULongInt & GetULongInt()
return l; return l;
} }
const ULongInt & GetULongInt(uint64_t n) const ULongInt & GetULongInt(unsigned long long n)
{ {
static ULongInt l; static ULongInt l;
l.SetNum(n); l.SetNum(n);

View File

@ -12,12 +12,12 @@ template < typename T > class LongInt;
/* ------------------------------------------------------------------------------------------------ /* ------------------------------------------------------------------------------------------------
* Specialization of the Long int class for signed integers. * Specialization of the Long int class for signed integers.
*/ */
template <> class LongInt< int64_t > template <> class LongInt< signed long long >
{ {
public: public:
// -------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------
typedef int64_t Type; // The specialized type. typedef signed long long Type; // The specialized type.
private: private:
@ -418,7 +418,7 @@ public:
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with another one. * Used by the script engine to compare an instance of this type with another one.
*/ */
SQMOD_NODISCARD int32_t Cmp(const LongInt< uint64_t > & o) const; SQMOD_NODISCARD int32_t Cmp(const LongInt< unsigned long long > & o) const;
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value. * Used by the script engine to compare an instance of this type with a scalar value.
@ -622,12 +622,12 @@ public:
/* ------------------------------------------------------------------------------------------------ /* ------------------------------------------------------------------------------------------------
* Specialization of the Long int class for unsigned integers. * Specialization of the Long int class for unsigned integers.
*/ */
template <> class LongInt< uint64_t > template <> class LongInt< unsigned long long >
{ {
public: public:
// -------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------
typedef uint64_t Type; // The specialized type. typedef unsigned long long Type; // The specialized type.
private: private:
@ -1028,7 +1028,7 @@ public:
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with another one. * Used by the script engine to compare an instance of this type with another one.
*/ */
SQMOD_NODISCARD int32_t Cmp(const LongInt< int64_t > & o) const SQMOD_NODISCARD int32_t Cmp(const LongInt< signed long long > & o) const
{ {
const Type v = ConvTo< Type >::From(o.GetNum()); const Type v = ConvTo< Type >::From(o.GetNum());
@ -1246,7 +1246,7 @@ public:
}; };
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
inline int32_t LongInt< int64_t >::Cmp(const LongInt< uint64_t > & o) const inline int32_t LongInt< signed long long >::Cmp(const LongInt< unsigned long long > & o) const
{ {
const Type v = ConvTo< Type >::From(o.GetNum()); const Type v = ConvTo< Type >::From(o.GetNum());
@ -1265,27 +1265,27 @@ inline int32_t LongInt< int64_t >::Cmp(const LongInt< uint64_t > & o) const
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
typedef LongInt< int64_t > SLongInt; typedef LongInt< signed long long > SLongInt;
typedef LongInt< uint64_t > ULongInt; typedef LongInt< unsigned long long > ULongInt;
/* ------------------------------------------------------------------------------------------------ /* ------------------------------------------------------------------------------------------------
* Attempt to pop the value at the specified index on the stack as a signed long integer. * Attempt to pop the value at the specified index on the stack as a signed long integer.
*/ */
int64_t PopStackSLong(HSQUIRRELVM vm, SQInteger idx); signed long long PopStackSLong(HSQUIRRELVM vm, SQInteger idx);
/* ------------------------------------------------------------------------------------------------ /* ------------------------------------------------------------------------------------------------
* Attempt to pop the value at the specified index on the stack as an unsigned long integer. * Attempt to pop the value at the specified index on the stack as an unsigned long integer.
*/ */
uint64_t PopStackULong(HSQUIRRELVM vm, SQInteger idx); unsigned long long PopStackULong(HSQUIRRELVM vm, SQInteger idx);
/* ------------------------------------------------------------------------------------------------ /* ------------------------------------------------------------------------------------------------
* Get a persistent LongInt instance with the given values. * Get a persistent LongInt instance with the given values.
*/ */
const SLongInt & GetSLongInt(); const SLongInt & GetSLongInt();
const SLongInt & GetSLongInt(int64_t n); const SLongInt & GetSLongInt(signed long long n);
const SLongInt & GetSLongInt(const SQChar * s); const SLongInt & GetSLongInt(const SQChar * s);
const ULongInt & GetULongInt(); const ULongInt & GetULongInt();
const ULongInt & GetULongInt(uint64_t n); const ULongInt & GetULongInt(unsigned long long n);
const ULongInt & GetULongInt(const SQChar * s); const ULongInt & GetULongInt(const SQChar * s);
} // Namespace:: SqMod } // Namespace:: SqMod