From 4b5718a6aeb1d53ededcf6fe36b0c85454a82deb Mon Sep 17 00:00:00 2001 From: Sandu Liviu Catalin Date: Tue, 3 Nov 2015 04:21:46 +0200 Subject: [PATCH] Added an untested implementation a 64bit integer wrapper for 32bit module. --- source/Library/LongInt.cpp | 75 ++++++------ source/Library/LongInt.hpp | 226 +++++++++++++++++++++++++++---------- 2 files changed, 204 insertions(+), 97 deletions(-) diff --git a/source/Library/LongInt.cpp b/source/Library/LongInt.cpp index 6c89012c..07ed7644 100644 --- a/source/Library/LongInt.cpp +++ b/source/Library/LongInt.cpp @@ -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(vm, _SC("SLongInt")) + // Output debugging information + LogDbg("Beginning registration of 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(_SC("_add"), &SLongInt::operator +) - .Func(_SC("_sub"), &SLongInt::operator -) - .Func(_SC("_mul"), &SLongInt::operator *) - .Func(_SC("_div"), &SLongInt::operator /) - .Func(_SC("_modulo"), &SLongInt::operator %) - - .Func(_SC("_unm"), &SLongInt::operator -) - - .Overload(_SC("random"), &SLongInt::Random) - .Overload(_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(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(_SC("_add"), &ULongInt::operator +) - .Func(_SC("_sub"), &ULongInt::operator -) - .Func(_SC("_mul"), &ULongInt::operator *) - .Func(_SC("_div"), &ULongInt::operator /) - .Func(_SC("_modulo"), &ULongInt::operator %) - - .Func(_SC("_unm"), &ULongInt::operator -) - - .Overload(_SC("random"), &ULongInt::Random) - .Overload(_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 type was successful"); + // Registration succeeded return true; } diff --git a/source/Library/LongInt.hpp b/source/Library/LongInt.hpp index 69b40c5b..7006c1c0 100644 --- a/source/Library/LongInt.hpp +++ b/source/Library/LongInt.hpp @@ -11,90 +11,117 @@ namespace SqMod { // ------------------------------------------------------------------------------------------------ -template class LongInt +template < typename T > class LongInt { public: // -------------------------------------------------------------------------------------------- - static_assert(std::is_integral::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 - LongInt(U data) + /* -------------------------------------------------------------------------------------------- + * ... + */ + template 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 & i) - : m_Data(i.m_Data), m_Text(i.m_Text) + /* -------------------------------------------------------------------------------------------- + * ... + */ + LongInt(const LongInt & o) + : m_Data(o.m_Data), m_Text(o.m_Text) { } - LongInt(LongInt && i) - : m_Data(i.m_Data), m_Text(std::move(i.m_Text)) + /* -------------------------------------------------------------------------------------------- + * ... + */ + LongInt(LongInt && o) + : m_Data(o.m_Data), m_Text(std::move(o.m_Text)) { } - // -------------------------------------------------------------------------------------------- + /* -------------------------------------------------------------------------------------------- + * ... + */ ~LongInt() { } - // -------------------------------------------------------------------------------------------- - LongInt & operator = (const LongInt & i) + /* -------------------------------------------------------------------------------------------- + * ... + */ + LongInt & operator = (const LongInt & 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 && i) + /* -------------------------------------------------------------------------------------------- + * ... + */ + LongInt & operator = (LongInt && 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 ::value>::type* = nullptr> LongInt & operator = (U data) { - m_Data = static_cast(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(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 & x) const + /* -------------------------------------------------------------------------------------------- + * ... + */ + bool operator == (const LongInt & o) const { - return (m_Data == x.m_Data); + return (m_Data == o.m_Data); } - bool operator != (const LongInt & x) const + /* -------------------------------------------------------------------------------------------- + * ... + */ + bool operator != (const LongInt & o) const { - return (m_Data != x.m_Data); + return (m_Data != o.m_Data); } - bool operator < (const LongInt & x) const + /* -------------------------------------------------------------------------------------------- + * ... + */ + bool operator < (const LongInt & o) const { - return (m_Data < x.m_Data); + return (m_Data < o.m_Data); } - bool operator > (const LongInt & x) const + /* -------------------------------------------------------------------------------------------- + * ... + */ + bool operator > (const LongInt & o) const { - return (m_Data > x.m_Data); + return (m_Data > o.m_Data); } - bool operator <= (const LongInt & x) const + /* -------------------------------------------------------------------------------------------- + * ... + */ + bool operator <= (const LongInt & o) const { - return (m_Data <= x.m_Data); + return (m_Data <= o.m_Data); } - bool operator >= (const LongInt & x) const + /* -------------------------------------------------------------------------------------------- + * ... + */ + bool operator >= (const LongInt & o) const { - return (m_Data >= x.m_Data); + return (m_Data >= o.m_Data); } - // -------------------------------------------------------------------------------------------- + /* -------------------------------------------------------------------------------------------- + * ... + */ inline operator T () const { return m_Data; } - // -------------------------------------------------------------------------------------------- - LongInt operator + (const LongInt & x) const + /* -------------------------------------------------------------------------------------------- + * ... + */ + LongInt operator + (const LongInt & o) const { - return LongInt(m_Data + x.m_Data); + return LongInt(m_Data + o.m_Data); } - LongInt operator - (const LongInt & x) const + /* -------------------------------------------------------------------------------------------- + * ... + */ + LongInt operator - (const LongInt & o) const { - return LongInt(m_Data - x.m_Data); + return LongInt(m_Data - o.m_Data); } - LongInt operator * (const LongInt & x) const + /* -------------------------------------------------------------------------------------------- + * ... + */ + LongInt operator * (const LongInt & o) const { - return LongInt(m_Data * x.m_Data); + return LongInt(m_Data * o.m_Data); } - LongInt operator / (const LongInt & x) const + /* -------------------------------------------------------------------------------------------- + * ... + */ + LongInt operator / (const LongInt & o) const { - return LongInt(m_Data / x.m_Data); + return LongInt(m_Data / o.m_Data); } - LongInt operator % (const LongInt & x) const + /* -------------------------------------------------------------------------------------------- + * ... + */ + LongInt operator % (const LongInt & o) const { - return LongInt(m_Data % x.m_Data); + return LongInt(m_Data % o.m_Data); } - // -------------------------------------------------------------------------------------------- + /* -------------------------------------------------------------------------------------------- + * ... + */ LongInt operator - () const { return LongInt(-m_Data); } - // -------------------------------------------------------------------------------------------- - SQInteger Cmp(const LongInt & x) const + /* -------------------------------------------------------------------------------------------- + * ... + */ + SQInteger Cmp(const LongInt & 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(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::Get(); m_Text = std::to_string(m_Data); } + /* -------------------------------------------------------------------------------------------- + * ... + */ void Random(T min, T max) { m_Data = RandomVal::Get(min, max); @@ -229,14 +328,21 @@ public: } private: - // -------------------------------------------------------------------------------------------- + + /* -------------------------------------------------------------------------------------------- + * ... + */ T m_Data; + + /* -------------------------------------------------------------------------------------------- + * ... + */ String m_Text; }; // ------------------------------------------------------------------------------------------------ -typedef LongInt SLongInt; -typedef LongInt ULongInt; +typedef LongInt< Int64 > SLongInt; +typedef LongInt< Uint64 > ULongInt; } // Namespace:: SqMod