Interest in C function pointer utility?

In rewriting a plugin interface I use, which for language portability uses C function pointers as the entry points, I came up with a "make_c_function" utility. I use it to translate C++ functions, any of objects, binds, function, members, et. to _real_ C function pointers. For example: class P { public: int foo(int); }; P p; extern "C" { void * get_plugin_call(int n) { if (1 == n) { return make_c_function<P,int(int)>( boost::bind(&p,p::foo,_1)); } else { return 0; } } } [example much abbreviated from real life use] -- -- Grafik - Don't Assume Anything -- Redshift Software, Inc. - http://redshift-software.com -- rrivera/acm.org - grafik/redshift-software.com -- 102708583/icq - grafikrobot/aim - Grafik/jabber.org

Rene Rivera <grafik.list@redshift-software.com> writes:
In rewriting a plugin interface I use, which for language portability uses C function pointers as the entry points, I came up with a "make_c_function" utility. I use it to translate C++ functions, any of objects, binds, function, members, et. to _real_ C function pointers. For example:
class P { public: int foo(int); };
P p;
extern "C" { void * get_plugin_call(int n) { if (1 == n) { return make_c_function<P,int(int)>( boost::bind(&p,p::foo,_1)); } else { return 0; } } }
[example much abbreviated from real life use]
I don't see how this can work. P p; Q q; extern "C" int (*f1)(int) = make_c_function<P,int(int)>(boost::bind(&p,P::foo,_1)); extern "C" int (*f2)(int) = make_c_function<P,int(int)>(boost::bind(&q,P::foo,_1)); Don't these call the same function with the same stored data? ooh, sneaky idea... do you specailize a class template on the address of the argument to make_c_function, thus generating unique functions? I would've used that idea in Boost.Python had I thought of it. -- Dave Abrahams Boost Consulting www.boost-consulting.com

David Abrahams <dave@boost-consulting.com> writes:
I don't see how this can work.
P p; Q q;
extern "C" int (*f1)(int) = make_c_function<P,int(int)>(boost::bind(&p,P::foo,_1));
extern "C" int (*f2)(int) = make_c_function<P,int(int)>(boost::bind(&q,P::foo,_1));
Don't these call the same function with the same stored data?
ooh, sneaky idea... do you specailize a class template on the address of the argument to make_c_function, thus generating unique functions?
Oh, of course you can't do that; the argument is a runtime argument. How can it possibly work? -- Dave Abrahams Boost Consulting www.boost-consulting.com

David Abrahams wrote:
David Abrahams <dave@boost-consulting.com> writes:
I don't see how this can work.
P p; Q q;
extern "C" int (*f1)(int) = make_c_function<P,int(int)>(boost::bind(&p,P::foo,_1));
extern "C" int (*f2)(int) = make_c_function<P,int(int)>(boost::bind(&q,P::foo,_1));
Don't these call the same function with the same stored data?
ooh, sneaky idea... do you specailize a class template on the address of the argument to make_c_function, thus generating unique functions?
Oh, of course you can't do that; the argument is a runtime argument. How can it possibly work?
:-) You are correct it doesn't.. But I don't need it to. The key is that the first template arg is really a GUID for the function. For me I specify the GUID when I bind the various functions for the plugin interface. The signature for the template function is: template < typename InterfaceGUID, typename FunctionType, typename CallType> typename detail::c_function_traits<InterfaceGUID,FunctionType>::function_type * make_c_function(CallType call); For your example you could do: P p; Q q; extern "C" int (*f1)(int) = make_c_function<P,int(int)>(boost::bind(&p,P::foo,_1)); extern "C" int (*f2)(int) = make_c_function<Q,int(int)>(boost::bind(&q,Q::foo,_1)); Where P and Q will work as GUIDs if you have that single function in Q and F. But more generally, one can use anonymous structs... P p; P q; extern "C" int (*f1)(int) = make_c_function< struct call_0001,int(int)>(boost::bind(&p,P::foo,_1)); extern "C" int (*f1)(int) = make_c_function< struct call_0002,int(int)>(boost::bind(&q,P::foo,_1)); -- -- Grafik - Don't Assume Anything -- Redshift Software, Inc. - http://redshift-software.com -- rrivera/acm.org - grafik/redshift-software.com -- 102708583/icq - grafikrobot/aim - Grafik/jabber.org
participants (3)
-
David Abrahams
-
Peter Simons
-
Rene Rivera