Get class members that are functions working
Automatic conversion of return values into Proxy_Function objects Issue: #155
This commit is contained in:
parent
2f444542ab
commit
d2ed8fdcf1
@ -372,6 +372,7 @@ namespace chaiscript
|
||||
m->add(user_type<Boxed_Value>(), "Object");
|
||||
m->add(user_type<Boxed_Number>(), "Number");
|
||||
m->add(user_type<Proxy_Function>(), "Function");
|
||||
m->add(user_type<dispatch::Assignable_Proxy_Function>(), "Assignable_Function");
|
||||
m->add(user_type<std::exception>(), "exception");
|
||||
|
||||
m->add(fun(&dispatch::Proxy_Function_Base::get_arity), "get_arity");
|
||||
@ -468,6 +469,13 @@ namespace chaiscript
|
||||
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::add_const<dispatch::Proxy_Function_Base>::type>), "=");
|
||||
m->add(chaiscript::base_class<dispatch::Proxy_Function_Base, dispatch::Assignable_Proxy_Function>());
|
||||
m->add(fun<void (dispatch::Assignable_Proxy_Function &, const std::shared_ptr<const dispatch::Proxy_Function_Base> &)>(
|
||||
[](dispatch::Assignable_Proxy_Function &t_lhs, const std::shared_ptr<const dispatch::Proxy_Function_Base> &t_rhs) {
|
||||
t_lhs.assign(t_rhs);
|
||||
}
|
||||
), "="
|
||||
);
|
||||
|
||||
m->add(fun(&Boxed_Value::type_match), "type_match");
|
||||
|
||||
|
@ -57,7 +57,7 @@ namespace chaiscript
|
||||
std::function<FunctionType>
|
||||
functor(Const_Proxy_Function func, const Type_Conversions *t_conversions)
|
||||
{
|
||||
return functor<FunctionType>(std::vector<Const_Proxy_Function>({func}), t_conversions);
|
||||
return functor<FunctionType>(std::vector<Const_Proxy_Function>({std::move(func)}), t_conversions);
|
||||
}
|
||||
|
||||
/// Helper for automatically unboxing a Boxed_Value that contains a function object
|
||||
|
@ -26,6 +26,9 @@ namespace chaiscript
|
||||
{
|
||||
namespace dispatch
|
||||
{
|
||||
template<class T> class Proxy_Function_Impl;
|
||||
template<class T> class Assignable_Proxy_Function_Impl;
|
||||
|
||||
namespace detail
|
||||
{
|
||||
/**
|
||||
@ -49,6 +52,98 @@ namespace chaiscript
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Ret>
|
||||
struct Handle_Return<const std::function<Ret> &>
|
||||
{
|
||||
static Boxed_Value handle(const std::function<Ret> &f) {
|
||||
return Boxed_Value(
|
||||
std::shared_ptr<dispatch::Proxy_Function_Base>(
|
||||
new dispatch::Proxy_Function_Impl<Ret>(f)
|
||||
)
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Ret>
|
||||
struct Handle_Return<std::function<Ret>>
|
||||
{
|
||||
static Boxed_Value handle(const std::function<Ret> &f) {
|
||||
return Boxed_Value(
|
||||
std::shared_ptr<dispatch::Proxy_Function_Base>(
|
||||
new Proxy_Function_Impl<Ret>(f)
|
||||
)
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Ret>
|
||||
struct Handle_Return<const std::shared_ptr<std::function<Ret>>>
|
||||
{
|
||||
static Boxed_Value handle(const std::shared_ptr<std::function<Ret>> &f) {
|
||||
return Boxed_Value(
|
||||
std::shared_ptr<Proxy_Function_Base>(
|
||||
new Assignable_Proxy_Function_Impl<Ret>(
|
||||
std::ref(*f),
|
||||
f
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Ret>
|
||||
struct Handle_Return<const std::shared_ptr<std::function<Ret>> &>
|
||||
{
|
||||
static Boxed_Value handle(const std::shared_ptr<std::function<Ret>> &f) {
|
||||
return Boxed_Value(
|
||||
std::shared_ptr<Proxy_Function_Base>(
|
||||
new Assignable_Proxy_Function_Impl<Ret>(
|
||||
std::ref(*f),
|
||||
f
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Ret>
|
||||
struct Handle_Return<std::shared_ptr<std::function<Ret>>>
|
||||
{
|
||||
static Boxed_Value handle(const std::shared_ptr<std::function<Ret>> &f) {
|
||||
return Boxed_Value(
|
||||
std::shared_ptr<Proxy_Function_Base>(
|
||||
new Assignable_Proxy_Function_Impl<Ret>(
|
||||
std::ref(*f),
|
||||
f
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Ret>
|
||||
struct Handle_Return<std::function<Ret> &>
|
||||
{
|
||||
static Boxed_Value handle(std::function<Ret> &f) {
|
||||
return Boxed_Value(
|
||||
std::shared_ptr<Proxy_Function_Base>(
|
||||
new Assignable_Proxy_Function_Impl<Ret>(
|
||||
std::ref(f),
|
||||
std::shared_ptr<std::function<Ret>>()
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
static Boxed_Value handle(const std::function<Ret> &f) {
|
||||
return Boxed_Value(
|
||||
std::shared_ptr<dispatch::Proxy_Function_Base>(
|
||||
new dispatch::Proxy_Function_Impl<Ret>(f)
|
||||
)
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Ret>
|
||||
struct Handle_Return<Ret *>
|
||||
{
|
||||
|
@ -43,6 +43,9 @@ namespace chaiscript
|
||||
|
||||
namespace dispatch
|
||||
{
|
||||
template<typename FunctionType>
|
||||
std::function<FunctionType> functor(std::shared_ptr<const Proxy_Function_Base> func, const Type_Conversions *t_conversions);
|
||||
|
||||
class Param_Types
|
||||
{
|
||||
public:
|
||||
@ -571,11 +574,73 @@ namespace chaiscript
|
||||
return detail::Do_Call<typename std::function<Func>::result_type>::go(m_f, params, t_conversions);
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
std::function<Func> m_f;
|
||||
Func *m_dummy_func;
|
||||
};
|
||||
|
||||
class Assignable_Proxy_Function : public Proxy_Function_Impl_Base
|
||||
{
|
||||
public:
|
||||
Assignable_Proxy_Function(const std::vector<Type_Info> &t_types)
|
||||
: Proxy_Function_Impl_Base(t_types)
|
||||
{
|
||||
}
|
||||
|
||||
virtual ~Assignable_Proxy_Function() {}
|
||||
|
||||
|
||||
virtual void assign(const std::shared_ptr<const Proxy_Function_Base> &t_rhs) = 0;
|
||||
|
||||
|
||||
};
|
||||
|
||||
template<typename Func>
|
||||
class Assignable_Proxy_Function_Impl : public Assignable_Proxy_Function
|
||||
{
|
||||
public:
|
||||
Assignable_Proxy_Function_Impl(std::reference_wrapper<std::function<Func>> t_f, std::shared_ptr<std::function<Func>> t_ptr)
|
||||
: Assignable_Proxy_Function(detail::build_param_type_list(static_cast<Func *>(nullptr))),
|
||||
m_f(std::move(t_f)), m_shared_ptr_holder(std::move(t_ptr)), m_dummy_func(nullptr)
|
||||
{
|
||||
assert(!m_shared_ptr_holder || m_shared_ptr_holder.get() == &m_f.get());
|
||||
|
||||
}
|
||||
|
||||
virtual ~Assignable_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 Assignable_Proxy_Function_Impl<Func> *>(&t_func) != nullptr;
|
||||
}
|
||||
|
||||
std::function<Func> internal_function() const
|
||||
{
|
||||
return m_f.get();
|
||||
}
|
||||
|
||||
virtual void assign(const std::shared_ptr<const Proxy_Function_Base> &t_rhs) {
|
||||
m_f.get() = dispatch::functor<Func>(t_rhs, nullptr);
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual Boxed_Value do_call(const std::vector<Boxed_Value> ¶ms, const Type_Conversions &t_conversions) const
|
||||
{
|
||||
return detail::Do_Call<typename std::function<Func>::result_type>::go(m_f.get(), params, t_conversions);
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
std::reference_wrapper<std::function<Func>> m_f;
|
||||
std::shared_ptr<std::function<Func>> m_shared_ptr_holder;
|
||||
Func *m_dummy_func;
|
||||
};
|
||||
/// Attribute getter Proxy_Function implementation
|
||||
template<typename T, typename Class>
|
||||
class Attribute_Access : public Proxy_Function_Base
|
||||
@ -625,7 +690,7 @@ namespace chaiscript
|
||||
if (bv.is_const())
|
||||
{
|
||||
const Class *o = boxed_cast<const Class *>(bv, &t_conversions);
|
||||
return detail::Handle_Return<typename std::add_lvalue_reference<T>::type>::handle(o->*m_attr);
|
||||
return detail::Handle_Return<const typename std::add_lvalue_reference<T>::type>::handle(o->*m_attr);
|
||||
} else {
|
||||
Class *o = boxed_cast<Class *>(bv, &t_conversions);
|
||||
return detail::Handle_Return<typename std::add_lvalue_reference<T>::type>::handle(o->*m_attr);
|
||||
|
@ -163,11 +163,6 @@ CHAISCRIPT_MODULE_EXPORT chaiscript::ModulePtr create_chaiscript_module_test_mo
|
||||
|
||||
// member that is a function
|
||||
m->add(chaiscript::fun(&TestBaseType::func_member), "func_member");
|
||||
m->add(chaiscript::fun<std::function<int(int)> &(std::function<int(int)> &, const std::function<int(int)> &)>(
|
||||
[](std::function<int (int)> &t_lhs, const std::function<int (int)> &t_rhs)-> std::function<int (int)> &{
|
||||
return t_lhs = t_rhs;
|
||||
}), "=");
|
||||
|
||||
m->add(chaiscript::fun(&get_new_int), "get_new_int");
|
||||
|
||||
|
||||
|
@ -16,6 +16,5 @@ t0.func_member = fun(int i){ i * 3; };
|
||||
|
||||
assert_true(func_member(t0)(2) == 6)
|
||||
assert_true((func_member(t0))(2) == 6)
|
||||
assert_true((t0.func_member)(2) == 6)
|
||||
assert_true(t0.func_member(2) == 6)
|
||||
|
||||
|
@ -8,6 +8,5 @@ t0.func_member = fun(int i){ i * 3; };
|
||||
|
||||
assert_true(func_member(t0)(2) == 6)
|
||||
assert_true((func_member(t0))(2) == 6)
|
||||
assert_true((t0.func_member)(2) == 6)
|
||||
assert_true(t0.func_member(2) == 6)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user