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 01:47 AM, hedgehogshiatus@gmail.com wrote:


On Wed, Nov 11, 2009 at 4:10 PM, Wayne Meissner <wmeissner@gmail.com>
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 ]

My vote would be for such a 'new' to accept (valid) vanilla Ruby:

ref = FFI::Reference.new Array.new(String.new)
str = ref.get # returns "" for nil values
and
ref = FFI::Reference.new Array.new(sz, String.new) str = ref.get #
returns array of size sz, with "" for nil values
and
ref = FFI::Reference.new Array.[]('a','b','z') # returns array of size
3, with 'a','b' or 'z' for nil values

not sure if that makes sense?
Cheers


+1

-- bk