Fix problem with functor<>() not casting to the proper type and add unit test for this case
This commit is contained in:
parent
e1fbf54e40
commit
a122403c20
@ -108,6 +108,10 @@ IF(BUILD_TESTING)
|
|||||||
target_link_libraries(dynamic_object_test ${DYNAMIC_LOADER} ${Boost_LIBRARIES} ${READLINE_LIB})
|
target_link_libraries(dynamic_object_test ${DYNAMIC_LOADER} ${Boost_LIBRARIES} ${READLINE_LIB})
|
||||||
add_test(NAME Dynamic_Object_Test COMMAND dynamic_object_test)
|
add_test(NAME Dynamic_Object_Test COMMAND dynamic_object_test)
|
||||||
|
|
||||||
|
add_executable(functor_creation_test unittests/functor_creation_test.cpp)
|
||||||
|
target_link_libraries(functor_creation_test ${DYNAMIC_LOADER} ${Boost_LIBRARIES} ${READLINE_LIB})
|
||||||
|
add_test(NAME Functor_Creation_Test COMMAND functor_creation_test)
|
||||||
|
|
||||||
add_library(test_module MODULE src/test_module.cpp)
|
add_library(test_module MODULE src/test_module.cpp)
|
||||||
target_link_libraries(test_module ${Boost_LIBRARIES})
|
target_link_libraries(test_module ${Boost_LIBRARIES})
|
||||||
|
|
||||||
|
@ -27,7 +27,7 @@ namespace chaiscript
|
|||||||
*/
|
*/
|
||||||
template<typename FunctionType>
|
template<typename FunctionType>
|
||||||
boost::function<FunctionType>
|
boost::function<FunctionType>
|
||||||
functor(const std::vector<std::pair<std::string, Proxy_Function > > &funcs)
|
functor(const std::vector<std::pair<std::string, Const_Proxy_Function > > &funcs)
|
||||||
{
|
{
|
||||||
FunctionType *p=0;
|
FunctionType *p=0;
|
||||||
return detail::build_function_caller_helper(p, funcs);
|
return detail::build_function_caller_helper(p, funcs);
|
||||||
@ -48,9 +48,9 @@ namespace chaiscript
|
|||||||
*/
|
*/
|
||||||
template<typename FunctionType>
|
template<typename FunctionType>
|
||||||
boost::function<FunctionType>
|
boost::function<FunctionType>
|
||||||
functor(Proxy_Function func)
|
functor(Const_Proxy_Function func)
|
||||||
{
|
{
|
||||||
std::vector<std::pair<std::string, Proxy_Function > > funcs;
|
std::vector<std::pair<std::string, Const_Proxy_Function > > funcs;
|
||||||
funcs.push_back(std::make_pair(std::string(), func));
|
funcs.push_back(std::make_pair(std::string(), func));
|
||||||
return functor<FunctionType>(funcs);
|
return functor<FunctionType>(funcs);
|
||||||
}
|
}
|
||||||
@ -63,7 +63,7 @@ namespace chaiscript
|
|||||||
boost::function<FunctionType>
|
boost::function<FunctionType>
|
||||||
functor(const Boxed_Value &bv)
|
functor(const Boxed_Value &bv)
|
||||||
{
|
{
|
||||||
return functor<FunctionType>(boxed_cast<Proxy_Function >(bv));
|
return functor<FunctionType>(boxed_cast<Const_Proxy_Function >(bv));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -32,7 +32,7 @@ namespace chaiscript
|
|||||||
template<typename Ret>
|
template<typename Ret>
|
||||||
struct Function_Caller_Ret
|
struct Function_Caller_Ret
|
||||||
{
|
{
|
||||||
static Ret call(const std::vector<std::pair<std::string, Proxy_Function > > &t_funcs,
|
static Ret call(const std::vector<std::pair<std::string, Const_Proxy_Function > > &t_funcs,
|
||||||
const std::vector<Boxed_Value> ¶ms)
|
const std::vector<Boxed_Value> ¶ms)
|
||||||
{
|
{
|
||||||
return boxed_cast<Ret>(dispatch(t_funcs, params));
|
return boxed_cast<Ret>(dispatch(t_funcs, params));
|
||||||
@ -45,7 +45,7 @@ namespace chaiscript
|
|||||||
template<>
|
template<>
|
||||||
struct Function_Caller_Ret<void>
|
struct Function_Caller_Ret<void>
|
||||||
{
|
{
|
||||||
static void call(const std::vector<std::pair<std::string, Proxy_Function > > &t_funcs,
|
static void call(const std::vector<std::pair<std::string, Const_Proxy_Function > > &t_funcs,
|
||||||
const std::vector<Boxed_Value> ¶ms)
|
const std::vector<Boxed_Value> ¶ms)
|
||||||
{
|
{
|
||||||
dispatch(t_funcs, params);
|
dispatch(t_funcs, params);
|
||||||
@ -70,7 +70,7 @@ namespace chaiscript
|
|||||||
* used internally for unwrapping a function call's types
|
* used internally for unwrapping a function call's types
|
||||||
*/
|
*/
|
||||||
template<typename Ret BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Param) >
|
template<typename Ret BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Param) >
|
||||||
Ret function_caller(const std::vector<std::pair<std::string, Proxy_Function > > &funcs
|
Ret function_caller(const std::vector<std::pair<std::string, Const_Proxy_Function > > &funcs
|
||||||
BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_BINARY_PARAMS(n, Param, p) )
|
BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_BINARY_PARAMS(n, Param, p) )
|
||||||
{
|
{
|
||||||
std::vector<Boxed_Value> params;
|
std::vector<Boxed_Value> params;
|
||||||
@ -85,7 +85,7 @@ namespace chaiscript
|
|||||||
*/
|
*/
|
||||||
template<typename Ret BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Param) >
|
template<typename Ret BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Param) >
|
||||||
boost::function<Ret (BOOST_PP_ENUM_PARAMS(n, Param)) >
|
boost::function<Ret (BOOST_PP_ENUM_PARAMS(n, Param)) >
|
||||||
build_function_caller_helper(Ret (BOOST_PP_ENUM_PARAMS(n, Param)), const std::vector<std::pair<std::string, Proxy_Function> > &funcs)
|
build_function_caller_helper(Ret (BOOST_PP_ENUM_PARAMS(n, Param)), const std::vector<std::pair<std::string, Const_Proxy_Function> > &funcs)
|
||||||
{
|
{
|
||||||
return boost::bind(&function_caller<Ret BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, Param)>, funcs
|
return boost::bind(&function_caller<Ret BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, Param)>, funcs
|
||||||
BOOST_PP_ENUM_TRAILING(n, curry, ~));
|
BOOST_PP_ENUM_TRAILING(n, curry, ~));
|
||||||
|
16
unittests/functor_creation_test.cpp
Normal file
16
unittests/functor_creation_test.cpp
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
#include <chaiscript/utility/utility.hpp>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
|
||||||
|
chaiscript::ChaiScript chai;
|
||||||
|
|
||||||
|
chai.eval("def func() { print(\"Hello World\"); } ");
|
||||||
|
|
||||||
|
boost::function<void ()> f = chai.functor<void ()>("func");
|
||||||
|
|
||||||
|
f();
|
||||||
|
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user