#ifndef _LIBRARY_LONGINT_HPP_ #define _LIBRARY_LONGINT_HPP_ // ------------------------------------------------------------------------------------------------ #include "Common.hpp" // ------------------------------------------------------------------------------------------------ #include // ------------------------------------------------------------------------------------------------ namespace SqMod { // ------------------------------------------------------------------------------------------------ template class LongInt { public: // -------------------------------------------------------------------------------------------- static_assert(std::is_integral::value, "LongInt type is not an integral type"); // -------------------------------------------------------------------------------------------- typedef T Value; // -------------------------------------------------------------------------------------------- LongInt() noexcept : m_Data(0), m_Text() { } template LongInt(U data) noexcept { *this = data; } // -------------------------------------------------------------------------------------------- LongInt(const SQChar * text) noexcept { *this = text; } LongInt(const SQChar * text, SQInteger overload = 0) noexcept { *this = text; } // -------------------------------------------------------------------------------------------- LongInt(const LongInt & i) noexcept : m_Data(i.m_Data), m_Text(i.m_Text) { } LongInt(LongInt && i) noexcept : m_Data(i.m_Data), m_Text(std::move(i.m_Text)) { } // -------------------------------------------------------------------------------------------- ~LongInt() { } // -------------------------------------------------------------------------------------------- LongInt & operator = (const LongInt & i) noexcept { m_Data = i.m_Data; m_Text = i.m_Text; return *this; } LongInt & operator = (LongInt && i) noexcept { m_Data = i.m_Data; m_Text = std::move(i.m_Text); return *this; } // -------------------------------------------------------------------------------------------- template ::value>::type* = nullptr> LongInt & operator = (U data) noexcept { m_Data = static_cast(data); m_Text = std::to_string(m_Data); return *this; } LongInt & operator = (const SQChar * text) noexcept { m_Text = text; try { m_Data = SToI(text, 0, 10); } catch (const std::invalid_argument & e) { LogErr("Unable to extract number: %s", e.what()); } return *this; } // -------------------------------------------------------------------------------------------- bool operator == (const LongInt & x) const noexcept { return (m_Data == x.m_Data); } bool operator != (const LongInt & x) const noexcept { return (m_Data != x.m_Data); } bool operator < (const LongInt & x) const noexcept { return (m_Data < x.m_Data); } bool operator > (const LongInt & x) const noexcept { return (m_Data > x.m_Data); } bool operator <= (const LongInt & x) const noexcept { return (m_Data <= x.m_Data); } bool operator >= (const LongInt & x) const noexcept { return (m_Data >= x.m_Data); } // -------------------------------------------------------------------------------------------- inline operator T () const noexcept { return m_Data; } // -------------------------------------------------------------------------------------------- LongInt operator + (const LongInt & x) const noexcept { return LongInt(m_Data + x.m_Data); } LongInt operator - (const LongInt & x) const noexcept { return LongInt(m_Data - x.m_Data); } LongInt operator * (const LongInt & x) const noexcept { return LongInt(m_Data * x.m_Data); } LongInt operator / (const LongInt & x) const noexcept { return LongInt(m_Data / x.m_Data); } LongInt operator % (const LongInt & x) const noexcept { return LongInt(m_Data % x.m_Data); } // -------------------------------------------------------------------------------------------- LongInt operator - () const noexcept { return LongInt(-m_Data); } // -------------------------------------------------------------------------------------------- SQInteger Cmp(const LongInt & x) const noexcept { return m_Data == x.m_Data ? 0 : (m_Data > x.m_Data ? 1 : -1); } const SQChar * ToString() const noexcept { return m_Text.c_str(); } // -------------------------------------------------------------------------------------------- void SetNum(T data) noexcept { *this = data; } T GetNum() const noexcept { return m_Data; } SQInteger GetSNum() const noexcept { return static_cast(m_Data); } // -------------------------------------------------------------------------------------------- void SetStr(const SQChar * text) noexcept { *this = text; } const String & GetStr() const noexcept { return m_Text; } const SQChar * GetCStr() const noexcept { return m_Text.c_str(); } // -------------------------------------------------------------------------------------------- void Random() noexcept { m_Data = RandomVal::Get(); m_Text = std::to_string(m_Data); } void Random(T min, T max) noexcept { m_Data = RandomVal::Get(min, max); m_Text = std::to_string(m_Data); } private: // -------------------------------------------------------------------------------------------- T m_Data; String m_Text; }; // ------------------------------------------------------------------------------------------------ typedef LongInt SLongInt; typedef LongInt ULongInt; } // Namespace:: SqMod #endif // _LIBRARY_LONGINT_HPP_