SWIG / Lua: defining the data type of the member field

SWIG kindly provides the swig_type () function to get a string representation of the data type of the passed userdata object. However, in the case of member fields, SWIG and Lua consider these to be simple "number" elements and therefore only prints the "number" instead of the data type name.

eg.

typedef num_years int;

class Person
{
    public:
           Person * spouse;
           num_years   age;
};

      

in C ++ will result in:

Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio

> p = MyModule.Person();
> print(swig_type(p.spouse));
Person *
> print(swig_type(p.age));
number

      

Is there some kind of backdoor I could use to define the data types of member fields? I can see that the get and set functions have the correct string representation of numeric fields when validating arguments.

Thanks for any help!

+2


source to share


2 answers


SWIG-wrapped C ++ objects are Lua user data objects that conform to a specific set of rules. swig_type()

works by looking at these userdata objects and getting the type name information that SWIG stores there. But it age

appears directly, so there is no type name field to look up, so swig_type()

it can't do its magic. You can get what you want by making num_years an opaque class type that wraps int (and which possibly defines some implicit conversions for ease of use in C ++), but that's a lot of work to go for the dubious reward.



The get and set functions work in a completely different way; they are correctly signed because SWIG stores it in the generated wrapper at compile time, copying from the interface file.

+3


source


I know this is super old, but I suppose I'll name a possible answer.

Wrap years in a structure, for example:



struct year_t
{
    year_t(int year) : m_year(year) {}
    void operator()(int year) { m_year = year; }
    int operator()() const { return m_year; } 
private:
    int m_year;
}

class Person
{
public:
       Person * spouse;
       year_t age;
};

      

0


source







All Articles