1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2024-11-14 11:47:15 +01:00
SqMod/source/Library/LongInt.hpp

350 lines
8.7 KiB
C++
Raw Normal View History

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