Replacing C functions with boost::function for callback functions in OpenGL

I have a main file from my OpenGL program which contains global variables and the functions that are used by OpenGL as callback functions. For example: int a; float b; void display () { .... display code calls ... } int main (int argc, char **argv) { glutDisplayFunc( display ); } Now I thought I would put all the variables and functions into a class and use that in the main. Here is where I am getting stuck on the use of boost::function. I changed the code to look something like the following: class MyMain { int a; float b; void display() { ... display code calls ... } } int main (int argc, char **argv) { MyMain m_ref; boost::function<void()> disp_func = boost::bind ( &MyMain::display, &m_ref ); glutDisplayFunc ( disp_func ); } What I am getting back from the g++ compiler is the following error message: error: cannot convert `boost::function<void ()(), std::allocator<void> >' to `void (*)()' for argument `1' to `void glutDisplayFunc(void (*)())' Any ideas where I am going wrong? Stephen

On 6/30/06, Stephen Torri <storri@torri.org> wrote:
I have a main file from my OpenGL program which contains global variables and the functions that are used by OpenGL as callback functions. For example:
int a; float b;
void display () { .... display code calls ... }
int main (int argc, char **argv) { glutDisplayFunc( display ); }
Now I thought I would put all the variables and functions into a class and use that in the main. Here is where I am getting stuck on the use of boost::function. I changed the code to look something like the following:
class MyMain { int a; float b;
void display() { ... display code calls ... } }
int main (int argc, char **argv) { MyMain m_ref; boost::function<void()> disp_func = boost::bind ( &MyMain::display, &m_ref ); glutDisplayFunc ( disp_func ); }
What I am getting back from the g++ compiler is the following error message:
error: cannot convert `boost::function<void ()(), std::allocator<void> >' to `void (*)()' for argument `1' to `void glutDisplayFunc(void (*)())'
Any ideas where I am going wrong?
You are passing a function object to a C function which expects a function pointer. A boost::function can not be converted into a function pointer.
Stephen
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Cory Nelson http://www.int64.org

On Fri, 2006-06-30 at 11:04 -0700, Cory Nelson wrote:
What I am getting back from the g++ compiler is the following error message:
error: cannot convert `boost::function<void ()(), std::allocator<void> >' to `void (*)()' for argument `1' to `void glutDisplayFunc(void (*)())'
Any ideas where I am going wrong?
You are passing a function object to a C function which expects a function pointer. A boost::function can not be converted into a function pointer.
I appreciate the response pointing out the error. It would be nice to have a solution and not just a critique. Stephen

On 6/30/06, Stephen Torri <storri@torri.org> wrote:
On Fri, 2006-06-30 at 11:04 -0700, Cory Nelson wrote:
What I am getting back from the g++ compiler is the following error message:
error: cannot convert `boost::function<void ()(), std::allocator<void> >' to `void (*)()' for argument `1' to `void glutDisplayFunc(void (*)())'
Any ideas where I am going wrong?
You are passing a function object to a C function which expects a function pointer. A boost::function can not be converted into a function pointer.
I appreciate the response pointing out the error. It would be nice to have a solution and not just a critique.
Glut was poorly designed in that respect (not giving an option to pass a user data pointer) - in this case there is no solution other than making your function object global and have a C function which calls it. You could always modify your glut source, it sounds like an easy enough patch.
Stephen
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Cory Nelson http://www.int64.org

On Jun 30, 2006, at 12:28 PM, Stephen Torri wrote:
boost::function<void()> disp_func = boost::bind ( &MyMain::display, &m_ref ); glutDisplayFunc ( disp_func );
Unfortunately, disp_func needs to be a function pointer. I wrote up a short article on the Wiki about using boost::function with C libraries. It may help you solve this problem: http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl? Generalizing_C-Style_Callbacks Doug

Douglas Gregor wrote:
On Jun 30, 2006, at 12:28 PM, Stephen Torri wrote:
boost::function<void()> disp_func = boost::bind ( &MyMain::display, &m_ref ); glutDisplayFunc ( disp_func );
Unfortunately, disp_func needs to be a function pointer. I wrote up a short article on the Wiki about using boost::function with C libraries. It may help you solve this problem:
http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl? Generalizing_C-Style_Callbacks
Once upon a time I mentioned making a solution... http://thread.gmane.org/gmane.comp.lib.boost.devel/124435/focus=124449 -- -- Grafik - Don't Assume Anything -- Redshift Software, Inc. - http://redshift-software.com -- rrivera/acm.org - grafik/redshift-software.com -- 102708583/icq - grafikrobot/aim - grafikrobot/yahoo

On Fri, 2006-06-30 at 14:40 -0400, Douglas Gregor wrote:
On Jun 30, 2006, at 12:28 PM, Stephen Torri wrote:
boost::function<void()> disp_func = boost::bind ( &MyMain::display, &m_ref ); glutDisplayFunc ( disp_func );
Unfortunately, disp_func needs to be a function pointer. I wrote up a short article on the Wiki about using boost::function with C libraries. It may help you solve this problem:
http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl? Generalizing_C-Style_Callbacks
Doug
Given your example I would need to create a class something like the following: class A { private: typedef boost::function<void()> generic_opegnl_callback; public: void init() { m_display_callback = boost::bind ( &display_impl, this ); } void display_impl (void) { ... display code ... } void display (void) { m_display_callback(); } private: generic_opengl_callback m_display_callback; }; Is this right? Stephen
participants (4)
-
Cory Nelson
-
Douglas Gregor
-
Rene Rivera
-
Stephen Torri