Completely remove Proxy_Function_Impl

This commit is contained in:
Jason Turner 2015-05-22 12:13:49 -06:00
parent 0b812942d4
commit df724b5c33
6 changed files with 21 additions and 130 deletions

View File

@ -7,9 +7,20 @@
#ifndef CHAISCRIPT_CALLABLE_TRAITS_HPP_ #ifndef CHAISCRIPT_CALLABLE_TRAITS_HPP_
#define CHAISCRIPT_CALLABLE_TRAITS_HPP_ #define CHAISCRIPT_CALLABLE_TRAITS_HPP_
#include <memory>
namespace chaiscript { namespace chaiscript {
namespace dispatch { namespace dispatch {
namespace detail { namespace detail {
template<typename Class, typename ... Param>
struct Constructor
{
template<typename ... Inner>
std::shared_ptr<Class> operator()(Inner&& ... inner) const {
return std::make_shared<Class>(std::forward<Inner>(inner)...);
}
};
template<typename Ret, typename Class, typename ... Param> template<typename Ret, typename Class, typename ... Param>
struct Const_Caller struct Const_Caller
{ {

View File

@ -95,6 +95,7 @@ namespace chaiscript
template<typename Ret, typename ... Params> template<typename Ret, typename ... Params>
std::function<Ret (Params...)> build_function_caller_helper(Ret (Params...), const std::vector<Const_Proxy_Function> &funcs, const Type_Conversions *t_conversions) std::function<Ret (Params...)> build_function_caller_helper(Ret (Params...), const std::vector<Const_Proxy_Function> &funcs, const Type_Conversions *t_conversions)
{ {
/*
if (funcs.size() == 1) if (funcs.size() == 1)
{ {
std::shared_ptr<const Proxy_Function_Impl<Ret (Params...)>> pfi = std::shared_ptr<const Proxy_Function_Impl<Ret (Params...)>> pfi =
@ -108,6 +109,7 @@ namespace chaiscript
// looks like this either wasn't a Proxy_Function_Impl or the types didn't match // looks like this either wasn't a Proxy_Function_Impl or the types didn't match
// we cannot make any other guesses or assumptions really, so continuing // we cannot make any other guesses or assumptions really, so continuing
} }
*/
return std::function<Ret (Params...)>(Build_Function_Caller_Helper<Ret, Params...>(funcs, t_conversions?*t_conversions:Type_Conversions())); return std::function<Ret (Params...)>(Build_Function_Caller_Helper<Ret, Params...>(funcs, t_conversions?*t_conversions:Type_Conversions()));
} }

View File

@ -23,7 +23,7 @@ namespace chaiscript
{ {
namespace dispatch namespace dispatch
{ {
template<class T> class Proxy_Function_Impl; template<class T, class U> class Proxy_Function_Callable_Impl;
template<class T> class Assignable_Proxy_Function_Impl; template<class T> class Assignable_Proxy_Function_Impl;
namespace detail namespace detail
@ -54,7 +54,7 @@ namespace chaiscript
{ {
static Boxed_Value handle(const std::function<Ret> &f) { static Boxed_Value handle(const std::function<Ret> &f) {
return Boxed_Value( return Boxed_Value(
chaiscript::make_shared<dispatch::Proxy_Function_Base, dispatch::Proxy_Function_Impl<Ret>>(f) chaiscript::make_shared<dispatch::Proxy_Function_Base, dispatch::Proxy_Function_Callable_Impl<Ret, std::function<Ret>>>(f)
); );
} }
}; };
@ -64,7 +64,7 @@ namespace chaiscript
{ {
static Boxed_Value handle(const std::function<Ret> &f) { static Boxed_Value handle(const std::function<Ret> &f) {
return Boxed_Value( return Boxed_Value(
chaiscript::make_shared<dispatch::Proxy_Function_Base, dispatch::Proxy_Function_Impl<Ret>>(f) chaiscript::make_shared<dispatch::Proxy_Function_Base, dispatch::Proxy_Function_Callable_Impl<Ret, std::function<Ret>>>(f)
); );
} }
}; };
@ -111,7 +111,7 @@ namespace chaiscript
static Boxed_Value handle(const std::function<Ret> &f) { static Boxed_Value handle(const std::function<Ret> &f) {
return Boxed_Value( return Boxed_Value(
chaiscript::make_shared<dispatch::Proxy_Function_Base, dispatch::Proxy_Function_Impl<Ret>>(f) chaiscript::make_shared<dispatch::Proxy_Function_Base, dispatch::Proxy_Function_Callable_Impl<Ret, std::function<Ret>>>(f)
); );
} }
}; };

View File

@ -16,21 +16,14 @@ namespace chaiscript
{ {
namespace detail namespace detail
{ {
/**
* A constructor function, used for creating a new object
* of a given type with a given set of params
*/
template<typename Class, typename ... Params>
std::shared_ptr<Class> constructor_(Params ... params)
{
return std::make_shared<Class>(params...);
}
template<typename Class, typename ... Params > template<typename Class, typename ... Params >
Proxy_Function build_constructor_(Class (*)(Params...)) Proxy_Function build_constructor_(Class (*)(Params...))
{ {
typedef std::shared_ptr<Class> (sig)(Params...); auto call = dispatch::detail::Constructor<Class, Params...>();
return Proxy_Function(static_cast<Proxy_Function_Impl_Base *>(new Proxy_Function_Impl<sig>(std::function<sig>(&(constructor_<Class, Params...>)))));
return Proxy_Function(
chaiscript::make_shared<dispatch::Proxy_Function_Base, dispatch::Proxy_Function_Callable_Impl<std::shared_ptr<Class> (Params...), decltype(call)>>(call));
} }
} }
} }

View File

@ -588,47 +588,6 @@ namespace chaiscript
Func *m_dummy_func; Func *m_dummy_func;
}; };
/// The standard typesafe function call implementation of Proxy_Function
/// It takes a std::function<> object and performs runtime
/// type checking of Boxed_Value parameters, in a type safe manner
template<typename Func>
class Proxy_Function_Impl : public Proxy_Function_Impl_Base
{
public:
Proxy_Function_Impl(std::function<Func> f)
: Proxy_Function_Impl_Base(detail::build_param_type_list(static_cast<Func *>(nullptr))),
m_f(std::move(f)), m_dummy_func(nullptr)
{
}
virtual ~Proxy_Function_Impl() {}
virtual bool compare_types_with_cast(const std::vector<Boxed_Value> &vals, const Type_Conversions &t_conversions) const CHAISCRIPT_OVERRIDE
{
return detail::compare_types_cast(m_dummy_func, vals, t_conversions);
}
virtual bool operator==(const Proxy_Function_Base &t_func) const CHAISCRIPT_OVERRIDE
{
return dynamic_cast<const Proxy_Function_Impl<Func> *>(&t_func) != nullptr;
}
std::function<Func> internal_function() const
{
return m_f;
}
protected:
virtual Boxed_Value do_call(const std::vector<Boxed_Value> &params, const Type_Conversions &t_conversions) const CHAISCRIPT_OVERRIDE
{
return detail::Do_Call<typename std::function<Func>::result_type>::template go<Func>(m_f, params, t_conversions);
}
private:
std::function<Func> m_f;
Func *m_dummy_func;
};
class Assignable_Proxy_Function : public Proxy_Function_Impl_Base class Assignable_Proxy_Function : public Proxy_Function_Impl_Base
{ {

View File

@ -15,76 +15,6 @@
namespace chaiscript namespace chaiscript
{ {
namespace dispatch
{
namespace detail
{
template<typename T>
struct FunctionSignature
{
};
template<typename Sig>
struct FunctionSignature<std::function<Sig> >
{
typedef Sig Signature;
};
template<typename Ret, typename ... Args>
std::function<Ret (Args...) > to_function(Ret (*func)(Args...))
{
return std::function<Ret (Args...)>(func);
}
template<typename Ret, typename Class, typename ... Args>
std::function<Ret (Class &, Args...) > to_function(Ret (Class::*func)(Args...))
{
#ifdef CHAISCRIPT_MSVC
/// \todo this std::mem_fn wrap shouldn't be necessary but type conversions for
/// std::function for member function pointers seems to be broken in MSVC
return std::function<Ret(Class &, Args...)>(std::mem_fn(func));
#else
return std::function<Ret(Class &, Args...)>(func);
#endif
}
template<typename Ret, typename Class, typename ... Args>
std::function<Ret (const Class &, Args...) > to_function(Ret (Class::*func)(Args...) const)
{
#if defined(CHAISCRIPT_MSVC) || defined(CHAISCRIPT_LIBCPP)
/// \todo this std::mem_fn wrap shouldn't be necessary but type conversions for
/// std::function for member function pointers seems to be broken in MSVC
return std::function<Ret (const Class &, Args...)>([func](const Class &o, Args... args)->Ret {
return (o.*func)(std::forward<Args>(args)...);
});
#else
return std::function<Ret(const Class &, Args...)>(func);
#endif
}
template<typename T, typename Ret, typename Class, typename ... Args>
std::function<Ret (Args...)> to_function_callable(Ret (Class::*)(Args...), T t)
{
return std::function<Ret (Args...)>(t);
}
template<typename T, typename Ret, typename Class, typename ... Args>
std::function<Ret (Args...)> to_function_callable(Ret (Class::*)(Args...) const, T t)
{
return std::function<Ret (Args...)>(t);
}
template<typename T>
auto to_function(T t) -> decltype(to_function_callable(&T::operator(), t))
{
return to_function_callable(&T::operator(), t);
}
}
}
/// \brief Creates a new Proxy_Function object from a free function, member function or data member /// \brief Creates a new Proxy_Function object from a free function, member function or data member
/// \param[in] t Function / member to expose /// \param[in] t Function / member to expose
@ -142,10 +72,6 @@ namespace chaiscript
return Proxy_Function( return Proxy_Function(
chaiscript::make_shared<dispatch::Proxy_Function_Base, dispatch::Proxy_Function_Callable_Impl<Ret (Class &, Param...), decltype(call)>>(call)); chaiscript::make_shared<dispatch::Proxy_Function_Base, dispatch::Proxy_Function_Callable_Impl<Ret (Class &, Param...), decltype(call)>>(call));
/*
return Proxy_Function(
chaiscript::make_shared<dispatch::Proxy_Function_Base, dispatch::Proxy_Function_Impl<typename dispatch::detail::FunctionSignature<decltype(dispatch::detail::to_function(t_func)) >::Signature>>(dispatch::detail::to_function(t_func)));
*/
} }