Remove remaining uses of std::function

This commit is contained in:
Jason Turner 2015-06-23 13:02:43 -06:00
parent a112d97141
commit b53432cf28
4 changed files with 124 additions and 76 deletions

View File

@ -520,7 +520,7 @@ namespace chaiscript
m->add(fun(&print), "print_string"); m->add(fun(&print), "print_string");
m->add(fun(&println), "println_string"); m->add(fun(&println), "println_string");
m->add(chaiscript::make_shared<dispatch::Proxy_Function_Base, dispatch::Dynamic_Proxy_Function>(&bind_function), "bind"); m->add(dispatch::make_dynamic_proxy_function(&bind_function), "bind");
m->add(fun(&shared_ptr_unconst_clone<dispatch::Proxy_Function_Base>), "clone"); m->add(fun(&shared_ptr_unconst_clone<dispatch::Proxy_Function_Base>), "clone");
m->add(fun(&ptr_assign<std::remove_const<dispatch::Proxy_Function_Base>::type>), "="); m->add(fun(&ptr_assign<std::remove_const<dispatch::Proxy_Function_Base>::type>), "=");

View File

@ -302,7 +302,6 @@ namespace chaiscript
{ {
public: public:
Dynamic_Proxy_Function( Dynamic_Proxy_Function(
std::function<Boxed_Value (const std::vector<Boxed_Value> &)> t_f,
int t_arity=-1, int t_arity=-1,
AST_NodePtr t_parsenode = AST_NodePtr(), AST_NodePtr t_parsenode = AST_NodePtr(),
Param_Types t_param_types = Param_Types(), Param_Types t_param_types = Param_Types(),
@ -310,8 +309,7 @@ namespace chaiscript
Proxy_Function t_guard = Proxy_Function()) Proxy_Function t_guard = Proxy_Function())
: Proxy_Function_Base(build_param_type_list(t_param_types), t_arity), : Proxy_Function_Base(build_param_type_list(t_param_types), t_arity),
m_param_types(std::move(t_param_types)), m_param_types(std::move(t_param_types)),
m_guard(std::move(t_guard)), m_parsenode(std::move(t_parsenode)), m_description(std::move(t_description)), m_guard(std::move(t_guard)), m_parsenode(std::move(t_parsenode)), m_description(std::move(t_description))
m_f(std::move(t_f))
{ {
} }
@ -350,6 +348,75 @@ namespace chaiscript
return m_description; return m_description;
} }
protected:
bool test_guard(const std::vector<Boxed_Value> &params, const Type_Conversions &t_conversions) const
{
if (m_guard)
{
try {
return boxed_cast<bool>((*m_guard)(params, t_conversions));
} catch (const exception::arity_error &) {
return false;
} catch (const exception::bad_boxed_cast &) {
return false;
}
} else {
return true;
}
}
private:
static std::vector<Type_Info> build_param_type_list(const Param_Types &t_types)
{
// For the return type
std::vector<Type_Info> types{chaiscript::detail::Get_Type_Info<Boxed_Value>::get()};
for (const auto &t : t_types.types())
{
if (t.second.is_undef()) {
types.push_back(chaiscript::detail::Get_Type_Info<Boxed_Value>::get());
} else {
types.push_back(t.second);
}
}
return types;
}
Param_Types m_param_types;
Proxy_Function m_guard;
AST_NodePtr m_parsenode;
std::string m_description;
};
template<typename Callable>
class Dynamic_Proxy_Function_Impl : public Dynamic_Proxy_Function
{
public:
Dynamic_Proxy_Function_Impl(
Callable t_f,
int t_arity=-1,
AST_NodePtr t_parsenode = AST_NodePtr(),
Param_Types t_param_types = Param_Types(),
std::string t_description = "",
Proxy_Function t_guard = Proxy_Function())
: Dynamic_Proxy_Function(
t_arity,
std::move(t_parsenode),
std::move(t_param_types),
std::move(t_description),
std::move(t_guard)
),
m_f(std::move(t_f))
{
}
virtual ~Dynamic_Proxy_Function_Impl() {}
protected: protected:
virtual Boxed_Value do_call(const std::vector<Boxed_Value> &params, const Type_Conversions &t_conversions) const CHAISCRIPT_OVERRIDE virtual Boxed_Value do_call(const std::vector<Boxed_Value> &params, const Type_Conversions &t_conversions) const CHAISCRIPT_OVERRIDE
{ {
@ -368,48 +435,17 @@ namespace chaiscript
} }
private: private:
bool test_guard(const std::vector<Boxed_Value> &params, const Type_Conversions &t_conversions) const
{
if (m_guard)
{
try {
return boxed_cast<bool>((*m_guard)(params, t_conversions));
} catch (const exception::arity_error &) {
return false;
} catch (const exception::bad_boxed_cast &) {
return false;
}
} else {
return true;
}
}
static std::vector<Type_Info> build_param_type_list(const Param_Types &t_types) Callable m_f;
{
std::vector<Type_Info> types;
// For the return type
types.push_back(chaiscript::detail::Get_Type_Info<Boxed_Value>::get());
for (const auto &t : t_types.types())
{
if (t.second.is_undef()) {
types.push_back(chaiscript::detail::Get_Type_Info<Boxed_Value>::get());
} else {
types.push_back(t.second);
}
}
return types;
}
Param_Types m_param_types;
Proxy_Function m_guard;
AST_NodePtr m_parsenode;
std::string m_description;
std::function<Boxed_Value (const std::vector<Boxed_Value> &)> m_f;
}; };
template<typename Callable, typename ... Arg>
Proxy_Function make_dynamic_proxy_function(Callable &&c, Arg&& ... a)
{
return chaiscript::make_shared<dispatch::Proxy_Function_Base, dispatch::Dynamic_Proxy_Function_Impl<Callable>>(
std::forward<Callable>(c), std::forward<Arg>(a)...);
}
/// An object used by Bound_Function to represent "_" parameters /// An object used by Bound_Function to represent "_" parameters
/// of a binding. This allows for unbound parameters during bind. /// of a binding. This allows for unbound parameters during bind.
struct Placeholder_Object struct Placeholder_Object

View File

@ -363,10 +363,12 @@ namespace chaiscript
m_engine.add(fun([this](){ return m_engine.get_function_objects(); }), "get_functions"); m_engine.add(fun([this](){ return m_engine.get_function_objects(); }), "get_functions");
m_engine.add(fun([this](){ return m_engine.get_scripting_objects(); }), "get_objects"); m_engine.add(fun([this](){ return m_engine.get_scripting_objects(); }), "get_objects");
m_engine.add(chaiscript::make_shared<dispatch::Proxy_Function_Base, dispatch::Dynamic_Proxy_Function>( m_engine.add(
dispatch::make_dynamic_proxy_function(
[this](const std::vector<Boxed_Value> &t_params) { [this](const std::vector<Boxed_Value> &t_params) {
return m_engine.call_exists(t_params); return m_engine.call_exists(t_params);
}), "call_exists"); })
, "call_exists");
// m_engine.add(fun<Boxed_Value (const dispatch::Proxy_Function_Base *, const std::vector<Boxed_Value> &)>(std::bind(&chaiscript::dispatch::Proxy_Function_Base::operator(), std::placeholders::_1, std::placeholders::_2, std::ref(m_engine.conversions()))), "call"); // m_engine.add(fun<Boxed_Value (const dispatch::Proxy_Function_Base *, const std::vector<Boxed_Value> &)>(std::bind(&chaiscript::dispatch::Proxy_Function_Base::operator(), std::placeholders::_1, std::placeholders::_2, std::ref(m_engine.conversions()))), "call");
// //

View File

@ -739,12 +739,15 @@ namespace chaiscript
const auto &lambda_node = this->children.back(); const auto &lambda_node = this->children.back();
return Boxed_Value(chaiscript::make_shared<dispatch::Proxy_Function_Base, dispatch::Dynamic_Proxy_Function>( return Boxed_Value(
[&t_ss, lambda_node, param_names, captures](const std::vector<Boxed_Value> &t_params) dispatch::make_dynamic_proxy_function(
{ [&t_ss, lambda_node, param_names, captures](const std::vector<Boxed_Value> &t_params)
return detail::eval_function(t_ss, lambda_node, param_names, t_params, captures); {
}, return detail::eval_function(t_ss, lambda_node, param_names, t_params, captures);
static_cast<int>(numparams), lambda_node, param_types)); },
static_cast<int>(numparams), lambda_node, param_types
)
);
} }
private: private:
@ -801,25 +804,28 @@ namespace chaiscript
} }
} }
std::shared_ptr<dispatch::Dynamic_Proxy_Function> guard; std::shared_ptr<dispatch::Proxy_Function_Base> guard;
if (guardnode) { if (guardnode) {
guard = std::make_shared<dispatch::Dynamic_Proxy_Function> guard = dispatch::make_dynamic_proxy_function(
([&t_ss, guardnode, t_param_names](const std::vector<Boxed_Value> &t_params) [&t_ss, guardnode, t_param_names](const std::vector<Boxed_Value> &t_params)
{ {
return detail::eval_function(t_ss, guardnode, t_param_names, t_params); return detail::eval_function(t_ss, guardnode, t_param_names, t_params);
}, static_cast<int>(numparams), guardnode); },
static_cast<int>(numparams), guardnode);
} }
try { try {
const std::string & l_function_name = this->children[0]->text; const std::string & l_function_name = this->children[0]->text;
const std::string & l_annotation = this->annotation?this->annotation->text:""; const std::string & l_annotation = this->annotation?this->annotation->text:"";
const auto & func_node = this->children.back(); const auto & func_node = this->children.back();
t_ss.add(chaiscript::make_shared<dispatch::Proxy_Function_Base, dispatch::Dynamic_Proxy_Function> t_ss.add(
([&t_ss, guardnode, func_node, t_param_names](const std::vector<Boxed_Value> &t_params) dispatch::make_dynamic_proxy_function(
{ [&t_ss, guardnode, func_node, t_param_names](const std::vector<Boxed_Value> &t_params)
return detail::eval_function(t_ss, func_node, t_param_names, t_params); {
}, static_cast<int>(numparams), this->children.back(), return detail::eval_function(t_ss, func_node, t_param_names, t_params);
param_types, l_annotation, guard), l_function_name); },
static_cast<int>(numparams), this->children.back(),
param_types, l_annotation, guard), l_function_name);
} }
catch (const exception::reserved_word_error &e) { catch (const exception::reserved_word_error &e) {
throw exception::eval_error("Reserved word used as function name '" + e.word() + "'"); throw exception::eval_error("Reserved word used as function name '" + e.word() + "'");
@ -1399,12 +1405,13 @@ namespace chaiscript
const size_t numparams = t_param_names.size(); const size_t numparams = t_param_names.size();
std::shared_ptr<dispatch::Dynamic_Proxy_Function> guard; std::shared_ptr<dispatch::Proxy_Function_Base> guard;
if (guardnode) { if (guardnode) {
guard = std::make_shared<dispatch::Dynamic_Proxy_Function> guard = dispatch::make_dynamic_proxy_function(
([&t_ss, t_param_names, guardnode](const std::vector<Boxed_Value> &t_params) { [&t_ss, t_param_names, guardnode](const std::vector<Boxed_Value> &t_params) {
return chaiscript::eval::detail::eval_function(t_ss, guardnode, t_param_names, t_params, std::map<std::string, Boxed_Value>()); return chaiscript::eval::detail::eval_function(t_ss, guardnode, t_param_names, t_params, std::map<std::string, Boxed_Value>());
}, static_cast<int>(numparams), guardnode); },
static_cast<int>(numparams), guardnode);
} }
try { try {
@ -1415,12 +1422,15 @@ namespace chaiscript
if (function_name == class_name) { if (function_name == class_name) {
param_types.push_front(class_name, Type_Info()); param_types.push_front(class_name, Type_Info());
t_ss.add(std::make_shared<dispatch::detail::Dynamic_Object_Constructor>(class_name, t_ss.add(
std::make_shared<dispatch::Dynamic_Proxy_Function>( std::make_shared<dispatch::detail::Dynamic_Object_Constructor>(class_name,
[&t_ss, t_param_names, node](const std::vector<Boxed_Value> &t_params) { dispatch::make_dynamic_proxy_function(
return chaiscript::eval::detail::eval_function(t_ss, node, t_param_names, t_params, std::map<std::string, Boxed_Value>()); [&t_ss, t_param_names, node](const std::vector<Boxed_Value> &t_params) {
}, return chaiscript::eval::detail::eval_function(t_ss, node, t_param_names, t_params, std::map<std::string, Boxed_Value>());
static_cast<int>(numparams), node, param_types, l_annotation, guard)), },
static_cast<int>(numparams), node, param_types, l_annotation, guard
)
),
function_name); function_name);
} else { } else {
@ -1429,8 +1439,8 @@ namespace chaiscript
auto type = t_ss.get_type(class_name, false); auto type = t_ss.get_type(class_name, false);
param_types.push_front(class_name, type); param_types.push_front(class_name, type);
t_ss.add(std::make_shared<dispatch::detail::Dynamic_Object_Function>(class_name, t_ss.add(std::make_shared<dispatch::detail::Dynamic_Object_Function>(class_name,
std::make_shared<dispatch::Dynamic_Proxy_Function>( dispatch::make_dynamic_proxy_function(
[&t_ss, t_param_names, node](const std::vector<Boxed_Value> &t_params) { [&t_ss, t_param_names, node](const std::vector<Boxed_Value> &t_params) {
return chaiscript::eval::detail::eval_function(t_ss, node, t_param_names, t_params, std::map<std::string, Boxed_Value>()); return chaiscript::eval::detail::eval_function(t_ss, node, t_param_names, t_params, std::map<std::string, Boxed_Value>());
}, },