Subject:
[ruby-ffi] Re: FFI core concepts wiki documentation
From:
Wayne Meissner
Date:
11/10/09 11:10 PM
To:
ruby-ffi@googlegroups.com


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.

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.