diff --git a/cheatsheet.md b/cheatsheet.md index c37045f7..6314728e 100644 --- a/cheatsheet.md +++ b/cheatsheet.md @@ -84,6 +84,33 @@ chai.add(chaiscript::user_type(), "MyClass"); User defined type conversions are possible, defined in either script or in C++. + +### ChaiScript Defined Conversions + +Function objects (including lambdas) can be used to add type conversions +from inside of ChaiScript: + +``` +add_type_conversion(type("string"), type("Type_Info"), fun(s) { return type(s); }); +``` + +### C++ Defined Conversions + +Invoking a C++ type conversion possible with `static_cast` + +``` +chai.add(chaiscript::type_conversion()); +``` + +Calling a user defined type conversion that takes a lambda + +``` +chai.add(chaiscript::type_conversion([](const TestBaseType &t_bt) { /* return converted thing */ })); +``` + + +### Helpers + A helper function exists for strongly typed and ChaiScript `Vector` function conversion definition: ``` @@ -97,6 +124,7 @@ chai.add(chaiscript::map_conversion>()); ``` + This allows you to pass a ChaiScript function to a function requiring `std::vector` ## Adding Objects diff --git a/include/chaiscript/language/chaiscript_common.hpp b/include/chaiscript/language/chaiscript_common.hpp index b44a5a3b..65629382 100644 --- a/include/chaiscript/language/chaiscript_common.hpp +++ b/include/chaiscript/language/chaiscript_common.hpp @@ -489,9 +489,9 @@ namespace chaiscript } } - static bool get_bool_condition(const Boxed_Value &t_bv) { + static bool get_bool_condition(const Boxed_Value &t_bv, const chaiscript::detail::Dispatch_State &t_ss) { try { - return boxed_cast(t_bv); + return t_ss->boxed_cast(t_bv); } catch (const exception::bad_boxed_cast &) { throw exception::eval_error("Condition not boolean"); diff --git a/include/chaiscript/language/chaiscript_eval.hpp b/include/chaiscript/language/chaiscript_eval.hpp index 000257ec..ae26fec7 100644 --- a/include/chaiscript/language/chaiscript_eval.hpp +++ b/include/chaiscript/language/chaiscript_eval.hpp @@ -809,7 +809,7 @@ namespace chaiscript chaiscript::eval::detail::Scope_Push_Pop spp(t_ss); try { - while (get_bool_condition(this->children[0]->eval(t_ss))) { + while (get_bool_condition(this->children[0]->eval(t_ss), t_ss)) { try { this->children[1]->eval(t_ss); } catch (detail::Continue_Loop &) { @@ -851,7 +851,7 @@ namespace chaiscript { assert(children.size() == 3); } virtual ~Ternary_Cond_AST_Node() {} virtual Boxed_Value eval_internal(const chaiscript::detail::Dispatch_State &t_ss) const CHAISCRIPT_OVERRIDE { - if (get_bool_condition(children[0]->eval(t_ss))) { + if (get_bool_condition(children[0]->eval(t_ss), t_ss)) { return children[1]->eval(t_ss); } else { @@ -868,7 +868,7 @@ namespace chaiscript virtual ~If_AST_Node() {} virtual Boxed_Value eval_internal(const chaiscript::detail::Dispatch_State &t_ss) const CHAISCRIPT_OVERRIDE{ - if (get_bool_condition(children[0]->eval(t_ss))) { + if (get_bool_condition(children[0]->eval(t_ss), t_ss)) { return children[1]->eval(t_ss); } else { if (children.size() > 2) { @@ -878,7 +878,7 @@ namespace chaiscript return children[i+1]->eval(t_ss); } else if (children[i]->text == "else if") { - if (get_bool_condition(children[i+1]->eval(t_ss))) { + if (get_bool_condition(children[i+1]->eval(t_ss), t_ss)) { return children[i+2]->eval(t_ss); } } @@ -905,7 +905,7 @@ namespace chaiscript try { for ( children[0]->eval(t_ss); - get_bool_condition(children[1]->eval(t_ss)); + get_bool_condition(children[1]->eval(t_ss), t_ss); children[2]->eval(t_ss) ) { try { @@ -1498,8 +1498,8 @@ namespace chaiscript virtual ~Logical_And_AST_Node() {} virtual Boxed_Value eval_internal(const chaiscript::detail::Dispatch_State &t_ss) const CHAISCRIPT_OVERRIDE{ - return const_var(get_bool_condition(children[0]->eval(t_ss)) - && get_bool_condition(children[2]->eval(t_ss))); + return const_var(get_bool_condition(children[0]->eval(t_ss), t_ss) + && get_bool_condition(children[2]->eval(t_ss), t_ss)); } virtual std::string pretty_print() const CHAISCRIPT_OVERRIDE @@ -1515,8 +1515,8 @@ namespace chaiscript { assert(children.size() == 3); } virtual ~Logical_Or_AST_Node() {} virtual Boxed_Value eval_internal(const chaiscript::detail::Dispatch_State &t_ss) const CHAISCRIPT_OVERRIDE{ - return const_var(get_bool_condition(children[0]->eval(t_ss)) - || get_bool_condition(children[2]->eval(t_ss))); + return const_var(get_bool_condition(children[0]->eval(t_ss), t_ss) + || get_bool_condition(children[2]->eval(t_ss), t_ss)); } virtual std::string pretty_print() const CHAISCRIPT_OVERRIDE