Dear all,
this topic has been discussed quite often here. The common agreed solution for implementing function pointers is as follows (taken from an old topic):
int test(int a) {
print "Hello " a "\n"
return a + 1
}
int dereference( int func(int), int par ) {
return func(par)
}
print test 4
int x = (addr_ test)
print dereference (addr_ x, 4)
This approach works well and I have been using it quite often to store callback functions in structs. But try this basic DXL code for a void function with no parameters:
void myCB() {print "CB calling ...\n"}
int x = addr_ myCB
DOORS does crash with following error box: "Internal error, please submit a problem report." I have tried several modifications of the code, e. g. (void) instead of (), but nothing helped. I use DOORS Client Version 9.5.1.1.
Any ideas?
rdratlos - Wed Oct 26 10:17:29 EDT 2016 |
Re: Function Pointers The problem is that in "addr myCB" you are actually calling the void function instead of getting its address ... There is a way though to get also the address of void functions. Its somewhere on the forum, but I did not find the post very quickly. You can search for it or if you do not care, just add a dummy integer parameter to your functions. Maybe this helps, regards, Mathias |
Re: Function Pointers Thank you Mathias for your hint. I have searched the forum but could not find a related post, either. But based on your hint there seems to be indeed a solution to the pointer for void functions issue. Take this example:
void fn(_x val)
{
print "callback fn() fires\n"
}
void Func_Proxy_( void cb() ) { cb() }
DB db = create("testDialog", styleStandard)
realize db
addAcceleratorKey(db, fn, keyF7, modKeyShift)
int iAddr = addr_ fn
print "Áddress fn(): " iAddr "" "\n"
Func_Proxy_(addr_ iAddr)
The DXL example correctly prints the address of callback fn() and the subsequent call of the function outputs "callback fn() fires". Also the Keyboard shortcut for the window works. It seems, that function addAcceleratorKey() accepts callbacks with an underscore type as parameter instead of void.
But there is a strange behaviour of the DXL Interpreter:
void myCB(_x) {print "CB calling ...\n"}
int x = addr_ myCB
does not lead to a DXL error. It works as fine as (_x val).
Can somebody confirm, that use of the underscore type can be a solution to implement function pointers for functions without input parameters?
|
Re: Function Pointers Hi Actually I do not have any access to a doors. So some code from memory:
// First two example routines
void routine1(int parameter) {
print "Routine 1: " parameter "\n";
}
void routine2(int parameter) {
print "Routine 2: " parameter "\n";
}
// Define a struct which handles the function pointer
struct delegate_v_i{};
delegate_v_i init(void routine(int)) {
int adress = addr_(routine);
return(adress);
}
void call(delegate_v_i intRtnPtr) {
void internalStarter(void routine(int), int parameter) {
routine(parameter);
}
internalStarter(addr_(intRtnPtr), parameter);
}
// Example use
delegate_v_i delegate_1 = init(routine1);
delegate_v_i delegate_2 = init(routine2);
call(delegate_2, 22);
call(delegate_1, 11);
Best regards Wolfgang |