lua-users home
lua-l archive

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


Hi everyone,
I have a design issue in wrapping a matrix class for Lua.  The
matrices can be char, long, float, or double in type and some
functions need to be able to determine if the lua values passed in are
ints or float values.  For example, a setcell methods looks like this:

matrix:setcell(1, 0, "val", {0, 1, 2, 3})

Here 1, 0 is the cell and an array of values {0, 1, 2, 3} are supposed
to be placed in that cell.  If matrix is of type char, then I need the
char values 0, 1, 2, 3 but if I naively get the values from Lua by
doing lua_tonumber, then the array  {0, 255, 255, 255} is what is put
in the cell because float values are clamed to the range {0, 1} and
values 1 and > translate into 255.  Now I realize I could check the
type of the matrix and do lua_tonumber, but what I'd really like to do
is determine if the values in lua have a '.' in their string
representation and then call lua_tonumber or lua_tointeger.  As an
example:

mat:setcell(1, 0, "val", {0., 1., 2., 3.}) -> should call lua_tonumber
mat:setcell(1, 0, "val", {0, 1, 2, 3}) -> should call lua_tointeger

What is the best practice method of doing this determination?

thanks,
wes