class URHO3D_API BigInt
{
private:
// If the value can be stored in i32, it is stored here. Otherwise, this field is -1 or +1.
i32 signOrShortValue_;
public:
BigInt(i32 value = 0)
: signOrShortValue_(value)
{
}
BigInt(u32 value)
: signOrShortValue_(+1)
{
}
String ToString() const
{
return String(signOrShortValue_);
}
};
// BigInt::BigInt(i32 value = 0)
static void BigInt__BigInt_i32(BigInt* _ptr, i32 value)
{
new(_ptr) BigInt(value);
}
// BigInt::BigInt(u32 value)
static void BigInt__BigInt_u32(BigInt* _ptr, u32 value)
{
new(_ptr) BigInt(value);
}
// BigInt::BigInt(i32 value = 0)
engine->RegisterObjectBehaviour("BigInt", asBEHAVE_CONSTRUCT, "void f(int = 0)", AS_FUNCTION_OBJFIRST(BigInt__BigInt_i32), AS_CALL_CDECL_OBJFIRST);
// BigInt::BigInt(u32 value)
engine->RegisterObjectBehaviour("BigInt", asBEHAVE_CONSTRUCT, "void f(uint)", AS_FUNCTION_OBJFIRST(BigInt__BigInt_u32), AS_CALL_CDECL_OBJFIRST);
// C++
{
BigInt bigInt = -7; // -7 is int
assert(bigInt.ToString() == "-7");
}
{
BigInt bigInt = 0xFFFFFFFF; // 0xFFFFFFFF is unsigned
assert(bigInt.ToString() == "1");
}
{
BigInt bigInt = 0xFFFFFFF; // 0xFFFFFFF is int
assert(bigInt.ToString() != "1");
}
// AngelScript
void Start()
{
BigInt i1(2147483647);
log.Error(i1.ToString()); // print 2147483647 (used int constructor)
BigInt u1(2147483648);
log.Error(u1.ToString()); // print -2147483648 (used int constructor)
BigInt i2(0xFF);
log.Error(i2.ToString()); // print 1 (used uint constructor)
BigInt u2(0xFFFFFFFF);
log.Error(u2.ToString()); // print 1 (used uint constructor)
}
p.s. I known about uint(xxx), but a user can easily forget about it