Re: wrapping C functions

new topic     » goto parent     » topic index » view thread      » older message » newer message
SDPringle said...

That's a little risky, you don't know if the integer might actually need to be a double. You should go through the format string instead and determine the types that way.

Agreed. I guess I should have pointed that out. My method is really more applicable to functions that take a variable number of the same type, typically pointers to objects. In any case, I would rely on the programmer to make the correct usage and adapt my method as necessary.

Parsing the format string would certainly work, but would also require much more code for a very small gain (one often knows what one is feeding a printf function). Perhaps we could pass the sequence of types directly...

procedure euSerialPrintf(integer fd, sequence fmt, object args = {}, object types = {}) 
 
  if atom( args ) then args = {args} end if 
  if atom( types ) then types = {types} end if 
 
  if length( args ) != length( types ) then 
    crash( "euSerialPrintf: args and types must be the same length! %d != %d\n", {length(args),length(types)} ) 
  end if 
 
  integer serialPrintf = link_c_proc(dll_ptr, "serialPrintf", {C_INT, C_POINTER} & types) 
 
  -- ...perform caching here... 
 
  c_proc(serialPrintf, {fd, allocate_string(fmt,1)} & args) 
 
end procedure 
 
petelomax said...

I was thinking that too. Then it dawned on me, there is a far simpler way:

serialPrintf = link_c_proc(dll_ptr, "serialPrintf", {C_INT, C_POINTER}) 
procedure euSerialPrintf(integer fd, sequence fmt, object args) 
 
  c_proc(serialPrintf, {fd, allocate_string(sprintf(fmt,args),1)}) 
 
end procedure 

While this method certainly works, it is only applicable in this situations. Other variadic functions may not be so convenient. getlost

-Greg

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

Search



Quick Links

User menu

Not signed in.

Misc Menu