Skip to content

Commit a93c425

Browse files
authored
Move big int methods to their own file (#512)
Streamlines big int metamethod handling
1 parent 0bc5bf4 commit a93c425

File tree

6 files changed

+592
-97
lines changed

6 files changed

+592
-97
lines changed

ElunaTemplate.h

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -141,18 +141,6 @@ class ElunaObjectValueImpl : public ElunaObject
141141
T _obj;
142142
};
143143

144-
#define ELUNA_MATH_OP(type, func, op) \
145-
template<> \
146-
int ElunaTemplate<type>::func(lua_State* L) { \
147-
return ElunaTemplateHelper<type>::PerformOp(L, std::op()); \
148-
}
149-
150-
#define ELUNA_SIMPLE_FORWARD(type, func) \
151-
template<> \
152-
int ElunaTemplate<type>::func(lua_State* L) { \
153-
return ElunaTemplateHelper<type>::func(L); \
154-
}
155-
156144
#define MAKE_ELUNA_OBJECT_VALUE_IMPL(type) \
157145
template <> \
158146
class ElunaObjectImpl<type> : public ElunaObjectValueImpl<type> \
@@ -508,48 +496,4 @@ class ElunaTemplate
508496

509497
template<typename T> const char* ElunaTemplate<T>::tname = NULL;
510498

511-
template <typename T>
512-
class ElunaTemplateHelper
513-
{
514-
public:
515-
static int PerformOp(lua_State* L, std::function<T(T, T)> op)
516-
{
517-
Eluna* E = Eluna::GetEluna(L);
518-
T val1 = E->CHECKVAL<T>(1);
519-
T val2 = E->CHECKVAL<T>(2);
520-
E->Push(op(val1, val2));
521-
return 1;
522-
}
523-
524-
static int PerformOp(lua_State* L, std::function<T(T)> op)
525-
{
526-
Eluna* E = Eluna::GetEluna(L);
527-
528-
T val = E->CHECKVAL<T>(1);
529-
E->Push(op(val));
530-
return 1;
531-
}
532-
533-
static int ToString(lua_State* L)
534-
{
535-
Eluna* E = Eluna::GetEluna(L);
536-
537-
T val = E->CHECKVAL<T>(1);
538-
std::ostringstream ss;
539-
ss << val;
540-
E->Push(ss.str());
541-
return 1;
542-
}
543-
544-
static int Pow(lua_State* L)
545-
{
546-
Eluna* E = Eluna::GetEluna(L);
547-
548-
T val1 = E->CHECKVAL<T>(1);
549-
T val2 = E->CHECKVAL<T>(2);
550-
E->Push(static_cast<T>(powl(static_cast<long double>(val1), static_cast<long double>(val2))));
551-
return 1;
552-
}
553-
};
554-
555499
#endif

methods/CMangos/BigIntMethods.h

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/*
2+
* Copyright (C) 2010 - 2025 Eluna Lua Engine <https://elunaluaengine.github.io/>
3+
* This program is free software licensed under GPL version 3
4+
* Please see the included DOCS/LICENSE.md for more information
5+
*/
6+
7+
#ifndef BIGINTMETHODS_H
8+
#define BIGINTMETHODS_H
9+
10+
namespace LuaBigInt
11+
{
12+
template <typename T, typename Op>
13+
constexpr int PerformOp(Eluna* E, Op op)
14+
{
15+
T val1 = E->CHECKVAL<T>(1);
16+
T val2 = E->CHECKVAL<T>(2);
17+
E->Push(op(val1, val2));
18+
return 1;
19+
}
20+
21+
template<typename T>
22+
int Add(Eluna* E, T*)
23+
{
24+
return PerformOp<T>(E, std::plus<T>{});
25+
}
26+
27+
template<typename T>
28+
int Subtract(Eluna* E, T*)
29+
{
30+
return PerformOp<T>(E, std::minus<T>{});
31+
}
32+
33+
template<typename T>
34+
int Multiply(Eluna* E, T*)
35+
{
36+
return PerformOp<T>(E, std::multiplies<T>{});
37+
}
38+
39+
template<typename T>
40+
int Divide(Eluna* E, T*)
41+
{
42+
return PerformOp<T>(E, std::divides<T>{});
43+
}
44+
45+
template<typename T>
46+
int Mod(Eluna* E, T*)
47+
{
48+
return PerformOp<T>(E, std::modulus<T>{});
49+
}
50+
51+
template<typename T>
52+
int UnaryMinus(Eluna* E, T*)
53+
{
54+
T val = E->CHECKVAL<T>(1);
55+
E->Push(std::negate<T>{}(val));
56+
return 1;
57+
}
58+
59+
template<typename T>
60+
int Equal(Eluna* E, T*)
61+
{
62+
return PerformOp<T>(E, std::equal_to<T>{});
63+
}
64+
65+
template<typename T>
66+
int Less(Eluna* E, T*)
67+
{
68+
return PerformOp<T>(E, std::less<T>{});
69+
}
70+
71+
template<typename T>
72+
int LessOrEqual(Eluna* E, T*)
73+
{
74+
return PerformOp<T>(E, std::less_equal<T>{});
75+
}
76+
77+
template<typename T>
78+
int ToString(Eluna* E, T*)
79+
{
80+
T val = E->CHECKVAL<T>(1);
81+
std::ostringstream ss;
82+
ss << val;
83+
E->Push(ss.str());
84+
return 1;
85+
}
86+
87+
template<typename T>
88+
int Pow(Eluna* E, T*)
89+
{
90+
T val1 = E->CHECKVAL<T>(1);
91+
T val2 = E->CHECKVAL<T>(2);
92+
E->Push(static_cast<T>(powl(static_cast<long double>(val1), static_cast<long double>(val2))));
93+
return 1;
94+
}
95+
96+
int Equal(Eluna* E, ObjectGuid*)
97+
{
98+
E->Push(E->CHECKVAL<ObjectGuid>(1) == E->CHECKVAL<ObjectGuid>(2));
99+
return 1;
100+
}
101+
102+
int ToString(Eluna* E, ObjectGuid*)
103+
{
104+
#if defined ELUNA_TRINITY
105+
E->Push(E->CHECKVAL<ObjectGuid>(1).ToString());
106+
#else
107+
E->Push(E->CHECKVAL<ObjectGuid>(1).GetString());
108+
#endif
109+
return 1;
110+
}
111+
112+
ElunaRegister<long long> LongLongMethods[] =
113+
{
114+
{ "__add", &LuaBigInt::Add<long long> },
115+
{ "__sub", &LuaBigInt::Subtract<long long> },
116+
{ "__mul", &LuaBigInt::Multiply<long long> },
117+
{ "__div", &LuaBigInt::Divide<long long> },
118+
{ "__mod", &LuaBigInt::Mod<long long> },
119+
{ "__unm", &LuaBigInt::UnaryMinus<long long> },
120+
{ "__eq", &LuaBigInt::Equal<long long> },
121+
{ "__lt", &LuaBigInt::Less<long long> },
122+
{ "__le", &LuaBigInt::LessOrEqual<long long> },
123+
{ "__tostring", &LuaBigInt::ToString<long long> },
124+
{ "__pow", &LuaBigInt::Pow<long long> },
125+
};
126+
127+
ElunaRegister<unsigned long long> ULongLongMethods[] =
128+
{
129+
{ "__sub", &LuaBigInt::Subtract<unsigned long long> },
130+
{ "__mul", &LuaBigInt::Multiply<unsigned long long> },
131+
{ "__div", &LuaBigInt::Divide<unsigned long long> },
132+
{ "__mod", &LuaBigInt::Mod<unsigned long long> },
133+
{ "__eq", &LuaBigInt::Equal<unsigned long long> },
134+
{ "__lt", &LuaBigInt::Less<unsigned long long> },
135+
{ "__le", &LuaBigInt::LessOrEqual<unsigned long long> },
136+
{ "__tostring", &LuaBigInt::ToString<unsigned long long> },
137+
{ "__pow", &LuaBigInt::Pow<unsigned long long> },
138+
};
139+
140+
ElunaRegister<ObjectGuid> ObjectGuidMethods[] =
141+
{
142+
{ "__tostring", &LuaBigInt::ToString<ObjectGuid> },
143+
{ "__eq", &LuaBigInt::Equal<ObjectGuid> },
144+
};
145+
};
146+
147+
#endif

methods/Mangos/BigIntMethods.h

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/*
2+
* Copyright (C) 2010 - 2025 Eluna Lua Engine <https://elunaluaengine.github.io/>
3+
* This program is free software licensed under GPL version 3
4+
* Please see the included DOCS/LICENSE.md for more information
5+
*/
6+
7+
#ifndef BIGINTMETHODS_H
8+
#define BIGINTMETHODS_H
9+
10+
namespace LuaBigInt
11+
{
12+
template <typename T, typename Op>
13+
constexpr int PerformOp(Eluna* E, Op op)
14+
{
15+
T val1 = E->CHECKVAL<T>(1);
16+
T val2 = E->CHECKVAL<T>(2);
17+
E->Push(op(val1, val2));
18+
return 1;
19+
}
20+
21+
template<typename T>
22+
int Add(Eluna* E, T*)
23+
{
24+
return PerformOp<T>(E, std::plus<T>{});
25+
}
26+
27+
template<typename T>
28+
int Subtract(Eluna* E, T*)
29+
{
30+
return PerformOp<T>(E, std::minus<T>{});
31+
}
32+
33+
template<typename T>
34+
int Multiply(Eluna* E, T*)
35+
{
36+
return PerformOp<T>(E, std::multiplies<T>{});
37+
}
38+
39+
template<typename T>
40+
int Divide(Eluna* E, T*)
41+
{
42+
return PerformOp<T>(E, std::divides<T>{});
43+
}
44+
45+
template<typename T>
46+
int Mod(Eluna* E, T*)
47+
{
48+
return PerformOp<T>(E, std::modulus<T>{});
49+
}
50+
51+
template<typename T>
52+
int UnaryMinus(Eluna* E, T*)
53+
{
54+
T val = E->CHECKVAL<T>(1);
55+
E->Push(std::negate<T>{}(val));
56+
return 1;
57+
}
58+
59+
template<typename T>
60+
int Equal(Eluna* E, T*)
61+
{
62+
return PerformOp<T>(E, std::equal_to<T>{});
63+
}
64+
65+
template<typename T>
66+
int Less(Eluna* E, T*)
67+
{
68+
return PerformOp<T>(E, std::less<T>{});
69+
}
70+
71+
template<typename T>
72+
int LessOrEqual(Eluna* E, T*)
73+
{
74+
return PerformOp<T>(E, std::less_equal<T>{});
75+
}
76+
77+
template<typename T>
78+
int ToString(Eluna* E, T*)
79+
{
80+
T val = E->CHECKVAL<T>(1);
81+
std::ostringstream ss;
82+
ss << val;
83+
E->Push(ss.str());
84+
return 1;
85+
}
86+
87+
template<typename T>
88+
int Pow(Eluna* E, T*)
89+
{
90+
T val1 = E->CHECKVAL<T>(1);
91+
T val2 = E->CHECKVAL<T>(2);
92+
E->Push(static_cast<T>(powl(static_cast<long double>(val1), static_cast<long double>(val2))));
93+
return 1;
94+
}
95+
96+
int Equal(Eluna* E, ObjectGuid*)
97+
{
98+
E->Push(E->CHECKVAL<ObjectGuid>(1) == E->CHECKVAL<ObjectGuid>(2));
99+
return 1;
100+
}
101+
102+
int ToString(Eluna* E, ObjectGuid*)
103+
{
104+
#if defined ELUNA_TRINITY
105+
E->Push(E->CHECKVAL<ObjectGuid>(1).ToString());
106+
#else
107+
E->Push(E->CHECKVAL<ObjectGuid>(1).GetString());
108+
#endif
109+
return 1;
110+
}
111+
112+
ElunaRegister<long long> LongLongMethods[] =
113+
{
114+
{ "__add", &LuaBigInt::Add<long long> },
115+
{ "__sub", &LuaBigInt::Subtract<long long> },
116+
{ "__mul", &LuaBigInt::Multiply<long long> },
117+
{ "__div", &LuaBigInt::Divide<long long> },
118+
{ "__mod", &LuaBigInt::Mod<long long> },
119+
{ "__unm", &LuaBigInt::UnaryMinus<long long> },
120+
{ "__eq", &LuaBigInt::Equal<long long> },
121+
{ "__lt", &LuaBigInt::Less<long long> },
122+
{ "__le", &LuaBigInt::LessOrEqual<long long> },
123+
{ "__tostring", &LuaBigInt::ToString<long long> },
124+
{ "__pow", &LuaBigInt::Pow<long long> },
125+
};
126+
127+
ElunaRegister<unsigned long long> ULongLongMethods[] =
128+
{
129+
{ "__sub", &LuaBigInt::Subtract<unsigned long long> },
130+
{ "__mul", &LuaBigInt::Multiply<unsigned long long> },
131+
{ "__div", &LuaBigInt::Divide<unsigned long long> },
132+
{ "__mod", &LuaBigInt::Mod<unsigned long long> },
133+
{ "__eq", &LuaBigInt::Equal<unsigned long long> },
134+
{ "__lt", &LuaBigInt::Less<unsigned long long> },
135+
{ "__le", &LuaBigInt::LessOrEqual<unsigned long long> },
136+
{ "__tostring", &LuaBigInt::ToString<unsigned long long> },
137+
{ "__pow", &LuaBigInt::Pow<unsigned long long> },
138+
};
139+
140+
ElunaRegister<ObjectGuid> ObjectGuidMethods[] =
141+
{
142+
{ "__tostring", &LuaBigInt::ToString<ObjectGuid> },
143+
{ "__eq", &LuaBigInt::Equal<ObjectGuid> },
144+
};
145+
};
146+
147+
#endif

0 commit comments

Comments
 (0)