1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2024-11-08 00:37:15 +01:00

Added an untested implementation a 64bit integer wrapper for 32bit module.

This commit is contained in:
Sandu Liviu Catalin 2015-11-03 04:21:46 +02:00
parent 1e18099176
commit 4b5718a6ae
2 changed files with 204 additions and 97 deletions

View File

@ -1,5 +1,5 @@
#include "Library/LongInt.hpp"
//#include "Register.hpp"
#include "Register.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
@ -7,54 +7,55 @@ namespace SqMod {
// ------------------------------------------------------------------------------------------------
bool Register_LongInt(HSQUIRRELVM vm)
{
SQMOD_UNUSED_VAR(vm);
/*
Sqrat::RootTable(vm).Bind(_SC("SLongInt"), Sqrat::Class<SLongInt>(vm, _SC("SLongInt"))
// Output debugging information
LogDbg("Beginning registration of <Long integer> type");
// Attempt to register the specified type
Sqrat::RootTable(vm).Bind(_SC("SLongInt"), Sqrat::Class< SLongInt >(vm, _SC("SLongInt"))
/* Constructors */
.Ctor()
.Ctor(SLongInt::Value)
.Ctor(const SQChar *, SQInteger)
.Ctor< SLongInt::Type >()
.template Ctor< const SQChar *, SQInteger >()
/* Properties */
.Prop(_SC("str"), &SLongInt::GetCStr, &SLongInt::SetStr)
.Prop(_SC("num"), &SLongInt::GetSNum, &SLongInt::SetNum)
/* Metamethods */
.Func(_SC("_tostring"), &SLongInt::ToString)
.Func(_SC("_cmp"), &SLongInt::Cmp)
.Func<SLongInt (SLongInt::*)(const SLongInt &) const>(_SC("_add"), &SLongInt::operator +)
.Func<SLongInt (SLongInt::*)(const SLongInt &) const>(_SC("_sub"), &SLongInt::operator -)
.Func<SLongInt (SLongInt::*)(const SLongInt &) const>(_SC("_mul"), &SLongInt::operator *)
.Func<SLongInt (SLongInt::*)(const SLongInt &) const>(_SC("_div"), &SLongInt::operator /)
.Func<SLongInt (SLongInt::*)(const SLongInt &) const>(_SC("_modulo"), &SLongInt::operator %)
.Func<SLongInt (SLongInt::*)(void) const>(_SC("_unm"), &SLongInt::operator -)
.Overload<void (SLongInt::*)(void)>(_SC("random"), &SLongInt::Random)
.Overload<void (SLongInt::*)(SLongInt::Value, SLongInt::Value)>(_SC("random"), &SLongInt::Random)
.Func< SLongInt (SLongInt::*)(const SLongInt &) const >(_SC("_add"), &SLongInt::operator +)
.Func< SLongInt (SLongInt::*)(const SLongInt &) const >(_SC("_sub"), &SLongInt::operator -)
.Func< SLongInt (SLongInt::*)(const SLongInt &) const >(_SC("_mul"), &SLongInt::operator *)
.Func< SLongInt (SLongInt::*)(const SLongInt &) const >(_SC("_div"), &SLongInt::operator /)
.Func< SLongInt (SLongInt::*)(const SLongInt &) const >(_SC("_modulo"), &SLongInt::operator %)
.Func< SLongInt (SLongInt::*)(void) const >(_SC("_unm"), &SLongInt::operator -)
/* Overloads */
.Overload< void (SLongInt::*)(void) >(_SC("random"), &SLongInt::Random)
.Overload< void (SLongInt::*)(SLongInt::Type, SLongInt::Type) >(_SC("random"), &SLongInt::Random)
);
Sqrat::RootTable(vm).Bind(_SC("ULongInt"), Sqrat::Class<ULongInt>(vm, _SC("ULongInt"))
// Attempt to register the specified type
Sqrat::RootTable(vm).Bind(_SC("ULongInt"), Sqrat::Class< ULongInt >(vm, _SC("ULongInt"))
/* Constructors */
.Ctor()
.Ctor(ULongInt::Value)
.Ctor(const SQChar *, SQInteger)
.Ctor< ULongInt::Type >()
.Ctor< const SQChar *, SQInteger >()
/* Properties */
.Prop(_SC("str"), &ULongInt::GetCStr, &ULongInt::SetStr)
.Prop(_SC("num"), &ULongInt::GetSNum, &ULongInt::SetNum)
/* Metamethods */
.Func(_SC("_tostring"), &ULongInt::ToString)
.Func(_SC("_cmp"), &ULongInt::Cmp)
.Func<ULongInt (ULongInt::*)(const ULongInt &) const>(_SC("_add"), &ULongInt::operator +)
.Func<ULongInt (ULongInt::*)(const ULongInt &) const>(_SC("_sub"), &ULongInt::operator -)
.Func<ULongInt (ULongInt::*)(const ULongInt &) const>(_SC("_mul"), &ULongInt::operator *)
.Func<ULongInt (ULongInt::*)(const ULongInt &) const>(_SC("_div"), &ULongInt::operator /)
.Func<ULongInt (ULongInt::*)(const ULongInt &) const>(_SC("_modulo"), &ULongInt::operator %)
.Func<ULongInt (ULongInt::*)(void) const>(_SC("_unm"), &ULongInt::operator -)
.Overload<void (ULongInt::*)(void)>(_SC("random"), &ULongInt::Random)
.Overload<void (ULongInt::*)(ULongInt::Value, ULongInt::Value)>(_SC("random"), &ULongInt::Random)
.Func< ULongInt (ULongInt::*)(const ULongInt &) const >(_SC("_add"), &ULongInt::operator +)
.Func< ULongInt (ULongInt::*)(const ULongInt &) const >(_SC("_sub"), &ULongInt::operator -)
.Func< ULongInt (ULongInt::*)(const ULongInt &) const >(_SC("_mul"), &ULongInt::operator *)
.Func< ULongInt (ULongInt::*)(const ULongInt &) const >(_SC("_div"), &ULongInt::operator /)
.Func< ULongInt (ULongInt::*)(const ULongInt &) const >(_SC("_modulo"), &ULongInt::operator %)
.Func< ULongInt (ULongInt::*)(void) const >(_SC("_unm"), &ULongInt::operator -)
/* Overloads */
.Overload< void (ULongInt::*)(void) >(_SC("random"), &ULongInt::Random)
.Overload< void (ULongInt::*)(ULongInt::Type, ULongInt::Type) >(_SC("random"), &ULongInt::Random)
);
*/
// Output debugging information
LogDbg("Registration of <IRCSession> type was successful");
// Registration succeeded
return true;
}

View File

@ -11,90 +11,117 @@
namespace SqMod {
// ------------------------------------------------------------------------------------------------
template <typename T> class LongInt
template < typename T > class LongInt
{
public:
// --------------------------------------------------------------------------------------------
static_assert(std::is_integral<T>::value, "LongInt type is not an integral type");
static_assert(std::is_integral< T >::value, "LongInt type is not an integral type");
// --------------------------------------------------------------------------------------------
typedef T Value;
typedef T Type;
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* ...
*/
LongInt()
: m_Data(0), m_Text()
{
}
template <typename U>
LongInt(U data)
/* --------------------------------------------------------------------------------------------
* ...
*/
template <typename U> LongInt(U data)
{
*this = data;
}
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* ...
*/
LongInt(const SQChar * text)
{
*this = text;
}
LongInt(const SQChar * text, SQInteger overload = 0)
/* --------------------------------------------------------------------------------------------
* ...
*/
LongInt(const SQChar * text, SQInteger overload)
{
SQMOD_UNUSED_VAR(overload);
*this = text;
}
// --------------------------------------------------------------------------------------------
LongInt(const LongInt<T> & i)
: m_Data(i.m_Data), m_Text(i.m_Text)
/* --------------------------------------------------------------------------------------------
* ...
*/
LongInt(const LongInt<T> & o)
: m_Data(o.m_Data), m_Text(o.m_Text)
{
}
LongInt(LongInt<T> && i)
: m_Data(i.m_Data), m_Text(std::move(i.m_Text))
/* --------------------------------------------------------------------------------------------
* ...
*/
LongInt(LongInt<T> && o)
: m_Data(o.m_Data), m_Text(std::move(o.m_Text))
{
}
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* ...
*/
~LongInt()
{
}
// --------------------------------------------------------------------------------------------
LongInt & operator = (const LongInt<T> & i)
/* --------------------------------------------------------------------------------------------
* ...
*/
LongInt & operator = (const LongInt<T> & o)
{
m_Data = i.m_Data;
m_Text = i.m_Text;
m_Data = o.m_Data;
m_Text = o.m_Text;
return *this;
}
LongInt & operator = (LongInt<T> && i)
/* --------------------------------------------------------------------------------------------
* ...
*/
LongInt & operator = (LongInt<T> && o)
{
m_Data = i.m_Data;
m_Text = std::move(i.m_Text);
m_Data = o.m_Data;
m_Text = std::move(o.m_Text);
return *this;
}
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* ...
*/
template <typename U, typename std::enable_if<std::is_integral<U>::value>::type* = nullptr>
LongInt & operator = (U data)
{
m_Data = static_cast<Value>(data);
m_Data = static_cast< Type >(data);
m_Text = std::to_string(m_Data);
return *this;
}
/* --------------------------------------------------------------------------------------------
* ...
*/
LongInt & operator = (const SQChar * text)
{
m_Text = text;
try
{
m_Data = SToI<T>(text, 0, 10);
m_Data = SToI< T >::Fn(text, 0, 10);
}
catch (const std::invalid_argument & e)
{
@ -103,125 +130,197 @@ public:
return *this;
}
// --------------------------------------------------------------------------------------------
bool operator == (const LongInt<T> & x) const
/* --------------------------------------------------------------------------------------------
* ...
*/
bool operator == (const LongInt<T> & o) const
{
return (m_Data == x.m_Data);
return (m_Data == o.m_Data);
}
bool operator != (const LongInt<T> & x) const
/* --------------------------------------------------------------------------------------------
* ...
*/
bool operator != (const LongInt<T> & o) const
{
return (m_Data != x.m_Data);
return (m_Data != o.m_Data);
}
bool operator < (const LongInt<T> & x) const
/* --------------------------------------------------------------------------------------------
* ...
*/
bool operator < (const LongInt<T> & o) const
{
return (m_Data < x.m_Data);
return (m_Data < o.m_Data);
}
bool operator > (const LongInt<T> & x) const
/* --------------------------------------------------------------------------------------------
* ...
*/
bool operator > (const LongInt<T> & o) const
{
return (m_Data > x.m_Data);
return (m_Data > o.m_Data);
}
bool operator <= (const LongInt<T> & x) const
/* --------------------------------------------------------------------------------------------
* ...
*/
bool operator <= (const LongInt<T> & o) const
{
return (m_Data <= x.m_Data);
return (m_Data <= o.m_Data);
}
bool operator >= (const LongInt<T> & x) const
/* --------------------------------------------------------------------------------------------
* ...
*/
bool operator >= (const LongInt<T> & o) const
{
return (m_Data >= x.m_Data);
return (m_Data >= o.m_Data);
}
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* ...
*/
inline operator T () const
{
return m_Data;
}
// --------------------------------------------------------------------------------------------
LongInt<T> operator + (const LongInt<T> & x) const
/* --------------------------------------------------------------------------------------------
* ...
*/
LongInt<T> operator + (const LongInt<T> & o) const
{
return LongInt<T>(m_Data + x.m_Data);
return LongInt<T>(m_Data + o.m_Data);
}
LongInt<T> operator - (const LongInt<T> & x) const
/* --------------------------------------------------------------------------------------------
* ...
*/
LongInt<T> operator - (const LongInt<T> & o) const
{
return LongInt<T>(m_Data - x.m_Data);
return LongInt<T>(m_Data - o.m_Data);
}
LongInt<T> operator * (const LongInt<T> & x) const
/* --------------------------------------------------------------------------------------------
* ...
*/
LongInt<T> operator * (const LongInt<T> & o) const
{
return LongInt<T>(m_Data * x.m_Data);
return LongInt<T>(m_Data * o.m_Data);
}
LongInt<T> operator / (const LongInt<T> & x) const
/* --------------------------------------------------------------------------------------------
* ...
*/
LongInt<T> operator / (const LongInt<T> & o) const
{
return LongInt<T>(m_Data / x.m_Data);
return LongInt<T>(m_Data / o.m_Data);
}
LongInt<T> operator % (const LongInt<T> & x) const
/* --------------------------------------------------------------------------------------------
* ...
*/
LongInt<T> operator % (const LongInt<T> & o) const
{
return LongInt<T>(m_Data % x.m_Data);
return LongInt<T>(m_Data % o.m_Data);
}
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* ...
*/
LongInt<T> operator - () const
{
return LongInt<T>(-m_Data);
}
// --------------------------------------------------------------------------------------------
SQInteger Cmp(const LongInt<T> & x) const
/* --------------------------------------------------------------------------------------------
* ...
*/
SQInteger Cmp(const LongInt<T> & o) const
{
return m_Data == x.m_Data ? 0 : (m_Data > x.m_Data ? 1 : -1);
if (m_Data == o.m_Data)
{
return 0;
}
else if (m_Data > o.m_Data)
{
return 1;
}
else
{
return -1;
}
}
/* --------------------------------------------------------------------------------------------
* ...
*/
const SQChar * ToString() const
{
return m_Text.c_str();
}
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* ...
*/
void SetNum(T data)
{
*this = data;
}
/* --------------------------------------------------------------------------------------------
* ...
*/
T GetNum() const
{
return m_Data;
}
/* --------------------------------------------------------------------------------------------
* ...
*/
SQInteger GetSNum() const
{
return static_cast<SQInteger>(m_Data);
return static_cast< SQInteger >(m_Data);
}
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* ...
*/
void SetStr(const SQChar * text)
{
*this = text;
}
/* --------------------------------------------------------------------------------------------
* ...
*/
const String & GetStr() const
{
return m_Text;
}
/* --------------------------------------------------------------------------------------------
* ...
*/
const SQChar * GetCStr() const
{
return m_Text.c_str();
}
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* ...
*/
void Random()
{
m_Data = RandomVal<T>::Get();
m_Text = std::to_string(m_Data);
}
/* --------------------------------------------------------------------------------------------
* ...
*/
void Random(T min, T max)
{
m_Data = RandomVal<T>::Get(min, max);
@ -229,14 +328,21 @@ public:
}
private:
// --------------------------------------------------------------------------------------------
/* --------------------------------------------------------------------------------------------
* ...
*/
T m_Data;
/* --------------------------------------------------------------------------------------------
* ...
*/
String m_Text;
};
// ------------------------------------------------------------------------------------------------
typedef LongInt<Int64> SLongInt;
typedef LongInt<Uint64> ULongInt;
typedef LongInt< Int64 > SLongInt;
typedef LongInt< Uint64 > ULongInt;
} // Namespace:: SqMod