Function pointer as parameter of a function to export in LUA ?

View: New views
3 Messages — Rating Filter:   Alert me  

Function pointer as parameter of a function to export in LUA ?

by Aurélien MAIGNAN :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

In my C++ code I have this fonction to export in lua using luabind :

inline void ScriptingApp::setCallBack(functionType func){_callbackFunc = func;}

doing this declaration :

luabind::class_<ScriptingApp>("ScriptingApp")
.def("setCallBack", &ScriptingApp::setCallBack)

The problem is that functionType is defined as a function pointer :

typedef const char * mstring;
typedef void (* functionType) (void, const int&, mstring);

And that doesn't compile.
I get this error :

Build Log
  

Rebuild started: Project: ScriptingApp, Configuration: Debug|Win32


Command Lines
  
Creating temporary file "f:\Silicon Worlds\TestPythonCpp\ScriptingApp\Debug\RSP00000626483936.rsp" with contents
[
/O2 /I "F:\Silicon Worlds\TestPythonCpp\PyOgre" /I "F:\Silicon Worlds\LuaBind\References\lua\include" /I "C:\Program Files\boost\boost_1_35_0" /I "F:\Silicon Worlds\LuaBind\References\luabind\include" /FD /EHsc /MDd /Fo"Debug\\" /Fd"Debug\vc80.pdb" /c /Z7 /TP ".\ScriptingApp.cpp"

".\Rocket.cpp"
]
Creating command line "cl.exe @"f:\Silicon Worlds\TestPythonCpp\ScriptingApp\Debug\RSP00000626483936.rsp" /nologo /errorReport:prompt"
Output Window
   
Compiling...
ScriptingApp.cpp
C:\Program Files\Microsoft Visual Studio 8\VC\include\xutility(2282) : warning C4996: 'std::_Copy_opt': You have used a std:: construct that is not safe. See documentation on how to use the Safe Standard C++ Library
C:\Program Files\Microsoft Visual Studio 8\VC\include\xutility(2270) : see declaration of 'std::_Copy_opt'
F:\Silicon Worlds\LuaBind\References\luabind\include\luabind/detail/primitives.hpp(68) : see reference to function template instantiation '_OutIt std::copy(_InIt,_InIt,_OutIt)' being compiled
with
[
_OutIt=char *,
_InIt=const char *
]
F:\Silicon Worlds\LuaBind\References\luabind\include\luabind/detail/call.hpp(280) : error C2784: 'make_const_pointer::type luabind::detail::const_pointer_converter::apply(lua_State *,luabind::detail::by_const_pointer,int)' : could not deduce template argument for 'luabind::detail::by_const_pointer' from 'luabind::detail::by_pointer'
with
[
T=void (int,const int &,mstring)
]
F:\Silicon Worlds\LuaBind\References\luabind\include\luabind/detail/policy.hpp(755) : see declaration of 'luabind::detail::const_pointer_converter::apply'
F:\Silicon Worlds\LuaBind\References\luabind\include\luabind/detail/call.hpp(403) : see reference to function template instantiation 'int luabind::detail::returns::call(void (__thiscall ScriptingApp::* )(A0),WrappedClass *,lua_State *,Policies *const )' being compiled
with
[
WrappedClass=ScriptingApp,
Policies=luabind::detail::null_type,
A0=functionType
]
F:\Silicon Worlds\LuaBind\References\luabind\include\luabind/class.hpp(210) : see reference to function template instantiation 'int luabind::detail::call(R (__thiscall ScriptingApp::* )(A0),WrappedClass *,lua_State *,const Policies *)' being compiled
with
[
Class=ScriptingApp,
Policies=luabind::detail::null_type,
R=void,
A0=functionType,
WrappedClass=ScriptingApp
]
F:\Silicon Worlds\LuaBind\References\luabind\include\luabind/class.hpp(209) : while compiling class template member function 'int luabind::detail::mem_fn_callback::operator ()(lua_State *) const'
with
[
Fn=void (__thiscall ScriptingApp::* )(functionType),
Class=ScriptingApp,
Policies=luabind::detail::null_type
]
F:\Silicon Worlds\LuaBind\References\luabind\include\luabind/class.hpp(1218) : see reference to class template instantiation 'luabind::detail::mem_fn_callback' being compiled
with
[
Fn=void (__thiscall ScriptingApp::* )(functionType),
Class=ScriptingApp,
Policies=luabind::detail::null_type
]
F:\Silicon Worlds\LuaBind\References\luabind\include\luabind/class.hpp(868) : see reference to function template instantiation 'luabind::class_ &luabind::class_::virtual_def(const char *,const F &,const Policies &,luabind::detail::null_type,boost::mpl::true_)' being compiled
with
[
T=ScriptingApp,
F=void (__thiscall ScriptingApp::* )(functionType),
Policies=luabind::detail::null_type
]
.\ScriptingApp.cpp(341) : see reference to function template instantiation 'luabind::class_ &luabind::class_::def(const char *,F)' being compiled
with
[
T=ScriptingApp,
F=void (__thiscall ScriptingApp::* )(functionType)
]
F:\Silicon Worlds\LuaBind\References\luabind\include\luabind/detail/call.hpp(282) : error C2784: 'void luabind::detail::const_pointer_converter::converter_postcall(lua_State *,luabind::detail::by_const_pointer,int)' : could not deduce template argument for 'luabind::detail::by_const_pointer' from 'luabind::detail::by_pointer'
with
[
T=void (int,const int &,mstring)
]
F:\Silicon Worlds\LuaBind\References\luabind\include\luabind/detail/policy.hpp(779) : see declaration of 'luabind::detail::const_pointer_converter::converter_postcall'
Rocket.cpp
Generating Code...
Results
  
Build log was saved at "file://f:\Silicon Worlds\TestPythonCpp\ScriptingApp\Debug\BuildLog.htm"
ScriptingApp - 2 error(s), 1 warning(s)


I don't know if it is possible to use such function pointer as argument of a function to export in LUA ?
If not maybe trying to see what is possible using functor instead of function pointer might work ?

Thanks for any help,

Aurélien MAIGNAN

-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
_______________________________________________
luabind-user mailing list
luabind-user@...
https://lists.sourceforge.net/lists/listinfo/luabind-user

Re: Function pointer as parameter of a function to export in LUA ?

by Enno Rehling :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Aurélien MAIGNAN wrote:

> Hi,
>
> In my C++ code I have this fonction to export in lua using luabind :
>
> inline void ScriptingApp::setCallBack(functionType func){_callbackFunc =
> func;}
>
> doing this declaration :
>
> luabind::class_<ScriptingApp>("ScriptingApp")
> .def("setCallBack", &ScriptingApp::setCallBack)
>
> The problem is that functionType is defined as a function pointer :

And the problem with that is that luabind cannot map this to one of the
integral types in Lua, and neither can it map it to one of your defined
classes.

Out of curiosity, and because it might help in understanding why you
want to this: How do you intend to call this function from your Lua
script? Where is the parameter coming from that you intend to pass?

If you want to pass a Lua function, that can be done by making the
parameter a luabind::object&, something like this:

static void
foo(const luabind::object& bar, int i, const char * str)
{
   try {
     bar(i, str);
   } catch (error& e) {
     // do your error handling here
   }
}

(this is untested; I just mixed together some bits from my own code)

Enno.
--
"Multiple exclamation marks [...] are a sure sign of a diseased mind."
                    - Terry Pratchett, "Eric" (http://tinyurl.com/evan)


-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
_______________________________________________
luabind-user mailing list
luabind-user@...
https://lists.sourceforge.net/lists/listinfo/luabind-user

Re: Function pointer as parameter of a function to export in LUA ?

by Aurélien MAIGNAN :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,
thanks for your reply.
My intend was to do something like that :
in C++ :
typedef void (*functionType) (void*, int) [sorry I wrote void instead of void* in my first mail]
class AClass {
  functionType _callbackFunc;
  void memberMethod(int);
  void memberMethodThatActLikeAListener(...);
  static void staticFunction(void* MyThis, int i)
  {
    AClass * Obj = reinterpret_cast<AClass*>(myThis);
    return Obj->memberMethod(i);
  };
  void setCallBack(functionType func);
  functionType getCallBack();
};
inline void AClass ::setCallBack(functionType func)
{_callbackFunc = func;}
inline functionType AClass ::getCallBack()
{return _callbackFunc;}
AClass ::memberMethodThatActLikeAListener(...)
{
  //do some stuff, and particularly :
  getCallBack()(this,id);
}

registering :

luabind::class_<AClass >("AClass ")
.def("setCallBack", &AClass ::setCallBack)
.def("staticFunction", &AClass ::staticFunction)

And in lua :

AClass a;
a:setCallBack(a:staticFunction);

This is what I wanted to do.
Up to now I manage to have another solution that act someway like this using what you suggested :
in C++ :
// the lua function to callback
luabind::object _callbackFunc;
void call_CallBack_Function(const int& id, mstring name);
void setCallBack(luabind::object func);
luabind::object getCallBack();

void AClass::call_CallBack_Function(const int& id, mstring name)
{
    if(_callbackFunc && luabind::type(_callbackFunc) == LUA_TFUNCTION)
    {
        luabind::call_function<void>(_callbackFunc, id, name, this);
    }
}


and in LUA :

--define the callback function
function doUpdatePos(id,name,self)
    print "\nI update the position NOW!\n"
    self:updatePos(id,name)
end

function initCallback()
    sas:setCallBack(doUpdatePos)
end

so instead of doing all the job in C++ in manage the callback process going from C+++ to lua and then going back from lua to C++.

Thanks,
Aurélien MAIGNAN



2008/6/23 Enno Rehling <enno.rehling@...>:
Aurélien MAIGNAN wrote:
> Hi,
>
> In my C++ code I have this fonction to export in lua using luabind :
>
> inline void ScriptingApp::setCallBack(functionType func){_callbackFunc =
> func;}
>
> doing this declaration :
>
> luabind::class_<ScriptingApp>("ScriptingApp")
> .def("setCallBack", &ScriptingApp::setCallBack)
>
> The problem is that functionType is defined as a function pointer :

And the problem with that is that luabind cannot map this to one of the
integral types in Lua, and neither can it map it to one of your defined
classes.

Out of curiosity, and because it might help in understanding why you
want to this: How do you intend to call this function from your Lua
script? Where is the parameter coming from that you intend to pass?

If you want to pass a Lua function, that can be done by making the
parameter a luabind::object&, something like this:

static void
foo(const luabind::object& bar, int i, const char * str)
{
  try {
    bar(i, str);
  } catch (error& e) {
    // do your error handling here
  }
}

(this is untested; I just mixed together some bits from my own code)

Enno.
--
"Multiple exclamation marks [...] are a sure sign of a diseased mind."
                   - Terry Pratchett, "Eric" (http://tinyurl.com/evan)


-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
_______________________________________________
luabind-user mailing list
luabind-user@...
https://lists.sourceforge.net/lists/listinfo/luabind-user


-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
_______________________________________________
luabind-user mailing list
luabind-user@...
https://lists.sourceforge.net/lists/listinfo/luabind-user
LightInTheBox - Buy quality products at wholesale price