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

View File

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