From 88708aaf7b1e8aaa6b4af1fafa3932dcb9d39de5 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Tue, 9 Jun 2009 03:12:59 +0000 Subject: [PATCH] Build smarter operators for POD types --- boxedcpp/bootstrap.hpp | 191 +++++++++++++++++++++++++++++++++------ boxedcpp/boxed_value.hpp | 144 ++++++++++++++++++++++++++++- samples/vector.wes | 2 +- 3 files changed, 306 insertions(+), 31 deletions(-) diff --git a/boxedcpp/bootstrap.hpp b/boxedcpp/bootstrap.hpp index 484e842..4dc6c86 100644 --- a/boxedcpp/bootstrap.hpp +++ b/boxedcpp/bootstrap.hpp @@ -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 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 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 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 bool bool_and(P1 p1, P2 p2) { @@ -47,66 +70,155 @@ P1 &assign(P1 &p1, const P2 &p2) return (p1 = p2); } +template +P1 &assign_pod(P1 &p1, Boxed_POD_Value v) +{ + if (v.m_isfloat) + { + return (p1 = v.d); + } else { + return (p1 = v.i); + } +} + + template bool equals(P1 p1, P2 p2) { return p1 == p2; } +bool pod_equals(Boxed_POD_Value l, Boxed_POD_Value r) +{ + return l == r; +} + template 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 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 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 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 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 P1 ×equal(P1 &p1, const P2 &p2) { return (p1 *= p2); } +template +P1 ×equal_pod(P1 &p1, Boxed_POD_Value r) +{ + if (r.m_isfloat) + { + return (p1 *= r.d); + } else { + return (p1 *= r.i); + } +} + + template P1 ÷sequal(P1 &p1, const P2 &p2) { return (p1 /= p2); } +template +P1 ÷sequal_pod(P1 &p1, Boxed_POD_Value r) +{ + if (r.m_isfloat) + { + return (p1 /= r.d); + } else { + return (p1 /= r.i); + } +} + + template P1 &addsequal(P1 &p1, const P2 &p2) { return (p1 += p2); } +template +P1 &addsequal_pod(P1 &p1, Boxed_POD_Value r) +{ + if (r.m_isfloat) + { + return (p1 += r.d); + } else { + return (p1 += r.i); + } +} + template P1 &subtractsequal(P1 &p1, const P2 &p2) { return (p1 -= p2); } +template +P1 &subtractsequal_pod(P1 &p1, Boxed_POD_Value r) +{ + if (r.m_isfloat) + { + return (p1 -= r.d); + } else { + return (p1 -= r.i); + } +} + template P1 &prefixincrement(P1 &p1) { @@ -169,6 +281,14 @@ void add_oper_assign(BoxedCPP_System &s) register_function(s, &assign, "="); } + +template +void add_oper_assign_pod(BoxedCPP_System &s) +{ + register_function(s, &assign_pod, "="); +} + + template 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, ">="); } +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 void add_opers_comparison(BoxedCPP_System &s) { @@ -226,11 +364,21 @@ void add_opers_arithmetic_overload(BoxedCPP_System &s) register_function(s, &prefixdecrement, "--"); } +template +void add_opers_arithmetic_modify_pod(BoxedCPP_System &s) +{ + register_function(s, ×equal_pod, "*="); + register_function(s, ÷sequal_pod, "/="); + register_function(s, &subtractsequal_pod, "-="); + register_function(s, &addsequal_pod, "+="); +} + template void add_basic_constructors(BoxedCPP_System &s, const std::string &type) { s.register_function(build_constructor(), type); s.register_function(build_constructor(), type); + s.register_function(build_constructor(), "clone"); } template @@ -243,6 +391,7 @@ template void add_opers_arithmetic(BoxedCPP_System &s) { add_opers_arithmetic_overload(s); + } //Built in to_string operator @@ -262,25 +411,16 @@ std::string to_string(bool b) } } -template +template void bootstrap_pod_type(BoxedCPP_System &s, const std::string &name) { s.register_type(name); add_basic_constructors(s, name); add_oper_assign(s); + add_oper_assign_pod(s); add_opers_arithmetic(s); - add_opers_comparison(s); + add_opers_arithmetic_modify_pod(s); register_function(s, &to_string, "to_string"); - - add_constructor_overload(s, name); - add_constructor_overload(s, name); - add_constructor_overload(s, name); - - /* - add_opers_comparison_overload(s); - add_opers_comparison_overload(s); - add_opers_comparison_overload(s); - */ } void bootstrap(BoxedCPP_System &s) @@ -289,30 +429,23 @@ void bootstrap(BoxedCPP_System &s) s.register_type("string"); - add_basic_constructors(s, "double"); - add_basic_constructors(s, "int"); - add_basic_constructors(s, "char"); add_basic_constructors(s, "bool"); add_basic_constructors(s, "string"); add_oper_assign(s); + register_function(s, &to_string, "to_string"); + register_function(s, &to_string, "to_string"); + + bootstrap_pod_type(s, "double"); + bootstrap_pod_type(s, "int"); + bootstrap_pod_type(s, "size_t"); + bootstrap_pod_type(s, "char"); + bootstrap_pod_type(s, "int64_t"); - bootstrap_pod_type(s, "double"); - bootstrap_pod_type(s, "int"); - bootstrap_pod_type(s, "size_t"); - bootstrap_pod_type(s, "char"); - - - add_opers_arithmetic_overload(s); - add_opers_arithmetic_overload(s); - - add_opers_arithmetic_overload(s); - add_opers_arithmetic_overload(s); - - add_opers_comparison_overload(s); - add_opers_comparison_overload(s); + add_opers_comparison_pod(s); + add_opers_arithmetic_pod(s); add_oper_add(s); diff --git a/boxedcpp/boxed_value.hpp b/boxedcpp/boxed_value.hpp index 2f39d47..efc6590 100644 --- a/boxedcpp/boxed_value.hpp +++ b/boxedcpp/boxed_value.hpp @@ -38,7 +38,7 @@ class Boxed_Value { } - const Type_Info &get_type_info() + const Type_Info &get_type_info() const { return m_type_info; } @@ -59,6 +59,7 @@ class Boxed_Value bool m_is_ref; }; + //cast_help specializations template struct Cast_Helper @@ -139,6 +140,147 @@ struct Cast_Helper } }; +struct Boxed_POD_Value +{ + Boxed_POD_Value(const Boxed_Value &v) + : d(0), i(0), m_isfloat(false) + { + const int inp_ = int(v.get_type_info().m_type_info); + + const int char_ = int(&typeid(char)); + const int bool_ = int(&typeid(bool)); + + const int double_ = int(&typeid(double)); + const int float_ = int(&typeid(float)); + + const int uint8_t_ = int(&typeid(uint8_t)); + const int uint16_t_ = int(&typeid(uint16_t)); + const int uint32_t_ = int(&typeid(uint32_t)); +// const int uint64_t_ = int(&typeid(uint64_t)); + + const int int8_t_ = int(&typeid(int8_t)); + const int int16_t_ = int(&typeid(int16_t)); + const int int32_t_ = int(&typeid(int32_t)); + const int int64_t_ = int(&typeid(int64_t)); + + if (inp_ == double_) + { + d = Cast_Helper()(v); + m_isfloat = true; + } else if (inp_ == float_) { + d = Cast_Helper()(v); + m_isfloat = true; + } else if (inp_ == bool_ ) { + i = Cast_Helper()(v); + } else if (inp_ == char_) { + i = Cast_Helper()(v); + } else if (inp_ == int8_t_) { + i = Cast_Helper()(v); + } else if (inp_ == int16_t_) { + i = Cast_Helper()(v); + } else if (inp_ == int32_t_) { + i = Cast_Helper()(v); + } else if (inp_ == int64_t_) { + i = Cast_Helper()(v); + } else if (inp_ == uint8_t_) { + i = Cast_Helper()(v); + } else if (inp_ == uint16_t_) { + i = Cast_Helper()(v); + } else if (inp_ == uint32_t_) { + i = Cast_Helper()(v); + } else { + throw boost::bad_any_cast(); + } + } + + + + bool operator==(const Boxed_POD_Value &r) const + { + return ((m_isfloat)?d:i) == ((r.m_isfloat)?r.d:r.i); + } + + bool operator<(const Boxed_POD_Value &r) const + { + return ((m_isfloat)?d:i) < ((r.m_isfloat)?r.d:r.i); + } + + bool operator>(const Boxed_POD_Value &r) const + { + return ((m_isfloat)?d:i) > ((r.m_isfloat)?r.d:r.i); + } + + bool operator>=(const Boxed_POD_Value &r) const + { + return ((m_isfloat)?d:i) >= ((r.m_isfloat)?r.d:r.i); + } + + bool operator<=(const Boxed_POD_Value &r) const + { + return ((m_isfloat)?d:i) <= ((r.m_isfloat)?r.d:r.i); + } + + bool operator!=(const Boxed_POD_Value &r) const + { + return ((m_isfloat)?d:i) != ((r.m_isfloat)?r.d:r.i); + } + + Boxed_Value operator+(const Boxed_POD_Value &r) const + { + if (!m_isfloat && !r.m_isfloat) + { + return Boxed_Value(i + r.i); + } + + return Boxed_Value(((m_isfloat)?d:i) + ((r.m_isfloat)?r.d:r.i)); + } + + Boxed_Value operator*(const Boxed_POD_Value &r) const + { + if (!m_isfloat && !r.m_isfloat) + { + return Boxed_Value(i * r.i); + } + + return Boxed_Value(((m_isfloat)?d:i) * ((r.m_isfloat)?r.d:r.i)); + } + + Boxed_Value operator/(const Boxed_POD_Value &r) const + { + if (!m_isfloat && !r.m_isfloat) + { + return Boxed_Value(i / r.i); + } + + return Boxed_Value(((m_isfloat)?d:i) / ((r.m_isfloat)?r.d:r.i)); + } + + Boxed_Value operator-(const Boxed_POD_Value &r) const + { + if (!m_isfloat && !r.m_isfloat) + { + return Boxed_Value(i - r.i); + } + + return Boxed_Value(((m_isfloat)?d:i) - ((r.m_isfloat)?r.d:r.i)); + } + + double d; + int64_t i; + + bool m_isfloat; +}; + +template<> +struct Cast_Helper +{ + Boxed_POD_Value operator()(Boxed_Value ob) + { + return Boxed_POD_Value(ob); + } +}; + + #endif diff --git a/samples/vector.wes b/samples/vector.wes index 88391f1..c3d98bc 100644 --- a/samples/vector.wes +++ b/samples/vector.wes @@ -6,7 +6,7 @@ vec.push_back(25.3) var i = 0 -var size = int(vec.size()) +var size = vec.size() print("Vector Size: " + size.to_string());