diff --git a/cbp/Module.cbp b/cbp/Module.cbp index 24addd8a..27bb62f5 100644 --- a/cbp/Module.cbp +++ b/cbp/Module.cbp @@ -505,12 +505,14 @@ - - - - + + + + + + diff --git a/shared/Base/Utility.cpp b/shared/Base/Utility.cpp index 21fcd6ba..7fa12d65 100644 --- a/shared/Base/Utility.cpp +++ b/shared/Base/Utility.cpp @@ -17,7 +17,7 @@ // ------------------------------------------------------------------------------------------------ #ifndef SQMOD_PLUGIN_API - #include "Library/Numeric.hpp" + #include "Library/Numeric/LongInt.hpp" #include #endif // SQMOD_PLUGIN_API diff --git a/source/Base/Shared.cpp b/source/Base/Shared.cpp index 018bda78..2a2f75b1 100644 --- a/source/Base/Shared.cpp +++ b/source/Base/Shared.cpp @@ -2,9 +2,9 @@ #include "Base/Shared.hpp" #include "Base/Buffer.hpp" #include "Base/Color3.hpp" -#include "Library/Random.hpp" +#include "Library/Numeric/Random.hpp" +#include "Library/Numeric/LongInt.hpp" #include "Library/String.hpp" -#include "Library/Numeric.hpp" // ------------------------------------------------------------------------------------------------ #include diff --git a/source/Exports.cpp b/source/Exports.cpp index 4845c8df..2779ebf1 100644 --- a/source/Exports.cpp +++ b/source/Exports.cpp @@ -2,7 +2,7 @@ #include "Core.hpp" // ------------------------------------------------------------------------------------------------ -#include "Library/Numeric.hpp" +#include "Library/Numeric/LongInt.hpp" #include "Library/Chrono/Date.hpp" #include "Library/Chrono/Time.hpp" #include "Library/Chrono/Datetime.hpp" diff --git a/source/Library/Chrono.cpp b/source/Library/Chrono.cpp index 147ef332..407892cb 100644 --- a/source/Library/Chrono.cpp +++ b/source/Library/Chrono.cpp @@ -5,7 +5,7 @@ #include "Library/Chrono/Time.hpp" #include "Library/Chrono/Timer.hpp" #include "Library/Chrono/Timestamp.hpp" -#include "Library/Numeric.hpp" +#include "Library/Numeric/LongInt.hpp" #include "Base/Shared.hpp" // ------------------------------------------------------------------------------------------------ diff --git a/source/Library/Chrono/Timestamp.cpp b/source/Library/Chrono/Timestamp.cpp index e85dcb43..42380af0 100644 --- a/source/Library/Chrono/Timestamp.cpp +++ b/source/Library/Chrono/Timestamp.cpp @@ -4,7 +4,7 @@ #include "Library/Chrono/Date.hpp" #include "Library/Chrono/Time.hpp" #include "Library/Chrono/Datetime.hpp" -#include "Library/Numeric.hpp" +#include "Library/Numeric/LongInt.hpp" // ------------------------------------------------------------------------------------------------ namespace SqMod { diff --git a/source/Library/Numeric.cpp b/source/Library/Numeric.cpp index 00b44920..ad28bfb8 100644 --- a/source/Library/Numeric.cpp +++ b/source/Library/Numeric.cpp @@ -1,159 +1,20 @@ // ------------------------------------------------------------------------------------------------ #include "Library/Numeric.hpp" -#include "Library/Random.hpp" - -// ------------------------------------------------------------------------------------------------ -#include -#include -#include // ------------------------------------------------------------------------------------------------ namespace SqMod { // ------------------------------------------------------------------------------------------------ -LongInt< Int64 >::LongInt(CSStr text) : m_Data(0), m_Text() -{ m_Data = strtoll(text, NULL, 10); } -LongInt< Int64 >::LongInt(CSStr text, SQInteger fall) : m_Data(0), m_Text() -{ - m_Data = strtoll(text, NULL, 10); - if (errno == ERANGE) - { - m_Data = ConvTo< Type >::From(fall); - } -} - -// ------------------------------------------------------------------------------------------------ -LongInt< Int64 > & LongInt< Int64 >::operator = (CSStr text) -{ - m_Data = strtoll(text, NULL, 10); - return *this; -} - -// ------------------------------------------------------------------------------------------------ -CSStr LongInt< Int64 >::ToString() -{ - if (snprintf(m_Text, sizeof(m_Text), "%llu", m_Data) < 0) - { - m_Text[0] = 0; - } - - return m_Text; -} - -// ------------------------------------------------------------------------------------------------ -void LongInt< Int64 >::Random() { m_Data = GetRandomInt64(); } -void LongInt< Int64 >::Random(Type n) { m_Data = GetRandomInt64(n); } -void LongInt< Int64 >::Random(Type m, Type n) { m_Data = GetRandomInt64(m, n); } - -// ------------------------------------------------------------------------------------------------ -LongInt< Uint64 >::LongInt(CSStr text) : m_Data(0), m_Text() -{ m_Data = strtoll(text, NULL, 10); } -LongInt< Uint64 >::LongInt(CSStr text, SQInteger fall) : m_Data(0), m_Text() -{ - m_Data = strtoull(text, NULL, 10); - if (errno == ERANGE) - { - m_Data = ConvTo< Type >::From(fall); - } -} - -// ------------------------------------------------------------------------------------------------ -LongInt< Uint64 > & LongInt< Uint64 >::operator = (CSStr text) -{ - m_Data = strtoull(text, NULL, 10); - return *this; -} - -// ------------------------------------------------------------------------------------------------ -CSStr LongInt< Uint64 >::ToString() -{ - if (std::snprintf(m_Text, sizeof(m_Text), "%llu", m_Data) < 0) - { - m_Text[0] = 0; - } - - return m_Text; -} - -// ------------------------------------------------------------------------------------------------ -void LongInt< Uint64 >::Random() { m_Data = GetRandomUint64(); } -void LongInt< Uint64 >::Random(Type n) { m_Data = GetRandomUint64(n); } -void LongInt< Uint64 >::Random(Type m, Type n) { m_Data = GetRandomUint64(m, n); } +extern void Register_LongInt(HSQUIRRELVM vm); +extern void Register_Math(HSQUIRRELVM vm); +extern void Register_Random(HSQUIRRELVM vm); // ================================================================================================ void Register_Numeric(HSQUIRRELVM vm) { - RootTable(vm).Bind(_SC("SLongInt"), Class< SLongInt >(vm, _SC("SLongInt")) - /* Constructors */ - .Ctor() - .Ctor< SLongInt::Type >() - .template Ctor< CCStr, SQInteger >() - /* Properties */ - .Prop(_SC("Str"), &SLongInt::GetCStr, &SLongInt::SetStr) - .Prop(_SC("Num"), &SLongInt::GetSNum, &SLongInt::SetNum) - /* Core Meta-methods */ - .Func(_SC("_tostring"), &SLongInt::ToString) - .Func(_SC("_typename"), &SLongInt::Typename) - .Func(_SC("_cmp"), &SLongInt::Cmp) - /* Core Functions */ - .Func(_SC("tointeger"), &SLongInt::ToSqInteger) - .Func(_SC("tofloat"), &SLongInt::ToSqFloat) - .Func(_SC("tostring"), &SLongInt::ToSqString) - .Func(_SC("tobool"), &SLongInt::ToSqBool) - .Func(_SC("tochar"), &SLongInt::ToSqChar) - /* Meta-methods */ - .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 -) - /* Functions */ - .Func(_SC("GetStr"), &SLongInt::GetCStr) - .Func(_SC("SetStr"), &SLongInt::SetStr) - .Func(_SC("GetNum"), &SLongInt::GetSNum) - .Func(_SC("SetNum"), &SLongInt::SetNum) - /* Overloads */ - .Overload< void (SLongInt::*)(void) >(_SC("Random"), &SLongInt::Random) - .Overload< void (SLongInt::*)(SLongInt::Type) >(_SC("Random"), &SLongInt::Random) - .Overload< void (SLongInt::*)(SLongInt::Type, SLongInt::Type) >(_SC("Random"), &SLongInt::Random) - ); - - RootTable(vm).Bind(_SC("ULongInt"), Class< ULongInt >(vm, _SC("ULongInt")) - /* Constructors */ - .Ctor() - .Ctor< ULongInt::Type >() - .Ctor< CCStr, SQInteger >() - /* Properties */ - .Prop(_SC("Str"), &ULongInt::GetCStr, &ULongInt::SetStr) - .Prop(_SC("Num"), &ULongInt::GetSNum, &ULongInt::SetNum) - /* Core Meta-methods */ - .Func(_SC("_tostring"), &ULongInt::ToString) - .Func(_SC("_typename"), &ULongInt::Typename) - .Func(_SC("_cmp"), &ULongInt::Cmp) - /* Core Functions */ - .Func(_SC("tointeger"), &ULongInt::ToSqInteger) - .Func(_SC("tofloat"), &ULongInt::ToSqFloat) - .Func(_SC("tostring"), &ULongInt::ToSqString) - .Func(_SC("tobool"), &ULongInt::ToSqBool) - .Func(_SC("tochar"), &ULongInt::ToSqChar) - /* Meta-methods */ - .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 -) - /* Functions */ - .Func(_SC("GetStr"), &ULongInt::GetCStr) - .Func(_SC("SetStr"), &ULongInt::SetStr) - .Func(_SC("GetNum"), &ULongInt::GetSNum) - .Func(_SC("SetNum"), &ULongInt::SetNum) - /* Overloads */ - .Overload< void (ULongInt::*)(void) >(_SC("Random"), &ULongInt::Random) - .Overload< void (ULongInt::*)(ULongInt::Type) >(_SC("Random"), &ULongInt::Random) - .Overload< void (ULongInt::*)(ULongInt::Type, ULongInt::Type) >(_SC("Random"), &ULongInt::Random) - ); + Register_LongInt(vm); + Register_Math(vm); + Register_Random(vm); } } // Namespace:: SqMod diff --git a/source/Library/Numeric.hpp b/source/Library/Numeric.hpp index f3286b61..6e80eafb 100644 --- a/source/Library/Numeric.hpp +++ b/source/Library/Numeric.hpp @@ -2,535 +2,12 @@ #define _LIBRARY_NUMERIC_HPP_ // ------------------------------------------------------------------------------------------------ -#include "Base/Shared.hpp" +#include "SqBase.hpp" // ------------------------------------------------------------------------------------------------ namespace SqMod { -// ------------------------------------------------------------------------------------------------ -template < typename T > class LongInt; -// ------------------------------------------------------------------------------------------------ -template <> class LongInt< Int64 > -{ -public: - - // -------------------------------------------------------------------------------------------- - typedef Int64 Type; - - /* -------------------------------------------------------------------------------------------- - * - */ - LongInt() - : m_Data(0), m_Text() - { - /* ... */ - } - - LongInt(Type n) - : m_Data(n), m_Text() - { - /* ... */ - } - - /* -------------------------------------------------------------------------------------------- - * - */ - LongInt(CSStr text); - LongInt(CSStr text, SQInteger fall); - - /* -------------------------------------------------------------------------------------------- - * - */ - LongInt(const LongInt< Type > & o) - : m_Data(o.m_Data), m_Text() - { - /* ... */ - } - - /* -------------------------------------------------------------------------------------------- - * - */ - ~LongInt() - { - /* ... */ - } - - /* -------------------------------------------------------------------------------------------- - * - */ - LongInt & operator = (const LongInt< Type > & o) - { - m_Data = o.m_Data; - return *this; - } - - /* -------------------------------------------------------------------------------------------- - * - */ - LongInt< Type > & operator = (Type data) - { - m_Data = data; - return *this; - } - - /* -------------------------------------------------------------------------------------------- - * - */ - LongInt< Type > & operator = (CSStr text); - - /* -------------------------------------------------------------------------------------------- - * - */ - bool operator == (const LongInt< Type > & o) const - { - return (m_Data == o.m_Data); - } - - bool operator != (const LongInt< Type > & o) const - { - return (m_Data != o.m_Data); - } - - bool operator < (const LongInt< Type > & o) const - { - return (m_Data < o.m_Data); - } - - bool operator > (const LongInt< Type > & o) const - { - return (m_Data > o.m_Data); - } - - bool operator <= (const LongInt< Type > & o) const - { - return (m_Data <= o.m_Data); - } - - bool operator >= (const LongInt< Type > & o) const - { - return (m_Data >= o.m_Data); - } - - - /* -------------------------------------------------------------------------------------------- - * - */ - operator Type () const { return m_Data; } - - /* -------------------------------------------------------------------------------------------- - * - */ - LongInt< Type > operator + (const LongInt< Type > & o) const - { - return LongInt< Type >(m_Data + o.m_Data); - } - - LongInt< Type > operator - (const LongInt< Type > & o) const - { - return LongInt< Type >(m_Data - o.m_Data); - } - - LongInt< Type > operator * (const LongInt< Type > & o) const - { - return LongInt< Type >(m_Data * o.m_Data); - } - - LongInt< Type > operator / (const LongInt< Type > & o) const - { - return LongInt< Type >(m_Data / o.m_Data); - } - - LongInt< Type > operator % (const LongInt< Type > & o) const - { - return LongInt< Type >(m_Data % o.m_Data); - } - - - /* -------------------------------------------------------------------------------------------- - * - */ - LongInt< Type > operator - () const - { - return LongInt< Type >(-m_Data); - } - - /* -------------------------------------------------------------------------------------------- - * - */ - Int32 Cmp(const LongInt< Type > & o) const - { - if (m_Data == o.m_Data) - return 0; - else if (m_Data > o.m_Data) - return 1; - else - return -1; - } - - /* -------------------------------------------------------------------------------------------- - * - */ - CSStr ToString(); - - /* -------------------------------------------------------------------------------------------- - * - */ - CSStr Typename() const - { - return _SC("SLongInt"); - } - - /* -------------------------------------------------------------------------------------------- - * - */ - void SetNum(Type data) - { - m_Data = data; - } - - Type GetNum() const - { - return m_Data; - } - - /* -------------------------------------------------------------------------------------------- - * - */ - SQInteger GetSNum() const - { - return ClampL< Type, SQInteger >(m_Data); - } - - /* -------------------------------------------------------------------------------------------- - * - */ - void SetStr(CSStr text) - { - *this = text; - } - - CSStr GetCStr() - { - return ToString(); - } - - /* -------------------------------------------------------------------------------------------- - * - */ - void Random(); - void Random(Type n); - void Random(Type m, Type n); - - /* -------------------------------------------------------------------------------------------- - * Attempt to convert the long integer to a squirrel integer. - */ - SQInteger ToSqInteger() const - { - return ClampL< Type, SQInteger >(m_Data); - } - - /* -------------------------------------------------------------------------------------------- - * Attempt to convert the long integer to a squirrel float. - */ - SQFloat ToSqFloat() const - { - return ClampL< Float64, SQFloat >(static_cast< Float64 >(m_Data)); - } - - /* -------------------------------------------------------------------------------------------- - * Attempt to convert the long integer to a squirrel string. - */ - CSStr ToSqString() - { - return ToString(); - } - - /* -------------------------------------------------------------------------------------------- - * Attempt to convert the long integer to a squirrel boolean. - */ - bool ToSqBool() const - { - return (m_Data > 0); - } - - /* -------------------------------------------------------------------------------------------- - * Attempt to convert the long integer to a squirrel character. - */ - SQChar ToSqChar() const - { - return ClampL< Type, SQChar >(m_Data); - } - -private: - - // -------------------------------------------------------------------------------------------- - Type m_Data; - SQChar m_Text[32]; -}; - -// ------------------------------------------------------------------------------------------------ -template <> class LongInt< Uint64 > -{ -public: - - // -------------------------------------------------------------------------------------------- - typedef Uint64 Type; - - /* -------------------------------------------------------------------------------------------- - * - */ - LongInt() - : m_Data(0), m_Text() - { - /* ... */ - } - - LongInt(Type n) - : m_Data(n), m_Text() - { - /* ... */ - } - - /* -------------------------------------------------------------------------------------------- - * - */ - LongInt(CSStr text); - LongInt(CSStr text, SQInteger fall); - - /* -------------------------------------------------------------------------------------------- - * - */ - LongInt(const LongInt< Type > & o) - : m_Data(o.m_Data), m_Text() - { - /* ... */ - } - - /* -------------------------------------------------------------------------------------------- - * - */ - ~LongInt() - { - /* ... */ - } - - /* -------------------------------------------------------------------------------------------- - * - */ - LongInt & operator = (const LongInt< Type > & o) - { - m_Data = o.m_Data; - return *this; - } - - /* -------------------------------------------------------------------------------------------- - * - */ - LongInt< Type > & operator = (Type data) - { - m_Data = data; - return *this; - } - - /* -------------------------------------------------------------------------------------------- - * - */ - LongInt< Type > & operator = (CSStr text); - - /* -------------------------------------------------------------------------------------------- - * - */ - bool operator == (const LongInt< Type > & o) const - { - return (m_Data == o.m_Data); - } - - bool operator != (const LongInt< Type > & o) const - { - return (m_Data != o.m_Data); - } - - bool operator < (const LongInt< Type > & o) const - { - return (m_Data < o.m_Data); - } - - bool operator > (const LongInt< Type > & o) const - { - return (m_Data > o.m_Data); - } - - bool operator <= (const LongInt< Type > & o) const - { - return (m_Data <= o.m_Data); - } - - bool operator >= (const LongInt< Type > & o) const - { - return (m_Data >= o.m_Data); - } - - - /* -------------------------------------------------------------------------------------------- - * - */ - operator Type () const { return m_Data; } - - /* -------------------------------------------------------------------------------------------- - * - */ - LongInt< Type > operator + (const LongInt< Type > & o) const - { - return LongInt< Type >(m_Data + o.m_Data); - } - - LongInt< Type > operator - (const LongInt< Type > & o) const - { - return LongInt< Type >(m_Data - o.m_Data); - } - - LongInt< Type > operator * (const LongInt< Type > & o) const - { - return LongInt< Type >(m_Data * o.m_Data); - } - - LongInt< Type > operator / (const LongInt< Type > & o) const - { - return LongInt< Type >(m_Data / o.m_Data); - } - - LongInt< Type > operator % (const LongInt< Type > & o) const - { - return LongInt< Type >(m_Data % o.m_Data); - } - - - /* -------------------------------------------------------------------------------------------- - * - */ - LongInt< Type > operator - () const - { - return LongInt< Type >(-m_Data); - } - - /* -------------------------------------------------------------------------------------------- - * - */ - Int32 Cmp(const LongInt< Type > & o) const - { - if (m_Data == o.m_Data) - return 0; - else if (m_Data > o.m_Data) - return 1; - else - return -1; - } - - /* -------------------------------------------------------------------------------------------- - * - */ - CSStr ToString(); - - /* -------------------------------------------------------------------------------------------- - * - */ - CSStr Typename() const - { - return _SC("ULongInt"); - } - - /* -------------------------------------------------------------------------------------------- - * - */ - void SetNum(Type data) - { - m_Data = data; - } - - Type GetNum() const - { - return m_Data; - } - - /* -------------------------------------------------------------------------------------------- - * - */ - SQInteger GetSNum() const - { - return (SQInteger)(m_Data); - } - - /* -------------------------------------------------------------------------------------------- - * - */ - void SetStr(CSStr text) - { - *this = text; - } - - CSStr GetCStr() - { - return ToString(); - } - - /* -------------------------------------------------------------------------------------------- - * - */ - void Random(); - void Random(Type n); - void Random(Type m, Type n); - - /* -------------------------------------------------------------------------------------------- - * Attempt to convert the long integer to a squirrel integer. - */ - SQInteger ToSqInteger() const - { - return ClampL< Type, SQInteger >(m_Data); - } - - /* -------------------------------------------------------------------------------------------- - * Attempt to convert the long integer to a squirrel float. - */ - SQFloat ToSqFloat() const - { - return ClampL< Float64, SQFloat >(static_cast< Float64 >(m_Data)); - } - - /* -------------------------------------------------------------------------------------------- - * Attempt to convert the long integer to a squirrel string. - */ - CSStr ToSqString() - { - return ToString(); - } - - /* -------------------------------------------------------------------------------------------- - * Attempt to convert the long integer to a squirrel boolean. - */ - bool ToSqBool() const - { - return (m_Data > 0); - } - - /* -------------------------------------------------------------------------------------------- - * Attempt to convert the long integer to a squirrel character. - */ - SQChar ToSqChar() const - { - return ClampL< Type, SQChar >(m_Data); - } - -private: - - // -------------------------------------------------------------------------------------------- - Type m_Data; - SQChar m_Text[32]; -}; - -// ------------------------------------------------------------------------------------------------ -typedef LongInt< Int64 > SLongInt; -typedef LongInt< Uint64 > ULongInt; } // Namespace:: SqMod diff --git a/source/Library/Numeric/LongInt.cpp b/source/Library/Numeric/LongInt.cpp new file mode 100644 index 00000000..f980f65e --- /dev/null +++ b/source/Library/Numeric/LongInt.cpp @@ -0,0 +1,196 @@ +// ------------------------------------------------------------------------------------------------ +#include "Library/Numeric/LongInt.hpp" +#include "Library/Numeric/Random.hpp" +#include "Base/Shared.hpp" + +// ------------------------------------------------------------------------------------------------ +#include +#include +#include + +// ------------------------------------------------------------------------------------------------ +namespace SqMod { + +// ------------------------------------------------------------------------------------------------ +LongInt< Int64 >::LongInt(CSStr text) : m_Data(0), m_Text() +{ + m_Data = std::strtoll(text, nullptr, 10); +} + +// ------------------------------------------------------------------------------------------------ +LongInt< Int64 >::LongInt(CSStr text, SQInteger fall) : m_Data(0), m_Text() +{ + m_Data = std::strtoll(text, nullptr, 10); + // Simple, check for conversion errors + if (errno == ERANGE) + { + m_Data = ConvTo< Type >::From(fall); + } +} + +// ------------------------------------------------------------------------------------------------ +LongInt< Int64 > & LongInt< Int64 >::operator = (CSStr text) +{ + m_Data = std::strtoll(text, nullptr, 10); + return *this; +} + +// ------------------------------------------------------------------------------------------------ +CSStr LongInt< Int64 >::ToString() +{ + if (std::snprintf(m_Text, sizeof(m_Text), "%llu", m_Data) < 0) + { + m_Text[0] = 0; + } + + return m_Text; +} + +// ------------------------------------------------------------------------------------------------ +void LongInt< Int64 >::Random() +{ + m_Data = GetRandomInt64(); +} + +// ------------------------------------------------------------------------------------------------ +void LongInt< Int64 >::Random(Type n) +{ + m_Data = GetRandomInt64(n); +} + +// ------------------------------------------------------------------------------------------------ +void LongInt< Int64 >::Random(Type m, Type n) +{ + m_Data = GetRandomInt64(m, n); +} + +// ------------------------------------------------------------------------------------------------ +LongInt< Uint64 >::LongInt(CSStr text) : m_Data(0), m_Text() +{ + m_Data = std::strtoll(text, nullptr, 10); +} + +// ------------------------------------------------------------------------------------------------ +LongInt< Uint64 >::LongInt(CSStr text, SQInteger fall) : m_Data(0), m_Text() +{ + m_Data = std::strtoull(text, nullptr, 10); + // Simple, check for conversion errors + if (errno == ERANGE) + { + m_Data = ConvTo< Type >::From(fall); + } +} + +// ------------------------------------------------------------------------------------------------ +LongInt< Uint64 > & LongInt< Uint64 >::operator = (CSStr text) +{ + m_Data = std::strtoull(text, nullptr, 10); + return *this; +} + +// ------------------------------------------------------------------------------------------------ +CSStr LongInt< Uint64 >::ToString() +{ + if (std::snprintf(m_Text, sizeof(m_Text), "%llu", m_Data) < 0) + { + m_Text[0] = 0; + } + + return m_Text; +} + +// ------------------------------------------------------------------------------------------------ +void LongInt< Uint64 >::Random() +{ + m_Data = GetRandomUint64(); +} + +// ------------------------------------------------------------------------------------------------ +void LongInt< Uint64 >::Random(Type n) +{ + m_Data = GetRandomUint64(n); +} + +// ------------------------------------------------------------------------------------------------ +void LongInt< Uint64 >::Random(Type m, Type n) +{ + m_Data = GetRandomUint64(m, n); +} + +// ================================================================================================ +void Register_LongInt(HSQUIRRELVM vm) +{ + RootTable(vm).Bind(_SC("SLongInt"), Class< SLongInt >(vm, _SC("SLongInt")) + /* Constructors */ + .Ctor() + .Ctor< SLongInt::Type >() + .template Ctor< CCStr, SQInteger >() + /* Properties */ + .Prop(_SC("Str"), &SLongInt::GetCStr, &SLongInt::SetStr) + .Prop(_SC("Num"), &SLongInt::GetSNum, &SLongInt::SetNum) + /* Core Meta-methods */ + .Func(_SC("_tostring"), &SLongInt::ToString) + .Func(_SC("_typename"), &SLongInt::Typename) + .Func(_SC("_cmp"), &SLongInt::Cmp) + /* Core Functions */ + .Func(_SC("tointeger"), &SLongInt::ToSqInteger) + .Func(_SC("tofloat"), &SLongInt::ToSqFloat) + .Func(_SC("tostring"), &SLongInt::ToSqString) + .Func(_SC("tobool"), &SLongInt::ToSqBool) + .Func(_SC("tochar"), &SLongInt::ToSqChar) + /* Meta-methods */ + .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 -) + /* Functions */ + .Func(_SC("GetStr"), &SLongInt::GetCStr) + .Func(_SC("SetStr"), &SLongInt::SetStr) + .Func(_SC("GetNum"), &SLongInt::GetSNum) + .Func(_SC("SetNum"), &SLongInt::SetNum) + /* Overloads */ + .Overload< void (SLongInt::*)(void) >(_SC("Random"), &SLongInt::Random) + .Overload< void (SLongInt::*)(SLongInt::Type) >(_SC("Random"), &SLongInt::Random) + .Overload< void (SLongInt::*)(SLongInt::Type, SLongInt::Type) >(_SC("Random"), &SLongInt::Random) + ); + + RootTable(vm).Bind(_SC("ULongInt"), Class< ULongInt >(vm, _SC("ULongInt")) + /* Constructors */ + .Ctor() + .Ctor< ULongInt::Type >() + .Ctor< CCStr, SQInteger >() + /* Properties */ + .Prop(_SC("Str"), &ULongInt::GetCStr, &ULongInt::SetStr) + .Prop(_SC("Num"), &ULongInt::GetSNum, &ULongInt::SetNum) + /* Core Meta-methods */ + .Func(_SC("_tostring"), &ULongInt::ToString) + .Func(_SC("_typename"), &ULongInt::Typename) + .Func(_SC("_cmp"), &ULongInt::Cmp) + /* Core Functions */ + .Func(_SC("tointeger"), &ULongInt::ToSqInteger) + .Func(_SC("tofloat"), &ULongInt::ToSqFloat) + .Func(_SC("tostring"), &ULongInt::ToSqString) + .Func(_SC("tobool"), &ULongInt::ToSqBool) + .Func(_SC("tochar"), &ULongInt::ToSqChar) + /* Meta-methods */ + .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 -) + /* Functions */ + .Func(_SC("GetStr"), &ULongInt::GetCStr) + .Func(_SC("SetStr"), &ULongInt::SetStr) + .Func(_SC("GetNum"), &ULongInt::GetSNum) + .Func(_SC("SetNum"), &ULongInt::SetNum) + /* Overloads */ + .Overload< void (ULongInt::*)(void) >(_SC("Random"), &ULongInt::Random) + .Overload< void (ULongInt::*)(ULongInt::Type) >(_SC("Random"), &ULongInt::Random) + .Overload< void (ULongInt::*)(ULongInt::Type, ULongInt::Type) >(_SC("Random"), &ULongInt::Random) + ); +} + +} // Namespace:: SqMod diff --git a/source/Library/Numeric/LongInt.hpp b/source/Library/Numeric/LongInt.hpp new file mode 100644 index 00000000..0d4a779a --- /dev/null +++ b/source/Library/Numeric/LongInt.hpp @@ -0,0 +1,551 @@ +#ifndef _LIBRARY_NUMERIC_LONGINT_HPP_ +#define _LIBRARY_NUMERIC_LONGINT_HPP_ + +// ------------------------------------------------------------------------------------------------ +#include "Base/Shared.hpp" + +// ------------------------------------------------------------------------------------------------ +namespace SqMod { + +// ------------------------------------------------------------------------------------------------ +template < typename T > class LongInt; + +/* ------------------------------------------------------------------------------------------------ + * +*/ +template <> class LongInt< Int64 > +{ +public: + + // -------------------------------------------------------------------------------------------- + typedef Int64 Type; + +private: + + // -------------------------------------------------------------------------------------------- + Type m_Data; + SQChar m_Text[32]; + +public: + + /* -------------------------------------------------------------------------------------------- + * + */ + LongInt() + : m_Data(0), m_Text() + { + /* ... */ + } + + LongInt(Type n) + : m_Data(n), m_Text() + { + /* ... */ + } + + /* -------------------------------------------------------------------------------------------- + * + */ + LongInt(CSStr text); + LongInt(CSStr text, SQInteger fall); + + /* -------------------------------------------------------------------------------------------- + * + */ + LongInt(const LongInt< Type > & o) + : m_Data(o.m_Data), m_Text() + { + /* ... */ + } + + /* -------------------------------------------------------------------------------------------- + * + */ + ~LongInt() + { + /* ... */ + } + + /* -------------------------------------------------------------------------------------------- + * + */ + LongInt & operator = (const LongInt< Type > & o) + { + m_Data = o.m_Data; + return *this; + } + + /* -------------------------------------------------------------------------------------------- + * + */ + LongInt< Type > & operator = (Type data) + { + m_Data = data; + return *this; + } + + /* -------------------------------------------------------------------------------------------- + * + */ + LongInt< Type > & operator = (CSStr text); + + /* -------------------------------------------------------------------------------------------- + * + */ + bool operator == (const LongInt< Type > & o) const + { + return (m_Data == o.m_Data); + } + + bool operator != (const LongInt< Type > & o) const + { + return (m_Data != o.m_Data); + } + + bool operator < (const LongInt< Type > & o) const + { + return (m_Data < o.m_Data); + } + + bool operator > (const LongInt< Type > & o) const + { + return (m_Data > o.m_Data); + } + + bool operator <= (const LongInt< Type > & o) const + { + return (m_Data <= o.m_Data); + } + + bool operator >= (const LongInt< Type > & o) const + { + return (m_Data >= o.m_Data); + } + + + /* -------------------------------------------------------------------------------------------- + * + */ + operator Type () const { return m_Data; } + + /* -------------------------------------------------------------------------------------------- + * + */ + LongInt< Type > operator + (const LongInt< Type > & o) const + { + return LongInt< Type >(m_Data + o.m_Data); + } + + LongInt< Type > operator - (const LongInt< Type > & o) const + { + return LongInt< Type >(m_Data - o.m_Data); + } + + LongInt< Type > operator * (const LongInt< Type > & o) const + { + return LongInt< Type >(m_Data * o.m_Data); + } + + LongInt< Type > operator / (const LongInt< Type > & o) const + { + return LongInt< Type >(m_Data / o.m_Data); + } + + LongInt< Type > operator % (const LongInt< Type > & o) const + { + return LongInt< Type >(m_Data % o.m_Data); + } + + + /* -------------------------------------------------------------------------------------------- + * + */ + LongInt< Type > operator - () const + { + return LongInt< Type >(-m_Data); + } + + /* -------------------------------------------------------------------------------------------- + * + */ + Int32 Cmp(const LongInt< Type > & o) const + { + if (m_Data == o.m_Data) + return 0; + else if (m_Data > o.m_Data) + return 1; + else + return -1; + } + + /* -------------------------------------------------------------------------------------------- + * + */ + CSStr ToString(); + + /* -------------------------------------------------------------------------------------------- + * + */ + CSStr Typename() const + { + return _SC("SLongInt"); + } + + /* -------------------------------------------------------------------------------------------- + * + */ + void SetNum(Type data) + { + m_Data = data; + } + + Type GetNum() const + { + return m_Data; + } + + /* -------------------------------------------------------------------------------------------- + * + */ + SQInteger GetSNum() const + { + return ClampL< Type, SQInteger >(m_Data); + } + + /* -------------------------------------------------------------------------------------------- + * + */ + void SetStr(CSStr text) + { + *this = text; + } + + CSStr GetCStr() + { + return ToString(); + } + + /* -------------------------------------------------------------------------------------------- + * + */ + void Random(); + void Random(Type n); + void Random(Type m, Type n); + + /* -------------------------------------------------------------------------------------------- + * Attempt to convert the long integer to a squirrel integer. + */ + SQInteger ToSqInteger() const + { + return ClampL< Type, SQInteger >(m_Data); + } + + /* -------------------------------------------------------------------------------------------- + * Attempt to convert the long integer to a squirrel float. + */ + SQFloat ToSqFloat() const + { + return ClampL< Float64, SQFloat >(static_cast< Float64 >(m_Data)); + } + + /* -------------------------------------------------------------------------------------------- + * Attempt to convert the long integer to a squirrel string. + */ + CSStr ToSqString() + { + return ToString(); + } + + /* -------------------------------------------------------------------------------------------- + * Attempt to convert the long integer to a squirrel boolean. + */ + bool ToSqBool() const + { + return (m_Data > 0); + } + + /* -------------------------------------------------------------------------------------------- + * Attempt to convert the long integer to a squirrel character. + */ + SQChar ToSqChar() const + { + return ClampL< Type, SQChar >(m_Data); + } +}; + +/* ------------------------------------------------------------------------------------------------ + * +*/ +template <> class LongInt< Uint64 > +{ +public: + + // -------------------------------------------------------------------------------------------- + typedef Uint64 Type; + +private: + + // -------------------------------------------------------------------------------------------- + Type m_Data; + SQChar m_Text[32]; + +public: + + /* -------------------------------------------------------------------------------------------- + * + */ + LongInt() + : m_Data(0), m_Text() + { + /* ... */ + } + + LongInt(Type n) + : m_Data(n), m_Text() + { + /* ... */ + } + + /* -------------------------------------------------------------------------------------------- + * + */ + LongInt(CSStr text); + LongInt(CSStr text, SQInteger fall); + + /* -------------------------------------------------------------------------------------------- + * + */ + LongInt(const LongInt< Type > & o) + : m_Data(o.m_Data), m_Text() + { + /* ... */ + } + + /* -------------------------------------------------------------------------------------------- + * + */ + ~LongInt() + { + /* ... */ + } + + /* -------------------------------------------------------------------------------------------- + * + */ + LongInt & operator = (const LongInt< Type > & o) + { + m_Data = o.m_Data; + return *this; + } + + /* -------------------------------------------------------------------------------------------- + * + */ + LongInt< Type > & operator = (Type data) + { + m_Data = data; + return *this; + } + + /* -------------------------------------------------------------------------------------------- + * + */ + LongInt< Type > & operator = (CSStr text); + + /* -------------------------------------------------------------------------------------------- + * + */ + bool operator == (const LongInt< Type > & o) const + { + return (m_Data == o.m_Data); + } + + bool operator != (const LongInt< Type > & o) const + { + return (m_Data != o.m_Data); + } + + bool operator < (const LongInt< Type > & o) const + { + return (m_Data < o.m_Data); + } + + bool operator > (const LongInt< Type > & o) const + { + return (m_Data > o.m_Data); + } + + bool operator <= (const LongInt< Type > & o) const + { + return (m_Data <= o.m_Data); + } + + bool operator >= (const LongInt< Type > & o) const + { + return (m_Data >= o.m_Data); + } + + + /* -------------------------------------------------------------------------------------------- + * + */ + operator Type () const { return m_Data; } + + /* -------------------------------------------------------------------------------------------- + * + */ + LongInt< Type > operator + (const LongInt< Type > & o) const + { + return LongInt< Type >(m_Data + o.m_Data); + } + + LongInt< Type > operator - (const LongInt< Type > & o) const + { + return LongInt< Type >(m_Data - o.m_Data); + } + + LongInt< Type > operator * (const LongInt< Type > & o) const + { + return LongInt< Type >(m_Data * o.m_Data); + } + + LongInt< Type > operator / (const LongInt< Type > & o) const + { + return LongInt< Type >(m_Data / o.m_Data); + } + + LongInt< Type > operator % (const LongInt< Type > & o) const + { + return LongInt< Type >(m_Data % o.m_Data); + } + + + /* -------------------------------------------------------------------------------------------- + * + */ + LongInt< Type > operator - () const + { + return LongInt< Type >(-m_Data); + } + + /* -------------------------------------------------------------------------------------------- + * + */ + Int32 Cmp(const LongInt< Type > & o) const + { + if (m_Data == o.m_Data) + { + return 0; + } + else if (m_Data > o.m_Data) + { + return 1; + } + else + { + return -1; + } + } + + /* -------------------------------------------------------------------------------------------- + * + */ + CSStr ToString(); + + /* -------------------------------------------------------------------------------------------- + * + */ + CSStr Typename() const + { + return _SC("ULongInt"); + } + + /* -------------------------------------------------------------------------------------------- + * + */ + void SetNum(Type data) + { + m_Data = data; + } + + Type GetNum() const + { + return m_Data; + } + + /* -------------------------------------------------------------------------------------------- + * + */ + SQInteger GetSNum() const + { + return (SQInteger)(m_Data); + } + + /* -------------------------------------------------------------------------------------------- + * + */ + void SetStr(CSStr text) + { + *this = text; + } + + CSStr GetCStr() + { + return ToString(); + } + + /* -------------------------------------------------------------------------------------------- + * + */ + void Random(); + void Random(Type n); + void Random(Type m, Type n); + + /* -------------------------------------------------------------------------------------------- + * Attempt to convert the long integer to a squirrel integer. + */ + SQInteger ToSqInteger() const + { + return ClampL< Type, SQInteger >(m_Data); + } + + /* -------------------------------------------------------------------------------------------- + * Attempt to convert the long integer to a squirrel float. + */ + SQFloat ToSqFloat() const + { + return ClampL< Float64, SQFloat >(static_cast< Float64 >(m_Data)); + } + + /* -------------------------------------------------------------------------------------------- + * Attempt to convert the long integer to a squirrel string. + */ + CSStr ToSqString() + { + return ToString(); + } + + /* -------------------------------------------------------------------------------------------- + * Attempt to convert the long integer to a squirrel boolean. + */ + bool ToSqBool() const + { + return (m_Data > 0); + } + + /* -------------------------------------------------------------------------------------------- + * Attempt to convert the long integer to a squirrel character. + */ + SQChar ToSqChar() const + { + return ClampL< Type, SQChar >(m_Data); + } +}; + +// ------------------------------------------------------------------------------------------------ +typedef LongInt< Int64 > SLongInt; +typedef LongInt< Uint64 > ULongInt; + +} // Namespace:: SqMod + +#endif // _LIBRARY_NUMERIC_LONGINT_HPP_ diff --git a/source/Library/Math.cpp b/source/Library/Numeric/Math.cpp similarity index 99% rename from source/Library/Math.cpp rename to source/Library/Numeric/Math.cpp index 0806412b..59d48e56 100644 --- a/source/Library/Math.cpp +++ b/source/Library/Numeric/Math.cpp @@ -1,6 +1,7 @@ // ------------------------------------------------------------------------------------------------ -#include "Library/Math.hpp" -#include "Library/Numeric.hpp" +#include "Library/Numeric/Math.hpp" +#include "Library/Numeric/LongInt.hpp" +#include "Base/Shared.hpp" // ------------------------------------------------------------------------------------------------ #include diff --git a/source/Library/Math.hpp b/source/Library/Numeric/Math.hpp similarity index 65% rename from source/Library/Math.hpp rename to source/Library/Numeric/Math.hpp index 6adc5428..59e64c23 100644 --- a/source/Library/Math.hpp +++ b/source/Library/Numeric/Math.hpp @@ -1,12 +1,14 @@ -#ifndef _LIBRARY_MATH_HPP_ -#define _LIBRARY_MATH_HPP_ +#ifndef _LIBRARY_NUMERIC_MATH_HPP_ +#define _LIBRARY_NUMERIC_MATH_HPP_ // ------------------------------------------------------------------------------------------------ -#include "Base/Shared.hpp" +#include "SqBase.hpp" // ------------------------------------------------------------------------------------------------ namespace SqMod { + + } // Namespace:: SqMod -#endif // _LIBRARY_MATH_HPP_ +#endif // _LIBRARY_NUMERIC_MATH_HPP_ diff --git a/source/Library/Random.cpp b/source/Library/Numeric/Random.cpp similarity index 99% rename from source/Library/Random.cpp rename to source/Library/Numeric/Random.cpp index 4ee0b588..d082adb1 100644 --- a/source/Library/Random.cpp +++ b/source/Library/Numeric/Random.cpp @@ -1,5 +1,5 @@ // ------------------------------------------------------------------------------------------------ -#include "Library/Random.hpp" +#include "Library/Numeric/Random.hpp" #include "Base/Shared.hpp" #include "Base/Buffer.hpp" diff --git a/source/Library/Random.hpp b/source/Library/Numeric/Random.hpp similarity index 98% rename from source/Library/Random.hpp rename to source/Library/Numeric/Random.hpp index 431a5c03..b03221ab 100644 --- a/source/Library/Random.hpp +++ b/source/Library/Numeric/Random.hpp @@ -1,5 +1,5 @@ -#ifndef _LIBRARY_RANDOM_HPP_ -#define _LIBRARY_RANDOM_HPP_ +#ifndef _LIBRARY_NUMERIC_RANDOM_HPP_ +#define _LIBRARY_NUMERIC_RANDOM_HPP_ // ------------------------------------------------------------------------------------------------ #include "SqBase.hpp" @@ -161,4 +161,4 @@ template <> struct RandomVal< bool > } // Namespace:: SqMod -#endif // _LIBRARY_RANDOM_HPP_ +#endif // _LIBRARY_NUMERIC_RANDOM_HPP_ diff --git a/source/Misc/Register.cpp b/source/Misc/Register.cpp index f39bc433..0602e6d6 100644 --- a/source/Misc/Register.cpp +++ b/source/Misc/Register.cpp @@ -5,7 +5,7 @@ #include "Base/Vector2.hpp" #include "Base/Vector3.hpp" #include "Entity/Player.hpp" -#include "Library/Numeric.hpp" +#include "Library/Numeric/LongInt.hpp" // ------------------------------------------------------------------------------------------------ #include "Misc/Functions.hpp" diff --git a/source/Register.cpp b/source/Register.cpp index 76085311..1b40c402 100644 --- a/source/Register.cpp +++ b/source/Register.cpp @@ -38,8 +38,6 @@ extern void Register_Chrono(HSQUIRRELVM vm); extern void Register_Crypt(HSQUIRRELVM vm); extern void Register_IO(HSQUIRRELVM vm); extern void Register_Numeric(HSQUIRRELVM vm); -extern void Register_Math(HSQUIRRELVM vm); -extern void Register_Random(HSQUIRRELVM vm); extern void Register_String(HSQUIRRELVM vm); extern void Register_System(HSQUIRRELVM vm); @@ -79,9 +77,7 @@ bool RegisterAPI(HSQUIRRELVM vm) Register_Chrono(vm); Register_Crypt(vm); Register_IO(vm); - Register_Random(vm); Register_Numeric(vm); - Register_Math(vm); Register_String(vm); Register_System(vm);