Build smarter operators for POD types
This commit is contained in:
@@ -10,18 +10,35 @@ Ret add(P1 p1, P2 p2)
|
||||
return p1 + p2;
|
||||
}
|
||||
|
||||
Boxed_Value pod_add(Boxed_POD_Value l, Boxed_POD_Value r)
|
||||
{
|
||||
return l + r;
|
||||
}
|
||||
|
||||
|
||||
template<typename Ret, typename P1, typename P2>
|
||||
Ret subtract(P1 p1, P2 p2)
|
||||
{
|
||||
return p1 - p2;
|
||||
}
|
||||
|
||||
Boxed_Value pod_subtract(Boxed_POD_Value l, Boxed_POD_Value r)
|
||||
{
|
||||
return l - r;
|
||||
}
|
||||
|
||||
|
||||
template<typename Ret, typename P1, typename P2>
|
||||
Ret divide(P1 p1, P2 p2)
|
||||
{
|
||||
return p1 / p2;
|
||||
}
|
||||
|
||||
Boxed_Value pod_divide(Boxed_POD_Value l, Boxed_POD_Value r)
|
||||
{
|
||||
return l / r;
|
||||
}
|
||||
|
||||
|
||||
template<typename Ret, typename P1, typename P2>
|
||||
Ret multiply(P1 p1, P2 p2)
|
||||
@@ -29,6 +46,12 @@ Ret multiply(P1 p1, P2 p2)
|
||||
return p1 * p2;
|
||||
}
|
||||
|
||||
Boxed_Value pod_multiply(Boxed_POD_Value l, Boxed_POD_Value r)
|
||||
{
|
||||
return l * r;
|
||||
}
|
||||
|
||||
|
||||
template<typename P1, typename P2>
|
||||
bool bool_and(P1 p1, P2 p2)
|
||||
{
|
||||
@@ -47,66 +70,155 @@ P1 &assign(P1 &p1, const P2 &p2)
|
||||
return (p1 = p2);
|
||||
}
|
||||
|
||||
template<typename P1>
|
||||
P1 &assign_pod(P1 &p1, Boxed_POD_Value v)
|
||||
{
|
||||
if (v.m_isfloat)
|
||||
{
|
||||
return (p1 = v.d);
|
||||
} else {
|
||||
return (p1 = v.i);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template<typename P1, typename P2>
|
||||
bool equals(P1 p1, P2 p2)
|
||||
{
|
||||
return p1 == p2;
|
||||
}
|
||||
|
||||
bool pod_equals(Boxed_POD_Value l, Boxed_POD_Value r)
|
||||
{
|
||||
return l == r;
|
||||
}
|
||||
|
||||
template<typename P1, typename P2>
|
||||
bool not_equals(P1 p1, P2 p2)
|
||||
{
|
||||
return p1 != p2;
|
||||
}
|
||||
|
||||
bool pod_not_equals(Boxed_POD_Value l, Boxed_POD_Value r)
|
||||
{
|
||||
return l != r;
|
||||
}
|
||||
|
||||
|
||||
template<typename P1, typename P2>
|
||||
bool less_than(P1 p1, P2 p2)
|
||||
{
|
||||
return p1 < p2;
|
||||
}
|
||||
|
||||
bool pod_less_than(Boxed_POD_Value l, Boxed_POD_Value r)
|
||||
{
|
||||
return l < r;
|
||||
}
|
||||
|
||||
template<typename P1, typename P2>
|
||||
bool greater_than(P1 p1, P2 p2)
|
||||
{
|
||||
return p1 > p2;
|
||||
}
|
||||
|
||||
bool pod_greater_than(Boxed_POD_Value l, Boxed_POD_Value r)
|
||||
{
|
||||
return l > r;
|
||||
}
|
||||
|
||||
template<typename P1, typename P2>
|
||||
bool less_than_equals(P1 p1, P2 p2)
|
||||
{
|
||||
return p1 <= p2;
|
||||
}
|
||||
|
||||
bool pod_less_than_equals(Boxed_POD_Value l, Boxed_POD_Value r)
|
||||
{
|
||||
return l <= r;
|
||||
}
|
||||
|
||||
template<typename P1, typename P2>
|
||||
bool greater_than_equals(P1 p1, P2 p2)
|
||||
{
|
||||
return p1 >= p2;
|
||||
}
|
||||
|
||||
bool pod_greater_than_equals(Boxed_POD_Value l, Boxed_POD_Value r)
|
||||
{
|
||||
return l >= r;
|
||||
}
|
||||
|
||||
template<typename P1, typename P2>
|
||||
P1 ×equal(P1 &p1, const P2 &p2)
|
||||
{
|
||||
return (p1 *= p2);
|
||||
}
|
||||
|
||||
template<typename P1>
|
||||
P1 ×equal_pod(P1 &p1, Boxed_POD_Value r)
|
||||
{
|
||||
if (r.m_isfloat)
|
||||
{
|
||||
return (p1 *= r.d);
|
||||
} else {
|
||||
return (p1 *= r.i);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template<typename P1, typename P2>
|
||||
P1 ÷sequal(P1 &p1, const P2 &p2)
|
||||
{
|
||||
return (p1 /= p2);
|
||||
}
|
||||
|
||||
template<typename P1>
|
||||
P1 ÷sequal_pod(P1 &p1, Boxed_POD_Value r)
|
||||
{
|
||||
if (r.m_isfloat)
|
||||
{
|
||||
return (p1 /= r.d);
|
||||
} else {
|
||||
return (p1 /= r.i);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template<typename P1, typename P2>
|
||||
P1 &addsequal(P1 &p1, const P2 &p2)
|
||||
{
|
||||
return (p1 += p2);
|
||||
}
|
||||
|
||||
template<typename P1>
|
||||
P1 &addsequal_pod(P1 &p1, Boxed_POD_Value r)
|
||||
{
|
||||
if (r.m_isfloat)
|
||||
{
|
||||
return (p1 += r.d);
|
||||
} else {
|
||||
return (p1 += r.i);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename P1, typename P2>
|
||||
P1 &subtractsequal(P1 &p1, const P2 &p2)
|
||||
{
|
||||
return (p1 -= p2);
|
||||
}
|
||||
|
||||
template<typename P1>
|
||||
P1 &subtractsequal_pod(P1 &p1, Boxed_POD_Value r)
|
||||
{
|
||||
if (r.m_isfloat)
|
||||
{
|
||||
return (p1 -= r.d);
|
||||
} else {
|
||||
return (p1 -= r.i);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename P1>
|
||||
P1 &prefixincrement(P1 &p1)
|
||||
{
|
||||
@@ -169,6 +281,14 @@ void add_oper_assign(BoxedCPP_System &s)
|
||||
register_function(s, &assign<T,T>, "=");
|
||||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
void add_oper_assign_pod(BoxedCPP_System &s)
|
||||
{
|
||||
register_function(s, &assign_pod<T>, "=");
|
||||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
void add_oper_less_than(BoxedCPP_System &s)
|
||||
{
|
||||
@@ -205,6 +325,24 @@ void add_opers_comparison_overload(BoxedCPP_System &s)
|
||||
register_function(s, &greater_than_equals<const T&, const R&>, ">=");
|
||||
}
|
||||
|
||||
void add_opers_comparison_pod(BoxedCPP_System &s)
|
||||
{
|
||||
register_function(s, &pod_equals, "==");
|
||||
register_function(s, &pod_not_equals, "!=");
|
||||
register_function(s, &pod_less_than, "<");
|
||||
register_function(s, &pod_greater_than, ">");
|
||||
register_function(s, &pod_less_than_equals, "<=");
|
||||
register_function(s, &pod_greater_than_equals, ">=");
|
||||
}
|
||||
|
||||
void add_opers_arithmetic_pod(BoxedCPP_System &s)
|
||||
{
|
||||
register_function(s, &pod_add, "+");
|
||||
register_function(s, &pod_subtract, "-");
|
||||
register_function(s, &pod_divide, "/");
|
||||
register_function(s, &pod_multiply, "*");
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void add_opers_comparison(BoxedCPP_System &s)
|
||||
{
|
||||
@@ -226,11 +364,21 @@ void add_opers_arithmetic_overload(BoxedCPP_System &s)
|
||||
register_function(s, &prefixdecrement<T>, "--");
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void add_opers_arithmetic_modify_pod(BoxedCPP_System &s)
|
||||
{
|
||||
register_function(s, ×equal_pod<T>, "*=");
|
||||
register_function(s, ÷sequal_pod<T>, "/=");
|
||||
register_function(s, &subtractsequal_pod<T>, "-=");
|
||||
register_function(s, &addsequal_pod<T>, "+=");
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void add_basic_constructors(BoxedCPP_System &s, const std::string &type)
|
||||
{
|
||||
s.register_function(build_constructor<T>(), type);
|
||||
s.register_function(build_constructor<T, const T &>(), type);
|
||||
s.register_function(build_constructor<T, const T &>(), "clone");
|
||||
}
|
||||
|
||||
template<typename T, typename U>
|
||||
@@ -243,6 +391,7 @@ template<typename T>
|
||||
void add_opers_arithmetic(BoxedCPP_System &s)
|
||||
{
|
||||
add_opers_arithmetic_overload<T, T, T>(s);
|
||||
|
||||
}
|
||||
|
||||
//Built in to_string operator
|
||||
@@ -262,25 +411,16 @@ std::string to_string(bool b)
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T, typename P1, typename P2, typename P3>
|
||||
template<typename T>
|
||||
void bootstrap_pod_type(BoxedCPP_System &s, const std::string &name)
|
||||
{
|
||||
s.register_type<T>(name);
|
||||
add_basic_constructors<T>(s, name);
|
||||
add_oper_assign<T>(s);
|
||||
add_oper_assign_pod<T>(s);
|
||||
add_opers_arithmetic<T>(s);
|
||||
add_opers_comparison<T>(s);
|
||||
add_opers_arithmetic_modify_pod<T>(s);
|
||||
register_function(s, &to_string<T>, "to_string");
|
||||
|
||||
add_constructor_overload<T, P1>(s, name);
|
||||
add_constructor_overload<T, P2>(s, name);
|
||||
add_constructor_overload<T, P3>(s, name);
|
||||
|
||||
/*
|
||||
add_opers_comparison_overload<T, P1>(s);
|
||||
add_opers_comparison_overload<T, P2>(s);
|
||||
add_opers_comparison_overload<T, P3>(s);
|
||||
*/
|
||||
}
|
||||
|
||||
void bootstrap(BoxedCPP_System &s)
|
||||
@@ -289,30 +429,23 @@ void bootstrap(BoxedCPP_System &s)
|
||||
|
||||
s.register_type<std::string>("string");
|
||||
|
||||
add_basic_constructors<double>(s, "double");
|
||||
add_basic_constructors<int>(s, "int");
|
||||
add_basic_constructors<char>(s, "char");
|
||||
add_basic_constructors<bool>(s, "bool");
|
||||
add_basic_constructors<std::string>(s, "string");
|
||||
add_oper_assign<std::string>(s);
|
||||
|
||||
register_function(s, &to_string<const std::string &>, "to_string");
|
||||
register_function(s, &to_string<bool>, "to_string");
|
||||
|
||||
bootstrap_pod_type<double>(s, "double");
|
||||
bootstrap_pod_type<int>(s, "int");
|
||||
bootstrap_pod_type<size_t>(s, "size_t");
|
||||
bootstrap_pod_type<char>(s, "char");
|
||||
bootstrap_pod_type<int64_t>(s, "int64_t");
|
||||
|
||||
|
||||
bootstrap_pod_type<double, int, size_t, char>(s, "double");
|
||||
bootstrap_pod_type<int, double, size_t, char>(s, "int");
|
||||
bootstrap_pod_type<size_t, int, double, char>(s, "size_t");
|
||||
bootstrap_pod_type<char, int, double, size_t>(s, "char");
|
||||
|
||||
|
||||
add_opers_arithmetic_overload<double, int, double>(s);
|
||||
add_opers_arithmetic_overload<double, double, int>(s);
|
||||
|
||||
add_opers_arithmetic_overload<double, size_t, double>(s);
|
||||
add_opers_arithmetic_overload<double, double, size_t>(s);
|
||||
|
||||
add_opers_comparison_overload<int, double>(s);
|
||||
add_opers_comparison_overload<double, int>(s);
|
||||
|
||||
add_opers_comparison_pod(s);
|
||||
add_opers_arithmetic_pod(s);
|
||||
|
||||
add_oper_add<std::string>(s);
|
||||
|
||||
|
Reference in New Issue
Block a user