diff --git a/chaiscript/chaiscript_engine.hpp b/chaiscript/chaiscript_engine.hpp index 7f14976..35b2914 100644 --- a/chaiscript/chaiscript_engine.hpp +++ b/chaiscript/chaiscript_engine.hpp @@ -27,7 +27,7 @@ namespace chaiscript std::string val; try { - val = dispatchkit::Cast_Helper()(vals[0]); + val = dispatchkit::boxed_cast(vals[0]); } catch (EvalError &ee) { throw EvalError("Can not evaluate string: " + val + " reason: " + ee.reason, langkit::TokenPtr()); diff --git a/chaiscript/chaiscript_eval.hpp b/chaiscript/chaiscript_eval.hpp index fcc1a44..f8010e3 100644 --- a/chaiscript/chaiscript_eval.hpp +++ b/chaiscript/chaiscript_eval.hpp @@ -179,7 +179,7 @@ namespace chaiscript bool cond; try { retval = eval_token(ss, node->children[1]); - cond = dispatchkit::Cast_Helper()(retval); + cond = dispatchkit::boxed_cast(retval); } catch (std::exception) { throw EvalError("Boolean not('!') condition not boolean", node->children[0]); @@ -320,7 +320,7 @@ namespace chaiscript retval = eval_token(ss, node->children[0]); bool cond; try { - cond = dispatchkit::Cast_Helper()(retval); + cond = dispatchkit::boxed_cast(retval); } catch (std::exception &e) { throw EvalError("If condition not boolean", node->children[0]); @@ -339,7 +339,7 @@ namespace chaiscript else if (node->children[i]->text == "elseif") { retval = eval_token(ss, node->children[i+1]); try { - cond = dispatchkit::Cast_Helper()(retval); + cond = dispatchkit::boxed_cast(retval); } catch (std::exception &e) { throw EvalError("Elseif condition not boolean", node->children[i+1]); @@ -358,7 +358,7 @@ namespace chaiscript retval = eval_token(ss, node->children[0]); bool cond; try { - cond = dispatchkit::Cast_Helper()(retval); + cond = dispatchkit::boxed_cast(retval); } catch (std::exception) { throw EvalError("While condition not boolean", node->children[0]); @@ -368,7 +368,7 @@ namespace chaiscript eval_token(ss, node->children[1]); retval = eval_token(ss, node->children[0]); try { - cond = dispatchkit::Cast_Helper()(retval); + cond = dispatchkit::boxed_cast(retval); } catch (std::exception) { throw EvalError("While condition not boolean", node->children[0]); @@ -393,7 +393,7 @@ namespace chaiscript else if (node->children.size() == 3){ condition = eval_token(ss, node->children[0]); } - cond = dispatchkit::Cast_Helper()(condition); + cond = dispatchkit::boxed_cast(condition); } catch (std::exception &e) { throw EvalError("For condition not boolean", node); @@ -410,7 +410,7 @@ namespace chaiscript eval_token(ss, node->children[1]); condition = eval_token(ss, node->children[0]); } - cond = dispatchkit::Cast_Helper()(condition); + cond = dispatchkit::boxed_cast(condition); } catch (std::exception &e) { diff --git a/dispatchkit/boxed_value.hpp b/dispatchkit/boxed_value.hpp index 00b8160..ee353c9 100644 --- a/dispatchkit/boxed_value.hpp +++ b/dispatchkit/boxed_value.hpp @@ -258,7 +258,7 @@ namespace dispatchkit { typedef typename boost::reference_wrapper::type > Result_Type; - Result_Type operator()(Boxed_Value ob) + static Result_Type cast(const Boxed_Value &ob) { if (ob.is_ref()) { @@ -274,7 +274,7 @@ namespace dispatchkit { typedef typename boost::reference_wrapper::type > Result_Type; - Result_Type operator()(Boxed_Value ob) + static Result_Type cast(const Boxed_Value &ob) { if (ob.is_ref()) { @@ -290,7 +290,7 @@ namespace dispatchkit { typedef const Result * Result_Type; - Result_Type operator()(Boxed_Value ob) + static Result_Type cast(const Boxed_Value &ob) { if (ob.is_ref()) { @@ -306,7 +306,7 @@ namespace dispatchkit { typedef Result * Result_Type; - Result_Type operator()(Boxed_Value ob) + static Result_Type cast(const Boxed_Value &ob) { if (ob.is_ref()) { @@ -322,7 +322,7 @@ namespace dispatchkit { typedef typename boost::reference_wrapper Result_Type; - Result_Type operator()(Boxed_Value ob) + static Result_Type cast(const Boxed_Value &ob) { if (ob.is_ref()) { @@ -338,7 +338,7 @@ namespace dispatchkit { typedef typename boost::shared_ptr Result_Type; - Result_Type operator()(Boxed_Value ob) + static Result_Type cast(const Boxed_Value &ob) { return boost::any_cast >(ob.get()); } @@ -350,12 +350,17 @@ namespace dispatchkit { typedef Boxed_Value Result_Type; - Result_Type operator()(Boxed_Value ob) + static Result_Type cast(const Boxed_Value &ob) { return ob; } }; + template + typename Cast_Helper::Result_Type boxed_cast(const Boxed_Value &bv) + { + return Cast_Helper::cast(bv); + } struct Boxed_POD_Value { @@ -371,37 +376,37 @@ namespace dispatchkit if (inp_ == typeid(double)) { - d = Cast_Helper()(v); + d = boxed_cast(v); m_isfloat = true; } else if (inp_ == typeid(float)) { - d = Cast_Helper()(v); + d = boxed_cast(v); m_isfloat = true; } else if (inp_ == typeid(bool)) { - i = Cast_Helper()(v); + i = boxed_cast(v); } else if (inp_ == typeid(char)) { - i = Cast_Helper()(v); + i = boxed_cast(v); } else if (inp_ == typeid(int)) { - i = Cast_Helper()(v); + i = boxed_cast(v); } else if (inp_ == typeid(unsigned int)) { - i = Cast_Helper()(v); + i = boxed_cast(v); } else if (inp_ == typeid(long)) { - i = Cast_Helper()(v); + i = boxed_cast(v); } else if (inp_ == typeid(unsigned long)) { - i = Cast_Helper()(v); + i = boxed_cast(v); } else if (inp_ == typeid(int8_t)) { - i = Cast_Helper()(v); + i = boxed_cast(v); } else if (inp_ == typeid(int16_t)) { - i = Cast_Helper()(v); + i = boxed_cast(v); } else if (inp_ == typeid(int32_t)) { - i = Cast_Helper()(v); + i = boxed_cast(v); } else if (inp_ == typeid(int64_t)) { - i = Cast_Helper()(v); + i = boxed_cast(v); } else if (inp_ == typeid(uint8_t)) { - i = Cast_Helper()(v); + i = boxed_cast(v); } else if (inp_ == typeid(uint16_t)) { - i = Cast_Helper()(v); + i = boxed_cast(v); } else if (inp_ == typeid(uint32_t)) { - i = Cast_Helper()(v); + i = boxed_cast(v); } else { throw boost::bad_any_cast(); } @@ -487,7 +492,9 @@ namespace dispatchkit template<> struct Cast_Helper { - Boxed_POD_Value operator()(Boxed_Value ob) + typedef Boxed_POD_Value Result_Type; + + static Result_Type cast(const Boxed_Value &ob) { return Boxed_POD_Value(ob); } diff --git a/dispatchkit/dispatchkit.hpp b/dispatchkit/dispatchkit.hpp index 435ae0b..a4ebfa6 100644 --- a/dispatchkit/dispatchkit.hpp +++ b/dispatchkit/dispatchkit.hpp @@ -126,7 +126,7 @@ namespace dispatchkit funcs.insert(funcs.end(), Function_Map::value_type( t_name, - Cast_Helper()(get_object(t_name))) + boxed_cast(get_object(t_name))) ); } catch (const std::bad_cast &) { } catch (const std::range_error &) { diff --git a/dispatchkit/function_call.hpp b/dispatchkit/function_call.hpp index bbc63db..d6b9c2d 100644 --- a/dispatchkit/function_call.hpp +++ b/dispatchkit/function_call.hpp @@ -28,7 +28,7 @@ namespace dispatchkit Ret call(const std::vector > > &t_funcs, const std::vector ¶ms) { - return Cast_Helper()(dispatch(t_funcs, params)); + return boxed_cast(dispatch(t_funcs, params)); } }; @@ -66,7 +66,7 @@ namespace dispatchkit template boost::function build_functor(ScriptEngine &e, const std::string &script) { - return build_function_caller(Cast_Helper >()(e.evaluate_string(script))); + return build_function_caller(boxed_cast >(e.evaluate_string(script))); } } diff --git a/dispatchkit/proxy_functions.hpp b/dispatchkit/proxy_functions.hpp index ab22311..c207336 100644 --- a/dispatchkit/proxy_functions.hpp +++ b/dispatchkit/proxy_functions.hpp @@ -1,7 +1,7 @@ #include #define gettypeinfo(z,n,text) ti.push_back(Get_Type_Info::get()); -#define casthelper(z,n,text) ,Cast_Helper()(params[n]) +#define casthelper(z,n,text) ,dispatchkit::boxed_cast< Param ## n >(params[n]) #ifndef BOOST_PP_IS_ITERATING diff --git a/dispatchkit/test.cpp b/dispatchkit/test.cpp index a64fc86..0fdedc6 100644 --- a/dispatchkit/test.cpp +++ b/dispatchkit/test.cpp @@ -101,12 +101,12 @@ int main() //choose the most appropriate version of the function Boxed_Value addresult = dispatch(ss.get_function("+"), Param_List_Builder() << double(5.1) << double(10.3)); - //Using the Cast_Helper to unbox the resultant value and output it - std::cout << Cast_Helper()(addresult) << std::endl; + //Using the cast to unbox the resultant value and output it + std::cout << boxed_cast(addresult) << std::endl; //Using the Boxed_Value as input to another function, again with automatic dispatch. //This time we will not bother saving the result and will instead send it straight out - std::cout << Cast_Helper()( + std::cout << boxed_cast( dispatch(ss.get_function("*"), Param_List_Builder() << 2 << addresult) ) << std::endl; @@ -159,7 +159,7 @@ int main() boost::function show_message = build_function_caller(ss.get_function("show_message")); - Test &t = Cast_Helper()(ss.get_object("testobj2")); + Test &t = boxed_cast(ss.get_object("testobj2")); //Print the message the object was created with show_message(t); @@ -167,8 +167,8 @@ int main() //Now, get a reference to the object's stored message Boxed_Value stringref = dispatch(ss.get_function("get_message"), sos); - //Unbox it using Cast_Helper - std::string &sr = Cast_Helper()(stringref); + //Unbox it using boxed_cast + std::string &sr = boxed_cast(stringref); //Update the value of the reference sr = "Bob Updated The message"; @@ -176,8 +176,8 @@ int main() //Now, get a reference to the object's stored number Boxed_Value numberref= dispatch(ss.get_function("number"), sos); - //Unbox it using Cast_Helper - int &ir = Cast_Helper()(numberref); + //Unbox it using boxed_cast + int &ir = boxed_cast(numberref); std::cout << "Number: " << ir << std::endl; @@ -194,7 +194,7 @@ int main() //Call our newly named "add" function (which in turn dispatches +) std::cout << "Result of add function: " << - Cast_Helper()(dispatch(ss.get_function("add"), Param_List_Builder() << 5 << 2)) + boxed_cast(dispatch(ss.get_function("add"), Param_List_Builder() << 5 << 2)) << std::endl; diff --git a/dispatchkit/unittest.cpp b/dispatchkit/unittest.cpp index 42b707e..2314060 100644 --- a/dispatchkit/unittest.cpp +++ b/dispatchkit/unittest.cpp @@ -13,5 +13,5 @@ BOOST_AUTO_TEST_CASE( add_operators ) 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); + BOOST_CHECK_EQUAL(boxed_cast(dispatch(ss.get_function("+"), Param_List_Builder() << double(5.1) << double(10.3))), 15.4); } diff --git a/samples/sensors.chai b/samples/sensors.chai index f0a5478..e83db1b 100644 --- a/samples/sensors.chai +++ b/samples/sensors.chai @@ -14,7 +14,7 @@ def initialize_cpu_sensor(state, cpuname, sensor_manager) def update_cpu_state(state, statfile, cpuname) { - var regex = cpuname + "\s+([0-9]+)\s+([0-9]+)\s+([0-9]+)\s+([0-9]+)\s+([0-9]+)\s+([0-9]+)\s+([0-9]+)"; + var regex = cpuname + "\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)"; var strs = regex_search(statfile, regex); var user = to_double(strs[1]); @@ -55,11 +55,12 @@ def update_state(state) update_cpu_state(state, file, "cpu1"); } - var global_state = Map() + initialize_cpu_sensor(global_state, "cpu", sensor_manager); initialize_cpu_sensor(global_state, "cpu0", sensor_manager); initialize_cpu_sensor(global_state, "cpu1", sensor_manager); + sensor_manager.add_sensor("cpu", 500, global_state, function(state) { update_state(state); state["cpu"]; } ) sensor_manager.add_sensor("cpu0", 500, global_state, function(state) { state["cpu0"]; } ) sensor_manager.add_sensor("cpu1", 500, global_state, function(state) { state["cpu1"]; } )