Original date:2020-11-02 08:59:33 Edited by: ghaberek Subject: Re: Wrapping Callback Functions

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:

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" ) 


Not Categorized, Please Help


Quick Links

User menu

Not signed in.

Misc Menu