lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi Mike,

just a small question. I've seen that if you define a metatype for a
'struct foo' and you have a cdata that is a 'struct foo *' then
metatables does not apply to this latter object. Is this the intended
behaviour ?

I mean, normally you can index both struct instance and pointer in the
same way and for me it was logical that both types are associated to
the same metatable but I'm not completely sure. The __gc metamethod
may be should not be the same for pointers and struct of the same
type.

>From the practical point of view my problem is that the GSL library
does provide the following functions:

gsl_matrix * gsl_matrix_alloc (size_t n1, size_t n2);
gsl_block *gsl_block_alloc (const size_t n);

Here the relevant cdefs:

     typedef struct
     {
       size_t size;
       double * data;
     } gsl_block;

     typedef struct
     {
       size_t size1;
       size_t size2;
       size_t tda;
       double * data;
       gsl_block * block;
       int owner;
     } gsl_matrix;


My problem here is that by calling gsl_matrix_alloc I get a pointer
and not a struct instance and so the metatable that I can associate to
a 'gsl_matrix' does not apply.

Here what I've done for the moment:

local gsl_matrix = ffi.typeof('gsl_matrix')
local double_size = ffi.sizeof('double')

function matrix.new(n1, n2, f)
   local block = cgsl.gsl_block_alloc (n1 * n2)
   local m = gsl_matrix {n1, n2, n2, block.data, block, 1}
   ffi.fill(m.data, n1 * n2 * double_size, 0)
   return m
end

but I was wondering if there was a better solution or a recommended
practice for this case.

Thanks in advance for any help.
-- 
Francesco