From: Oliver Date: 2007-07-17T08:50:32+09:00 Subject: Re: C library extension question Thanks for the suggestion, I will take a look at the sample lib you suggested. However, I don't fully understand the reasoning of "not wrapping it". Here is my line of thinking: - the original C function register() expects an array of function pointers, and those functions are suppose to be user-supplied and implemented by C. - Ideally, we should now allow those functions to be implemented by ruby ... is this possible at all? thanks Oliver On Jul 16, 6:53 pm, Ryan Davis wrote: > On Jul 16, 2007, at 12:20 , Oliver wrote: > > > /* command dispatch structure */ > > typedef int (*proxy_cmd)(int trans_id, int nargs, char **args); > > > struct proxy_commands { > > int cmd_base; > > int cmd_size; > > proxy_cmd * cmd_funcs; > > }; > > typedef struct proxy_commands proxy_commands; > > What you're looking for is probably Data_Wrap_Struct and friends. See > ruby.h and intern.h. For a simple example of this in action, look at > the image_science.rb code (available via gems or on the seattlerb > project on rubyforge). It is only 228 lines long currently so it is a > pretty easy read. > > > > > Then, as a user you can define an array of function pointers like > > this: > > > proxy_cmd cmds[] = { > > func_a, > > func_b, > > func_c, > > ... > > }; > > > and fill in the structure: > > > proxy_commands mytabs = { > > CMD_BASE; > > sizeof(cmds)/sizeof(proxy_cmd), > > cmds > > } > > I'd suggest using a ruby array at this stage. Cleaner. > > > An example of API will look like this: > > register(&mytabs, .... ) > > > What is the best way of wrapping this in Ruby? > > Well, the BEST way is to not wrap it at all: > > def func_a; ...; end > def func_b; ...; end > > msgs = [:func_a, :func_b] > > msgs.each do |msg| > send msg > end > > Short of that, you'll want to poke around with DataWrapStruct.