Cleanup Cast_Helper and add new boxed_cast<>() function.

This commit is contained in:
Jason Turner 2009-06-23 02:47:47 +00:00
parent a5ecd3ceb0
commit d02620bb1b
9 changed files with 55 additions and 47 deletions

View File

@ -27,7 +27,7 @@ namespace chaiscript
std::string val; std::string val;
try { try {
val = dispatchkit::Cast_Helper<std::string &>()(vals[0]); val = dispatchkit::boxed_cast<std::string &>(vals[0]);
} }
catch (EvalError &ee) { catch (EvalError &ee) {
throw EvalError("Can not evaluate string: " + val + " reason: " + ee.reason, langkit::TokenPtr()); throw EvalError("Can not evaluate string: " + val + " reason: " + ee.reason, langkit::TokenPtr());

View File

@ -179,7 +179,7 @@ namespace chaiscript
bool cond; bool cond;
try { try {
retval = eval_token(ss, node->children[1]); retval = eval_token(ss, node->children[1]);
cond = dispatchkit::Cast_Helper<bool &>()(retval); cond = dispatchkit::boxed_cast<bool &>(retval);
} }
catch (std::exception) { catch (std::exception) {
throw EvalError("Boolean not('!') condition not boolean", node->children[0]); throw EvalError("Boolean not('!') condition not boolean", node->children[0]);
@ -320,7 +320,7 @@ namespace chaiscript
retval = eval_token(ss, node->children[0]); retval = eval_token(ss, node->children[0]);
bool cond; bool cond;
try { try {
cond = dispatchkit::Cast_Helper<bool &>()(retval); cond = dispatchkit::boxed_cast<bool &>(retval);
} }
catch (std::exception &e) { catch (std::exception &e) {
throw EvalError("If condition not boolean", node->children[0]); throw EvalError("If condition not boolean", node->children[0]);
@ -339,7 +339,7 @@ namespace chaiscript
else if (node->children[i]->text == "elseif") { else if (node->children[i]->text == "elseif") {
retval = eval_token(ss, node->children[i+1]); retval = eval_token(ss, node->children[i+1]);
try { try {
cond = dispatchkit::Cast_Helper<bool &>()(retval); cond = dispatchkit::boxed_cast<bool &>(retval);
} }
catch (std::exception &e) { catch (std::exception &e) {
throw EvalError("Elseif condition not boolean", node->children[i+1]); throw EvalError("Elseif condition not boolean", node->children[i+1]);
@ -358,7 +358,7 @@ namespace chaiscript
retval = eval_token(ss, node->children[0]); retval = eval_token(ss, node->children[0]);
bool cond; bool cond;
try { try {
cond = dispatchkit::Cast_Helper<bool &>()(retval); cond = dispatchkit::boxed_cast<bool &>(retval);
} }
catch (std::exception) { catch (std::exception) {
throw EvalError("While condition not boolean", node->children[0]); throw EvalError("While condition not boolean", node->children[0]);
@ -368,7 +368,7 @@ namespace chaiscript
eval_token(ss, node->children[1]); eval_token(ss, node->children[1]);
retval = eval_token(ss, node->children[0]); retval = eval_token(ss, node->children[0]);
try { try {
cond = dispatchkit::Cast_Helper<bool &>()(retval); cond = dispatchkit::boxed_cast<bool &>(retval);
} }
catch (std::exception) { catch (std::exception) {
throw EvalError("While condition not boolean", node->children[0]); throw EvalError("While condition not boolean", node->children[0]);
@ -393,7 +393,7 @@ namespace chaiscript
else if (node->children.size() == 3){ else if (node->children.size() == 3){
condition = eval_token(ss, node->children[0]); condition = eval_token(ss, node->children[0]);
} }
cond = dispatchkit::Cast_Helper<bool &>()(condition); cond = dispatchkit::boxed_cast<bool &>(condition);
} }
catch (std::exception &e) { catch (std::exception &e) {
throw EvalError("For condition not boolean", node); throw EvalError("For condition not boolean", node);
@ -410,7 +410,7 @@ namespace chaiscript
eval_token(ss, node->children[1]); eval_token(ss, node->children[1]);
condition = eval_token(ss, node->children[0]); condition = eval_token(ss, node->children[0]);
} }
cond = dispatchkit::Cast_Helper<bool &>()(condition); cond = dispatchkit::boxed_cast<bool &>(condition);
} }
catch (std::exception &e) { catch (std::exception &e) {

View File

@ -258,7 +258,7 @@ namespace dispatchkit
{ {
typedef typename boost::reference_wrapper<typename boost::add_const<Result>::type > Result_Type; typedef typename boost::reference_wrapper<typename boost::add_const<Result>::type > Result_Type;
Result_Type operator()(Boxed_Value ob) static Result_Type cast(const Boxed_Value &ob)
{ {
if (ob.is_ref()) if (ob.is_ref())
{ {
@ -274,7 +274,7 @@ namespace dispatchkit
{ {
typedef typename boost::reference_wrapper<typename boost::add_const<Result>::type > Result_Type; typedef typename boost::reference_wrapper<typename boost::add_const<Result>::type > Result_Type;
Result_Type operator()(Boxed_Value ob) static Result_Type cast(const Boxed_Value &ob)
{ {
if (ob.is_ref()) if (ob.is_ref())
{ {
@ -290,7 +290,7 @@ namespace dispatchkit
{ {
typedef const Result * Result_Type; typedef const Result * Result_Type;
Result_Type operator()(Boxed_Value ob) static Result_Type cast(const Boxed_Value &ob)
{ {
if (ob.is_ref()) if (ob.is_ref())
{ {
@ -306,7 +306,7 @@ namespace dispatchkit
{ {
typedef Result * Result_Type; typedef Result * Result_Type;
Result_Type operator()(Boxed_Value ob) static Result_Type cast(const Boxed_Value &ob)
{ {
if (ob.is_ref()) if (ob.is_ref())
{ {
@ -322,7 +322,7 @@ namespace dispatchkit
{ {
typedef typename boost::reference_wrapper<Result> Result_Type; typedef typename boost::reference_wrapper<Result> Result_Type;
Result_Type operator()(Boxed_Value ob) static Result_Type cast(const Boxed_Value &ob)
{ {
if (ob.is_ref()) if (ob.is_ref())
{ {
@ -338,7 +338,7 @@ namespace dispatchkit
{ {
typedef typename boost::shared_ptr<Result> Result_Type; typedef typename boost::shared_ptr<Result> Result_Type;
Result_Type operator()(Boxed_Value ob) static Result_Type cast(const Boxed_Value &ob)
{ {
return boost::any_cast<boost::shared_ptr<Result> >(ob.get()); return boost::any_cast<boost::shared_ptr<Result> >(ob.get());
} }
@ -350,12 +350,17 @@ namespace dispatchkit
{ {
typedef Boxed_Value Result_Type; typedef Boxed_Value Result_Type;
Result_Type operator()(Boxed_Value ob) static Result_Type cast(const Boxed_Value &ob)
{ {
return ob; return ob;
} }
}; };
template<typename Type>
typename Cast_Helper<Type>::Result_Type boxed_cast(const Boxed_Value &bv)
{
return Cast_Helper<Type>::cast(bv);
}
struct Boxed_POD_Value struct Boxed_POD_Value
{ {
@ -371,37 +376,37 @@ namespace dispatchkit
if (inp_ == typeid(double)) if (inp_ == typeid(double))
{ {
d = Cast_Helper<double>()(v); d = boxed_cast<double>(v);
m_isfloat = true; m_isfloat = true;
} else if (inp_ == typeid(float)) { } else if (inp_ == typeid(float)) {
d = Cast_Helper<float>()(v); d = boxed_cast<float>(v);
m_isfloat = true; m_isfloat = true;
} else if (inp_ == typeid(bool)) { } else if (inp_ == typeid(bool)) {
i = Cast_Helper<bool>()(v); i = boxed_cast<bool>(v);
} else if (inp_ == typeid(char)) { } else if (inp_ == typeid(char)) {
i = Cast_Helper<char>()(v); i = boxed_cast<char>(v);
} else if (inp_ == typeid(int)) { } else if (inp_ == typeid(int)) {
i = Cast_Helper<int>()(v); i = boxed_cast<int>(v);
} else if (inp_ == typeid(unsigned int)) { } else if (inp_ == typeid(unsigned int)) {
i = Cast_Helper<unsigned int>()(v); i = boxed_cast<unsigned int>(v);
} else if (inp_ == typeid(long)) { } else if (inp_ == typeid(long)) {
i = Cast_Helper<long>()(v); i = boxed_cast<long>(v);
} else if (inp_ == typeid(unsigned long)) { } else if (inp_ == typeid(unsigned long)) {
i = Cast_Helper<unsigned long>()(v); i = boxed_cast<unsigned long>(v);
} else if (inp_ == typeid(int8_t)) { } else if (inp_ == typeid(int8_t)) {
i = Cast_Helper<int8_t>()(v); i = boxed_cast<int8_t>(v);
} else if (inp_ == typeid(int16_t)) { } else if (inp_ == typeid(int16_t)) {
i = Cast_Helper<int16_t>()(v); i = boxed_cast<int16_t>(v);
} else if (inp_ == typeid(int32_t)) { } else if (inp_ == typeid(int32_t)) {
i = Cast_Helper<int32_t>()(v); i = boxed_cast<int32_t>(v);
} else if (inp_ == typeid(int64_t)) { } else if (inp_ == typeid(int64_t)) {
i = Cast_Helper<int64_t>()(v); i = boxed_cast<int64_t>(v);
} else if (inp_ == typeid(uint8_t)) { } else if (inp_ == typeid(uint8_t)) {
i = Cast_Helper<uint8_t>()(v); i = boxed_cast<uint8_t>(v);
} else if (inp_ == typeid(uint16_t)) { } else if (inp_ == typeid(uint16_t)) {
i = Cast_Helper<uint16_t>()(v); i = boxed_cast<uint16_t>(v);
} else if (inp_ == typeid(uint32_t)) { } else if (inp_ == typeid(uint32_t)) {
i = Cast_Helper<uint32_t>()(v); i = boxed_cast<uint32_t>(v);
} else { } else {
throw boost::bad_any_cast(); throw boost::bad_any_cast();
} }
@ -487,7 +492,9 @@ namespace dispatchkit
template<> template<>
struct Cast_Helper<Boxed_POD_Value> struct Cast_Helper<Boxed_POD_Value>
{ {
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); return Boxed_POD_Value(ob);
} }

View File

@ -126,7 +126,7 @@ namespace dispatchkit
funcs.insert(funcs.end(), funcs.insert(funcs.end(),
Function_Map::value_type( Function_Map::value_type(
t_name, t_name,
Cast_Helper<Function_Map::mapped_type>()(get_object(t_name))) boxed_cast<Function_Map::mapped_type>(get_object(t_name)))
); );
} catch (const std::bad_cast &) { } catch (const std::bad_cast &) {
} catch (const std::range_error &) { } catch (const std::range_error &) {

View File

@ -28,7 +28,7 @@ namespace dispatchkit
Ret call(const std::vector<std::pair<std::string, boost::shared_ptr<Proxy_Function> > > &t_funcs, Ret call(const std::vector<std::pair<std::string, boost::shared_ptr<Proxy_Function> > > &t_funcs,
const std::vector<Boxed_Value> &params) const std::vector<Boxed_Value> &params)
{ {
return Cast_Helper<Ret>()(dispatch(t_funcs, params)); return boxed_cast<Ret>(dispatch(t_funcs, params));
} }
}; };
@ -66,7 +66,7 @@ namespace dispatchkit
template<typename FunctionType, typename ScriptEngine> template<typename FunctionType, typename ScriptEngine>
boost::function<FunctionType> build_functor(ScriptEngine &e, const std::string &script) boost::function<FunctionType> build_functor(ScriptEngine &e, const std::string &script)
{ {
return build_function_caller<FunctionType>(Cast_Helper<boost::shared_ptr<Proxy_Function> >()(e.evaluate_string(script))); return build_function_caller<FunctionType>(boxed_cast<boost::shared_ptr<Proxy_Function> >(e.evaluate_string(script)));
} }
} }

View File

@ -1,7 +1,7 @@
#include <boost/preprocessor.hpp> #include <boost/preprocessor.hpp>
#define gettypeinfo(z,n,text) ti.push_back(Get_Type_Info<Param ## n>::get()); #define gettypeinfo(z,n,text) ti.push_back(Get_Type_Info<Param ## n>::get());
#define casthelper(z,n,text) ,Cast_Helper<Param ## n>()(params[n]) #define casthelper(z,n,text) ,dispatchkit::boxed_cast< Param ## n >(params[n])
#ifndef BOOST_PP_IS_ITERATING #ifndef BOOST_PP_IS_ITERATING

View File

@ -101,12 +101,12 @@ int main()
//choose the most appropriate version of the function //choose the most appropriate version of the function
Boxed_Value addresult = dispatch(ss.get_function("+"), Param_List_Builder() << double(5.1) << double(10.3)); 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 //Using the cast to unbox the resultant value and output it
std::cout << Cast_Helper<double>()(addresult) << std::endl; std::cout << boxed_cast<double>(addresult) << std::endl;
//Using the Boxed_Value as input to another function, again with automatic dispatch. //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 //This time we will not bother saving the result and will instead send it straight out
std::cout << Cast_Helper<double>()( std::cout << boxed_cast<double>(
dispatch(ss.get_function("*"), Param_List_Builder() << 2 << addresult) dispatch(ss.get_function("*"), Param_List_Builder() << 2 << addresult)
) << std::endl; ) << std::endl;
@ -159,7 +159,7 @@ int main()
boost::function<void (Test &)> show_message = boost::function<void (Test &)> show_message =
build_function_caller<void (Test &)>(ss.get_function("show_message")); build_function_caller<void (Test &)>(ss.get_function("show_message"));
Test &t = Cast_Helper<Test &>()(ss.get_object("testobj2")); Test &t = boxed_cast<Test &>(ss.get_object("testobj2"));
//Print the message the object was created with //Print the message the object was created with
show_message(t); show_message(t);
@ -167,8 +167,8 @@ int main()
//Now, get a reference to the object's stored message //Now, get a reference to the object's stored message
Boxed_Value stringref = dispatch(ss.get_function("get_message"), sos); Boxed_Value stringref = dispatch(ss.get_function("get_message"), sos);
//Unbox it using Cast_Helper //Unbox it using boxed_cast
std::string &sr = Cast_Helper<std::string &>()(stringref); std::string &sr = boxed_cast<std::string &>(stringref);
//Update the value of the reference //Update the value of the reference
sr = "Bob Updated The message"; sr = "Bob Updated The message";
@ -176,8 +176,8 @@ int main()
//Now, get a reference to the object's stored number //Now, get a reference to the object's stored number
Boxed_Value numberref= dispatch(ss.get_function("number"), sos); Boxed_Value numberref= dispatch(ss.get_function("number"), sos);
//Unbox it using Cast_Helper //Unbox it using boxed_cast
int &ir = Cast_Helper<int &>()(numberref); int &ir = boxed_cast<int &>(numberref);
std::cout << "Number: " << ir << std::endl; std::cout << "Number: " << ir << std::endl;
@ -194,7 +194,7 @@ int main()
//Call our newly named "add" function (which in turn dispatches +) //Call our newly named "add" function (which in turn dispatches +)
std::cout << "Result of add function: " << std::cout << "Result of add function: " <<
Cast_Helper<int>()(dispatch(ss.get_function("add"), Param_List_Builder() << 5 << 2)) boxed_cast<int>(dispatch(ss.get_function("add"), Param_List_Builder() << 5 << 2))
<< std::endl; << std::endl;

View File

@ -13,5 +13,5 @@ BOOST_AUTO_TEST_CASE( add_operators )
Bootstrap::bootstrap(ss); Bootstrap::bootstrap(ss);
dump_system(ss); dump_system(ss);
BOOST_CHECK_EQUAL(Cast_Helper<int>()(dispatch(ss.get_function("+"), Param_List_Builder() << double(5.1) << double(10.3))), 15.4); BOOST_CHECK_EQUAL(boxed_cast<int>(dispatch(ss.get_function("+"), Param_List_Builder() << double(5.1) << double(10.3))), 15.4);
} }

View File

@ -14,7 +14,7 @@ def initialize_cpu_sensor(state, cpuname, sensor_manager)
def update_cpu_state(state, statfile, cpuname) 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 strs = regex_search(statfile, regex);
var user = to_double(strs[1]); var user = to_double(strs[1]);
@ -55,11 +55,12 @@ def update_state(state)
update_cpu_state(state, file, "cpu1"); update_cpu_state(state, file, "cpu1");
} }
var global_state = Map() var global_state = Map()
initialize_cpu_sensor(global_state, "cpu", sensor_manager); initialize_cpu_sensor(global_state, "cpu", sensor_manager);
initialize_cpu_sensor(global_state, "cpu0", sensor_manager); initialize_cpu_sensor(global_state, "cpu0", sensor_manager);
initialize_cpu_sensor(global_state, "cpu1", 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("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("cpu0", 500, global_state, function(state) { state["cpu0"]; } )
sensor_manager.add_sensor("cpu1", 500, global_state, function(state) { state["cpu1"]; } ) sensor_manager.add_sensor("cpu1", 500, global_state, function(state) { state["cpu1"]; } )