From 4edea184deab8daf0211a59ba60ae1b2fd8167e5 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Sat, 13 Jun 2009 21:55:55 +0000 Subject: [PATCH] Move towards more header only implementation friendly --- chaiscript/chaiscript_engine.hpp | 4 +- dispatchkit/bootstrap.hpp | 151 +++++++++---------------------- dispatchkit/bootstrap_pod.hpp | 80 ++++++++++++++++ dispatchkit/boxed_value.hpp | 8 +- dispatchkit/boxedcpp.hpp | 2 +- dispatchkit/proxy_functions.hpp | 6 +- dispatchkit/test.cpp | 2 +- dispatchkit/type_info.hpp | 6 +- dispatchkit/unittest.cpp | 4 +- 9 files changed, 138 insertions(+), 125 deletions(-) create mode 100644 dispatchkit/bootstrap_pod.hpp diff --git a/chaiscript/chaiscript_engine.hpp b/chaiscript/chaiscript_engine.hpp index 31bbc44..1e0c226 100644 --- a/chaiscript/chaiscript_engine.hpp +++ b/chaiscript/chaiscript_engine.hpp @@ -186,10 +186,8 @@ public: Eval_Engine build_eval_system(Lexer &lexer, Rule &parser) { Eval_Engine ss; - bootstrap(ss); - bootstrap_vector >(ss, "VectorInt"); + Bootstrap::bootstrap(ss); bootstrap_vector >(ss, "Vector"); - //dump_system(ss); ss.register_function(boost::shared_ptr( new Dynamic_Proxy_Function(boost::bind(&ChaiScript_System::eval, boost::ref(*this), _1), 1)), "eval"); diff --git a/dispatchkit/bootstrap.hpp b/dispatchkit/bootstrap.hpp index d0a854a..16b60e7 100644 --- a/dispatchkit/bootstrap.hpp +++ b/dispatchkit/bootstrap.hpp @@ -2,7 +2,7 @@ #define __bootstrap_hpp__ #include "boxedcpp.hpp" -#include "register_function.hpp" +#include "bootstrap_pod.hpp" template Ret add(P1 p1, P2 p2) @@ -10,11 +10,6 @@ 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) @@ -22,11 +17,6 @@ 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) @@ -34,11 +24,6 @@ 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) @@ -46,11 +31,6 @@ 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) @@ -88,10 +68,7 @@ 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) @@ -99,11 +76,6 @@ 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) @@ -111,44 +83,24 @@ 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) { @@ -344,24 +296,6 @@ void add_opers_comparison_overload(Dispatch_Engine &s) register_function(s, &greater_than_equals, ">="); } -void add_opers_comparison_pod(Dispatch_Engine &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(Dispatch_Engine &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(Dispatch_Engine &s) { @@ -436,15 +370,7 @@ class bad_boxed_value_cast : public std::bad_cast std::string m_val; }; -Boxed_Value unknown_assign(Boxed_Value lhs, Boxed_Value rhs) -{ - if (lhs.is_unknown()) - { - return (lhs.assign(rhs)); - } else { - throw bad_boxed_value_cast("boxed_value has a set type alread"); - } -} + //Built in to_string operator template @@ -475,48 +401,59 @@ void bootstrap_pod_type(Dispatch_Engine &s, const std::string &name) register_function(s, &to_string, "to_string"); } -void print(const std::string &s) +struct Bootstrap { - std::cout << s << std::endl; -} + static Boxed_Value unknown_assign(Boxed_Value lhs, Boxed_Value rhs) + { + if (lhs.is_unknown()) + { + return (lhs.assign(rhs)); + } else { + throw bad_boxed_value_cast("boxed_value has a set type alread"); + } + } -void bootstrap(Dispatch_Engine &s) -{ - s.register_type("void"); + static void print(const std::string &s) + { + std::cout << s << std::endl; + } - s.register_type("string"); + static void bootstrap(Dispatch_Engine &s) + { + s.register_type("void"); - add_basic_constructors(s, "bool"); - add_basic_constructors(s, "string"); - add_oper_assign(s); + s.register_type("string"); - register_function(s, &to_string, "to_string"); - register_function(s, &to_string, "to_string"); - register_function(s, &unknown_assign, "="); + add_basic_constructors(s, "bool"); + add_basic_constructors(s, "string"); + add_oper_assign(s); - 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"); + register_function(s, &to_string, "to_string"); + register_function(s, &to_string, "to_string"); + register_function(s, &unknown_assign, "="); + + 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"); - add_opers_comparison_pod(s); - add_opers_arithmetic_pod(s); + Pod_Bootstrap::add_opers_comparison(s); + Pod_Bootstrap::add_opers_arithmetic(s); - add_oper_add(s); - add_oper_add_equals (s); + add_oper_add(s); + add_oper_add_equals (s); - register_function(s, &print, "print_string"); + register_function(s, &print, "print_string"); - s.register_function(boost::function(boost::bind(&dump_system, boost::ref(s))), "dump_system"); - s.register_function(boost::function(boost::bind(&dump_object, _1)), "dump_object"); + s.register_function(boost::function(boost::bind(&dump_system, boost::ref(s))), "dump_system"); + s.register_function(boost::function(boost::bind(&dump_object, _1)), "dump_object"); - - register_function(s, &bool_and, "&&"); - register_function(s, &bool_or, "||"); - -} + register_function(s, &bool_and, "&&"); + register_function(s, &bool_or, "||"); + } +}; #endif diff --git a/dispatchkit/bootstrap_pod.hpp b/dispatchkit/bootstrap_pod.hpp new file mode 100644 index 0000000..aca3d99 --- /dev/null +++ b/dispatchkit/bootstrap_pod.hpp @@ -0,0 +1,80 @@ +#ifndef __dispatchkit_bootstrap_pod_hpp__ +#define __dispatchkit_bootstrap_pod_hpp__ + + +#include "register_function.hpp" + + +struct Pod_Bootstrap +{ + static Boxed_Value add(Boxed_POD_Value l, Boxed_POD_Value r) + { + return l + r; + } + + static Boxed_Value subtract(Boxed_POD_Value l, Boxed_POD_Value r) + { + return l - r; + } + + static Boxed_Value divide(Boxed_POD_Value l, Boxed_POD_Value r) + { + return l / r; + } + + static Boxed_Value multiply(Boxed_POD_Value l, Boxed_POD_Value r) + { + return l * r; + } + + static bool equals(Boxed_POD_Value l, Boxed_POD_Value r) + { + return l == r; + } + + static bool not_equals(Boxed_POD_Value l, Boxed_POD_Value r) + { + return l != r; + } + + static bool less_than(Boxed_POD_Value l, Boxed_POD_Value r) + { + return l < r; + } + + static bool greater_than(Boxed_POD_Value l, Boxed_POD_Value r) + { + return l > r; + } + + static bool less_than_equals(Boxed_POD_Value l, Boxed_POD_Value r) + { + return l <= r; + } + + static bool greater_than_equals(Boxed_POD_Value l, Boxed_POD_Value r) + { + return l >= r; + } + + static void add_opers_comparison(Dispatch_Engine &s) + { + register_function(s, &equals, "=="); + register_function(s, ¬_equals, "!="); + register_function(s, &less_than, "<"); + register_function(s, &greater_than, ">"); + register_function(s, &less_than_equals, "<="); + register_function(s, &greater_than_equals, ">="); + } + + static void add_opers_arithmetic(Dispatch_Engine &s) + { + register_function(s, &add, "+"); + register_function(s, &subtract, "-"); + register_function(s, ÷, "/"); + register_function(s, &multiply, "*"); + } +}; + +#endif + diff --git a/dispatchkit/boxed_value.hpp b/dispatchkit/boxed_value.hpp index 2949ebd..c7a1125 100644 --- a/dispatchkit/boxed_value.hpp +++ b/dispatchkit/boxed_value.hpp @@ -83,7 +83,7 @@ class Boxed_Value boost::shared_ptr get(Boxed_Value::Void_Type) { return boost::shared_ptr (new Data( - Get_Type_Info()(), + Get_Type_Info::get(), boost::any(), false) ); @@ -93,7 +93,7 @@ class Boxed_Value boost::shared_ptr get(boost::shared_ptr obj) { boost::shared_ptr data(new Data( - Get_Type_Info()(), + Get_Type_Info::get(), boost::any(obj), false, boost::shared_ptr(new Data::Shared_Ptr_Proxy_Impl())) @@ -116,7 +116,7 @@ class Boxed_Value boost::shared_ptr get(boost::reference_wrapper obj) { boost::shared_ptr data(new Data( - Get_Type_Info()(), + Get_Type_Info::get(), boost::any(obj), true) ); @@ -137,7 +137,7 @@ class Boxed_Value boost::shared_ptr get(const T& t) { boost::shared_ptr data(new Data( - Get_Type_Info()(), + Get_Type_Info::get(), boost::any(boost::shared_ptr(new T(t))), false, boost::shared_ptr(new Data::Shared_Ptr_Proxy_Impl())) diff --git a/dispatchkit/boxedcpp.hpp b/dispatchkit/boxedcpp.hpp index 2ea25e5..ff3fa15 100644 --- a/dispatchkit/boxedcpp.hpp +++ b/dispatchkit/boxedcpp.hpp @@ -108,7 +108,7 @@ class Dispatch_Engine template void register_type(const std::string &name) { - m_types.insert(std::make_pair(name, Get_Type_Info()())); + m_types.insert(std::make_pair(name, Get_Type_Info::get())); } std::vector get_types() const diff --git a/dispatchkit/proxy_functions.hpp b/dispatchkit/proxy_functions.hpp index 0645a5d..d4820cb 100644 --- a/dispatchkit/proxy_functions.hpp +++ b/dispatchkit/proxy_functions.hpp @@ -1,6 +1,6 @@ #include -#define gettypeinfo(z,n,text) ti.push_back(Get_Type_Info()()); +#define gettypeinfo(z,n,text) ti.push_back(Get_Type_Info::get()); #define casthelper(z,n,text) ,Cast_Helper()(params[n]) @@ -69,7 +69,7 @@ template std::vector build_param_type_list(const boost::function &f) { std::vector ti; - ti.push_back(Get_Type_Info()()); + ti.push_back(Get_Type_Info::get()); return ti; } @@ -205,7 +205,7 @@ template std::vector build_param_type_list(const boost::function &f) { std::vector ti; - ti.push_back(Get_Type_Info()()); + ti.push_back(Get_Type_Info::get()); BOOST_PP_REPEAT(n, gettypeinfo, ~) diff --git a/dispatchkit/test.cpp b/dispatchkit/test.cpp index 5e15d38..4da09cd 100644 --- a/dispatchkit/test.cpp +++ b/dispatchkit/test.cpp @@ -88,7 +88,7 @@ void test(const std::string &p) int main() { Dispatch_Engine ss; - bootstrap(ss); + Bootstrap::bootstrap(ss); bootstrap_vector >(ss, "VectorInt"); dump_system(ss); diff --git a/dispatchkit/type_info.hpp b/dispatchkit/type_info.hpp index 5d2db8d..48e806c 100644 --- a/dispatchkit/type_info.hpp +++ b/dispatchkit/type_info.hpp @@ -44,7 +44,7 @@ struct Type_Info template struct Get_Type_Info { - Type_Info operator()() + static Type_Info get() { return Type_Info(boost::is_const::value, boost::is_reference::value, boost::is_pointer::value, boost::is_void::value, @@ -56,7 +56,7 @@ struct Get_Type_Info template struct Get_Type_Info > { - Type_Info operator()() + static Type_Info get() { return Type_Info(boost::is_const::value, boost::is_reference::value, boost::is_pointer::value, boost::is_void::value, @@ -68,7 +68,7 @@ struct Get_Type_Info > template struct Get_Type_Info > { - Type_Info operator()() + static Type_Info get() { return Type_Info(boost::is_const::value, boost::is_reference::value, boost::is_pointer::value, boost::is_void::value, diff --git a/dispatchkit/unittest.cpp b/dispatchkit/unittest.cpp index 1062a2a..958cf61 100644 --- a/dispatchkit/unittest.cpp +++ b/dispatchkit/unittest.cpp @@ -8,10 +8,8 @@ BOOST_AUTO_TEST_CASE( add_operators ) { Dispatch_Engine ss; - bootstrap(ss); + Bootstrap::bootstrap(ss); dump_system(ss); BOOST_CHECK_EQUAL(Cast_Helper()(dispatch(ss.get_function("+"), Param_List_Builder() << double(5.1) << double(10.3))), 15.4); - - }