[Rubycocoa-devel 759] C-array pointers of variable length

Back to archive index

Laurent Sansonetti lsans****@apple*****
Fri Feb 23 03:45:44 JST 2007


Hi guys,

I added some better support for functions/methods dealing with C-array  
like arguments and/or return value, whose length isn't fixed and  
depend on the context.

Examples:

CGColorRef CGColorCreate ( CGColorSpaceRef colorspace, const float  
components[] );
// 'components' length depends on the colorspace (it's the number of  
colors + 1)

const float *CGColorGetComponents ( CGColorRef color );
// the return value also depends on the chosen colorspace

Before it was necessary to deal with Array#pack, String#unpack and  
OSX::ObjCPtr.

Now you can also write:

     components = [0.1, 0.5, 0.9, 0]
     color = CGColorCreate(CGColorSpaceCreateDeviceRGB(), components)  
# (1)
     components2 = CGColorGetComponents(color) # (2)
     p components2[0]

Technically,

(1) RubyCocoa is now able to convert a Ruby array to this kind of _C_PTR
(2) RubyCocoa has no way to know the size of the returned array, so it  
still returns an OSX::ObjCPtr object. But it's now possible to call  
OSX::ObjCPtr#[] to retrieve arbitrary elements from the pointer.

Of course, the user needs to be very careful when writing such code.  
Passing a array of non-expected length and/or trying to access an  
element at a non-expected index can crash the current application. But  
this kind of problems exist in C/ObjC too, and also with the previous  
way (using the packing functions).

Laurent



More information about the Rubycocoa-devel mailing list
Back to archive index