lua-users home
lua-l archive

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


Can arguments and return values be passed by reference?

I'm trying to work out how I would bind the following C function:

  int * inc (int *p)
  {
    (*p)++;
    return p;
  }

e.g.

  int x = 6;
  inc(inc(&x));
  assert(x == 8);

(This is a trivial example but is representative of a class of functions
in the C++ API I am trying to bind.)

In my bindings, C ints are represented in Lua as numbers. So, the above C
example would be as follows in lua:

  x = 6
  inc(inc(x))
  -- x now equals 8

Any thoughts? If there is no way to pass or return values by reference, do
I just outlaw pointers (and C++ references) in the C++ API?


BTW the binding is automated by way of a SWIG Lua 5.0 language module; if
anyone else is working on one, I would be happy to stop developing it and
be a guinea pig for someone else's.

A real function I'm trying to bind (if the above does not please you) is

        const Vector3 & Vector3::Normalize()
        {
                float mag = Magnitude();

                if(mag == 0.0f)
                        return *this;

                float oo_mag = 1.0f/mag;
                mx *= oo_mag;
                my *= oo_mag;
                mz *= oo_mag;

                return *this;
        }