I have about 100 trampoline functions. I would like to know if it is possible to automate the wrapping of each of them inside a try / catch block.
Please be warned in advance, this is not an easy question. I will start by describing the problem with the (simplified) code and try to answer it as best as possible so that the reader can see where I am.
Foo has a function pointer table:
EDIT . This is table C <pointer> . Therefore, it can take static W::w .
Signatures here: http://svn.python.org/projects/python/trunk/Include/object.h
EDIT: I tried to run a test case here :
class Foo { Table table; Foo() {
If a specific slot is βloadedβ, this is what loads into it:
int func53(S s, A a, B b) { try{ return get_base(s)->f53(a,b); } catch(...) { return 42;} } float func54(S s, C c, D d, E e) { try{ return get_base(s)->f54(c,d,e); } catch(...) { return 3.14;} }
I am trying to accomplish this using lambdas to get around the need to define all of these func53 separately. Something like that:
class Foo { : void load53() { table->fp_53 = [](S s, A a, B b)->int { return get_base(s)->f53(a,b); } } void load54() { table->fp_54 = [](S s, C c, D d, E e)->float { return get_base(s)->f54(c,d,e); } }
However, this does not lead to errors. I need to put try / catch on the return statement:
try{ return get_base(s)->f53(a,b); } catch{ return 42; }
However, this creates a lot of confusion. It would be nice if I could:
return trap( get_base(s)->f53(a,b); )
My question is: is there a way to write this trap function (without using #define)?
This is what I came up with so far:
I think this will convey all the necessary information:
trap<int, &Base::f53>(s,a,b)
a trap definition might look like this:
template<typename RET, Base::Func> static RET trap(S s, ...) { try { return get_base(s)->Func(...); } catch { return std::is_integral<RET>::value ? (RET)(42) : (RET)(3.14); } }
This can provide very clean syntax:
class Foo { : void load53() { table->fp_53 = &trap<int, &Base::f53>; } void load54() { table->fp_54 = &trap<float, &Base::f54>; } }
At this moment, Iβm not even sure whether some laws have been violated. table->fp_53 must be a valid C function pointer.
Passing a non-static member function ( &Base::f53> ) to the address will not violate this, since it is a template parameter and does not affect the signature for trap
Likewise, ... should be fine, since C allows varargs.
So, if this is true, can it be cleaned?
My thoughts:
1), perhaps ... should be returned to the template parameter as a package.
2) it is possible to deduce the return type for the trap and save one template parameter
3) that the Base::Func template parameter is illegal syntax. And I suspect that he is not even close to something legal. Which can disrupt the whole approach.