Subject:
[ruby-ffi] Re: FFI core concepts wiki documentation
From:
Bryan Kearney
Date:
11/11/09 7:41 AM
To:
ruby-ffi@googlegroups.com


On 11/11/2009 12:10 AM, Wayne Meissner wrote:

2009/11/11 Bryan Kearney<bkearney@redhat.com>:
Passing a Single String by Reference
-------------------------------------

Assume the method:
        int aug_get(const augeas *aug, const char *path, const char **value);

[...]
Passing an Array of Strings back by Reference
----------------------------------------------

int aug_match(const augeas *aug, const char *path, char ***matches);

Since these are pretty common things in C (well, in those types of
apis), I've been wondering if adding a higher-level wrapper would make
it easier to read/less confusing to write.

e.g.
   ref = FFI::Reference.new :string
   AugeasLib.aug_get("foo", "/bar", ref)
   str = ref.get  # returns a string, or nil for null
   # do stuff with str as you would expect

and
   ref = FFI::Reference.new :string_array # or maybe Reference.new [ :string ]
   AugeasLib.aug_match("foo", "/bar", ref)
   str_ary = ref.get  # pulls out all the strings and puts them in an
array, stopping at first null
   str_ary.each { |s| # do stuff with each string }

I think this adds a bit of clarity when reading, since it is easy to
spot that you're just passing in a reference that you expect the C
code to fill out, and not for some more generic pointer/memory use.

+1. Check out JNA ByReferenceClass and StringArray Class https://jna.dev.java.net/nonav/javadoc/index.html. This makes it much easier.


This could all be implemented in a third-party gem, e.g. NiceFFI,
which would be preferable to putting it in ruby-ffi itself, since that
way it can evolve faster, and be used across both CRuby and JRuby.

I dont know the packaging issues for you reference, but I would really like to see it combined. If the packaging makes that not possible, at least document the two together.

-- bk