Re: Wrapping Callback Functions

new topic     » goto parent     » topic index » view thread      » older message » newer message

One neat feature that was implemented in Euphoria 4.0 (or maybe 4.1? can't remember) but not really advertised is the "stacking" of default parameters that are evaluated at runtime at the callee's location.

What I mean by "stacking" is that parameters are evaluated from left to right, so you can use a previous parameter's value later down the line. So you can "clean up" code above by adding a few default parameters:

Edit: since we've determined this uses cdecl on 32-bit Windows, we also need to preface our call backs with '+' to force cdecl inside Euphoria.

public procedure NewtonSetMemorySystem( sequence malloc_name, sequence free_name, integer malloc_id=routine_id(malloc_name), integer free_id=routine_id(free_name) ) 
 
    atom malloc_cb = call_back({ '+', malloc_id }) 
    atom free_cb = call_back({ '+', free_id }) 
 
    c_proc( xNewtonSetMemorySystem, {malloc_cb,free_cb} ) 
end procedure 

And now you can eliminate all the separate calls to routine_id and call_back and just call NewtonSetMemorySystem like this:

function NewtonAllocMemory( atom sizeInBytes ) 
    return allocate( sizeInBytes ) 
end function 
 
function NewtonFreeMemory( atom ptr, atom sizeInBytes ) 
    free( ptr ) 
    return NULL 
end function 
 
NewtonSetMemorySystem( "NewtonAllocMemory", "NewtonFreeMemory" ) 

-Greg

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu