A C++ reference in a Ruby extension

D

David Simas

I'm trying to write an extension to access some C++ libraries from
Ruby (1.8.4). One of the C++ functions uses a reference to pass
a value back to the caller as a side effect:

ErrorHandle* DetectInvisibleWatermark(const char *inputfilename,
const char *wmfilename,
float horizOffset,
float vertOffset,
float lowPassFilterFactor,
float &modulationStrength,
const char *key, const char *seed,
const char *outfilename);

(N.B.: "float &modulationStrength")

Any suggestions on how to handle this reference in an extension?
I don't have the C++ sources, just libraries and header files.

Thanks,

DGS
 
L

Logan Capaldo

I'm trying to write an extension to access some C++ libraries from
Ruby (1.8.4). One of the C++ functions uses a reference to pass
a value back to the caller as a side effect:

ErrorHandle* DetectInvisibleWatermark(const char *inputfilename,
const char *wmfilename,
float horizOffset,
float vertOffset,
float lowPassFilterFactor,
float &modulationStrength,
const char *key, const char *seed,
const char *outfilename);

(N.B.: "float &modulationStrength")
Wrap it in a C function
void foo(float* bar) {
baz(*bar);
}

void baz(float& quux) {
quux /= 1.5;
}
 
J

Joel VanderWerf

Logan said:
Wrap it in a C function
void foo(float* bar) {
baz(*bar);
}

void baz(float& quux) {
quux /= 1.5;
}

There's still the question of how to get both return values back to the
caller. It depends on how you want to use the function from ruby. You
could return both the ErrorHandle and a float, as a two element array (a
ruby array, that is). The caller can then unpack them like this:

err_h, mod_str = wrap_DetectInvisibleWatermark(...)

Or you could return one and yield the other (a good choice if one is
less likely to be used).

Or maybe return the float (as a ruby float, of course) and rb_raise() if
there is a nonzero ErrorHandle* .
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top