[cffi-devel] mem-aref and complex numbers

Tamas K Papp tkpapp at gmail.com
Sun Aug 16 20:06:16 UTC 2009


On Sun, 16 Aug 2009 17:43:04 +0100, Luís Oliveira wrote:

> On Sun, Aug 16, 2009 at 5:14 PM, Tamas K Papp<tkpapp at gmail.com> wrote:
>> Is there a way to make mem-aref work with CL's complex numbers?  I
>> don't need anything else from CFFI (no complex function arguments,
>> return types, etc), just mem-aref, eg something like
>>
>> (mem-aref ptr :complex-float 11)
> 
> That is doable with something like:
> 
> (defmethod translate-from-foreign (ptr (type complex-float-type))
>   (complex (mem-aref ptr :float 0) (mem-aref ptr :float 1)))
> 
> 
>> (setf (mem-aref ptr :complex-float 12) #C(1s0 2s0))
> 
> But that isn't. Would defining your own setter work for you?
> 
> (defun complex-float-aref (ptr index c)
>   (let ((p (* (foreign-type-size :complex-float) index)))
>     (setf (mem-aref p :float 0) (realpart c)
>             (mem-aref p :float 1) (imagpart c))))
> 
> To do that with plain MEM-AREF as you wanted, I think we would need some
> sort of TRANSLATE-INTO-FOREIGN hook.

Thanks for the quick reply.  Yes, my own setter works, and I managed
to prettify my code by writing a wrapper around mem-aref that calls
the setter above when necessary.  But if mem-ref and mem-aref could be
extended to handle C99 complex types in the future, that would be neat
(that means that my code would more more standard, and I would benefit
from optimizations like compiler macros, etc).

Tamas





More information about the cffi-devel mailing list