using typedefed enums in lua

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

using typedefed enums in lua

Jason Rego

I have a typedefed enum such as the following:

 

typedef enum

{

   StickDir_Invalid = -1,

   StickDir_Center,

   StickDir_Up,

   StickDir_Left,

   StickDir_Right,

   StickDir_Down,

   StickDir_UpLeft,

   StickDir_UpRight,

   StickDir_DownLeft,

   StickDir_DownRight

} StickDirection;

 

Its included in one of the headers inside the %% includes in my bind file, I have not specified it in the expose part of the bind file.  This type (StickDirection) is a parameter to one of the class methods that I've exposed.  The method call works fine when I call with no parameter (all the parameters have default values) but when I try using sending a StickDirection parameter the script fails to run.  I try using this enum in the following manner:

 

Modulename.StickDir_Center

 

Every attempt of putting this typedefed enum in the expose area of the bind file (which I assume I have to) has resulted in redefinition errors when compiling or type cast errors.  How do you expose such an enum for use in lua?

 

 

 

/*

Jason Rego

Software Engineer

Rainbow Studios

[hidden email]

*/

 

Reply | Threaded
Open this post in threaded view
|

Re: using typedefed enums in lua

mark gossage

> Message: 4
> From: Jason Rego <[hidden email]>
> To: "'[hidden email]'"
> <[hidden email]>
> Date: Wed, 11 Jan 2006 17:50:41 -0700
> Subject: [Swig-user] using typedefed enums in lua
>
>
> I have a typedefed enum such as the following:
>  
> typedef enum
> {
>    StickDir_Invalid = -1,
>    StickDir_Center,
>    StickDir_Up,
>    StickDir_Left,
>    StickDir_Right,
>    StickDir_Down,
>    StickDir_UpLeft,
>    StickDir_UpRight,
>    StickDir_DownLeft,
>    StickDir_DownRight
> } StickDirection;
>  
> Its included in one of the headers inside the %% includes in my bind file, I
> have not specified it in the expose part of the bind file.  This type
> (StickDirection) is a parameter to one of the class methods that I've
> exposed.  The method call works fine when I call with no parameter (all the
> parameters have default values) but when I try using sending a
> StickDirection parameter the script fails to run.  I try using this enum in
> the following manner:
>  
> Modulename.StickDir_Center
>  
> Every attempt of putting this typedefed enum in the expose area of the bind
> file (which I assume I have to) has resulted in redefinition errors when
> compiling or type cast errors.  How do you expose such an enum for use in
> lua?
>  

Hello Jason,
I just tried compiling this, it worked fine for me:
=====demo.i===============
%{
        #include "demo.h"
%}
%include "demo.h"
============================
=======demo.h===============
typedef enum
{
StickDir_Invalid = -1,
StickDir_Center,
StickDir_Up,
StickDir_Left,
StickDir_Right,
StickDir_Down,
StickDir_UpLeft,
StickDir_UpRight,
StickDir_DownLeft,
StickDir_DownRight
} StickDirection;

inline void print_stick(StickDirection dir=StickDir_Invalid)
{
        printf("print_stick %d\n",dir);
}
============================

I compiled this down with the CVS SWIG (though earlier version should work) and lua 5.1-alpha
then started lua
> require("demo")
> demo.print_stick()
print_stick -1
> print(demo.StickDir_Invalid) -- only works with the demo prefix
-1
> print(StickDir_Invalid)
nil -- no such value
> demo.print_stick(demo.StickDir_Up)
1  -- all ok

If you look in the wrapper file for the following
static swig_lua_const_info swig_constants[] = {
{ SWIG_LUA_INT,     (char *)"StickDir_Invalid", (long) StickDir_Invalid, 0, 0, 0},
...
    {0,0,0,0,0,0}
};
This will show you all the constants (SWIG treats enums as constants) that SWIG is wrappering.

Hope this helps,
Mark Gossage




-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|

RE: using typedefed enums in lua

Jason Rego
In reply to this post by Jason Rego
Hmm, this might be a compiler issue then.  When I try your example, that is
when I get that type cast error I was talking about.

I'm using visual c++ .net 2003, and it give a bunch of these errors:
Type cast: cannot convert from lua_Number to StickDirection; Conversions
between enumeration and floating point values are no longer allowed.

In these functions in the bind file:

static int _wrap_QueueMember_m_Direction_set(lua_State* L) {
    int SWIG_arg;
    QueueMember *arg1 = (QueueMember *) 0 ;
    StickDirection arg2 ;
   
    if(!lua_isuserdata(L,1)) SWIG_fail_arg(1);
    if(!lua_isnumber(L,2)) SWIG_fail_arg(2);
    arg1=(QueueMember
*)SWIG_MustGetPtr(L,1,SWIGTYPE_p_QueueMember,0,1,"QueueMember_m_Direction_se
t");
    arg2 = (StickDirection)lua_tonumber(L, 2);  //This line
    if (arg1) (arg1)->m_Direction = arg2;
   
    SWIG_arg=0;
   
    return SWIG_arg;
   
    fail:
    lua_error(L);
}

Any ideas?  Do I need to format the enum in a different manner?

-----Original Message-----
From: mark gossage [mailto:[hidden email]]
Sent: Wednesday, January 11, 2006 11:56 PM
To: [hidden email]
Cc: Jason Rego
Subject: Re: [Swig-user] using typedefed enums in lua


> Message: 4
> From: Jason Rego <[hidden email]>
> To: "'[hidden email]'"
> <[hidden email]>
> Date: Wed, 11 Jan 2006 17:50:41 -0700
> Subject: [Swig-user] using typedefed enums in lua
>
>
> I have a typedefed enum such as the following:
>  
> typedef enum
> {
>    StickDir_Invalid = -1,
>    StickDir_Center,
>    StickDir_Up,
>    StickDir_Left,
>    StickDir_Right,
>    StickDir_Down,
>    StickDir_UpLeft,
>    StickDir_UpRight,
>    StickDir_DownLeft,
>    StickDir_DownRight
> } StickDirection;
>  
> Its included in one of the headers inside the %% includes in my bind file,
I
> have not specified it in the expose part of the bind file.  This type
> (StickDirection) is a parameter to one of the class methods that I've
> exposed.  The method call works fine when I call with no parameter (all
the
> parameters have default values) but when I try using sending a
> StickDirection parameter the script fails to run.  I try using this enum
in
> the following manner:
>  
> Modulename.StickDir_Center
>  
> Every attempt of putting this typedefed enum in the expose area of the
bind
> file (which I assume I have to) has resulted in redefinition errors when
> compiling or type cast errors.  How do you expose such an enum for use in
> lua?
>  

Hello Jason,
I just tried compiling this, it worked fine for me:
=====demo.i===============
%{
        #include "demo.h"
%}
%include "demo.h"
============================
=======demo.h===============
typedef enum
{
StickDir_Invalid = -1,
StickDir_Center,
StickDir_Up,
StickDir_Left,
StickDir_Right,
StickDir_Down,
StickDir_UpLeft,
StickDir_UpRight,
StickDir_DownLeft,
StickDir_DownRight
} StickDirection;

inline void print_stick(StickDirection dir=StickDir_Invalid)
{
        printf("print_stick %d\n",dir);
}
============================

I compiled this down with the CVS SWIG (though earlier version should work)
and lua 5.1-alpha
then started lua
> require("demo")
> demo.print_stick()
print_stick -1
> print(demo.StickDir_Invalid) -- only works with the demo prefix
-1
> print(StickDir_Invalid)
nil -- no such value
> demo.print_stick(demo.StickDir_Up)
1  -- all ok

If you look in the wrapper file for the following
static swig_lua_const_info swig_constants[] = {
{ SWIG_LUA_INT,     (char *)"StickDir_Invalid", (long) StickDir_Invalid, 0,
0, 0},
...
    {0,0,0,0,0,0}
};
This will show you all the constants (SWIG treats enums as constants) that
SWIG is wrappering.

Hope this helps,
Mark Gossage



-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|

RE: using typedefed enums in lua

mark gossage
In reply to this post by Jason Rego
Jason Rego <[hidden email]> wrote :

> Hmm, this might be a compiler issue then.  When I try your example, that is
> when I get that type cast error I was talking about.
>
> I'm using visual c++ .net 2003, and it give a bunch of these errors:
> Type cast: cannot convert from lua_Number to StickDirection; Conversions
> between enumeration and floating point values are no longer allowed.
>
> In these functions in the bind file:
>
> static int _wrap_QueueMember_m_Direction_set(lua_State* L) {
>     int SWIG_arg;
>     QueueMember *arg1 = (QueueMember *) 0 ;
>     StickDirection arg2 ;
>    
>     if(!lua_isuserdata(L,1)) SWIG_fail_arg(1);
>     if(!lua_isnumber(L,2)) SWIG_fail_arg(2);
>     arg1=(QueueMember
> *)SWIG_MustGetPtr(L,1,SWIGTYPE_p_QueueMember,0,1,&quot;QueueMember_m_Direction_se
> t&quot;);
>     arg2 = (StickDirection)lua_tonumber(L, 2);  //This line
>     if (arg1) (arg1)-&gt;m_Direction = arg2;
>    
>     SWIG_arg=0;
>    
>     return SWIG_arg;
>    
>     fail:
>     lua_error(L);
> }
>
> Any ideas?  Do I need to format the enum in a different manner?
>

Hello Jason,

I am using MingW gcc version 3.2 and it works fine. So no idea on that one. I also compared our binding code, they look identical.
Is the enum a global thing, or is it part of a class? Are you using C or C++?
I tried the following (amended) code snipped on my machine, it worked ok. Can you try just this code & see if it works on your compiler. If it doesn't then it may be something about the new compiler.

===========demo.i=======
%module demo
%inline %{
typedef enum
{
StickDir_Invalid = -1,
StickDir_Center,
StickDir_Up,
StickDir_Left,
StickDir_Right,
StickDir_Down,
StickDir_UpLeft,
StickDir_UpRight,
StickDir_DownLeft,
StickDir_DownRight
} StickDirection;

struct QueueMember
{
        StickDirection m_Direction;
};

inline void print_QueueMember(const QueueMember& qm)
{
        printf("print_QueueMember %d\n",qm.m_Direction);
}
%}
================================
Then compile it
swig.exe -c++ -lua -o demo_wrap.cxx demo.i
compile & link as usual.

Can you try this & let me know if it works.

Thanks,
Mark Gossage





-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|

RE: using typedefed enums in lua

Jason Rego
In reply to this post by Jason Rego
Yes, I get the same exact compile error.  It seems that the .net compiler
chokes on typecasting the return value of lua_tonumber (which is a double)
to the enum StickDirection.

I did some tests on this and these are the results:

//This doesn't compile, this is essential what the bind file is doing

StickDirection sdir;
sdir = (StickDirection)5.0;

//This compiles and works fine, typecasting from a int is no problem

StickDirection sdir;
sdir = (StickDirection)5;

//This also compiles, seems like .net doesn't mind this "hack" enough to
//complain about it

StickDirection sdir;
sdir = (StickDirection)(int)5.0;

-----Original Message-----
From: 'mark gossage' [mailto:[hidden email]]
Sent: Thursday, January 12, 2006 6:39 PM
To: Jason Rego
Cc: [hidden email]
Subject: RE: [Swig-user] using typedefed enums in lua

Jason Rego <[hidden email]> wrote :

> Hmm, this might be a compiler issue then.  When I try your example, that
is

> when I get that type cast error I was talking about.
>
> I'm using visual c++ .net 2003, and it give a bunch of these errors:
> Type cast: cannot convert from lua_Number to StickDirection; Conversions
> between enumeration and floating point values are no longer allowed.
>
> In these functions in the bind file:
>
> static int _wrap_QueueMember_m_Direction_set(lua_State* L) {
>     int SWIG_arg;
>     QueueMember *arg1 = (QueueMember *) 0 ;
>     StickDirection arg2 ;
>    
>     if(!lua_isuserdata(L,1)) SWIG_fail_arg(1);
>     if(!lua_isnumber(L,2)) SWIG_fail_arg(2);
>     arg1=(QueueMember
>
*)SWIG_MustGetPtr(L,1,SWIGTYPE_p_QueueMember,0,1,&quot;QueueMember_m_Directi
on_se

> t&quot;);
>     arg2 = (StickDirection)lua_tonumber(L, 2);  //This line
>     if (arg1) (arg1)-&gt;m_Direction = arg2;
>    
>     SWIG_arg=0;
>    
>     return SWIG_arg;
>    
>     fail:
>     lua_error(L);
> }
>
> Any ideas?  Do I need to format the enum in a different manner?
>

Hello Jason,

I am using MingW gcc version 3.2 and it works fine. So no idea on that one.
I also compared our binding code, they look identical.
Is the enum a global thing, or is it part of a class? Are you using C or
C++?
I tried the following (amended) code snipped on my machine, it worked ok.
Can you try just this code & see if it works on your compiler. If it doesn't
then it may be something about the new compiler.

===========demo.i=======
%module demo
%inline %{
typedef enum
{
StickDir_Invalid = -1,
StickDir_Center,
StickDir_Up,
StickDir_Left,
StickDir_Right,
StickDir_Down,
StickDir_UpLeft,
StickDir_UpRight,
StickDir_DownLeft,
StickDir_DownRight
} StickDirection;

struct QueueMember
{
        StickDirection m_Direction;
};

inline void print_QueueMember(const QueueMember& qm)
{
        printf("print_QueueMember %d\n",qm.m_Direction);
}
%}
================================
Then compile it
swig.exe -c++ -lua -o demo_wrap.cxx demo.i
compile & link as usual.

Can you try this & let me know if it works.

Thanks,
Mark Gossage




-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|

RE: using typedefed enums in lua

mark gossage
In reply to this post by Jason Rego
Hello Jason,

Thank You Mr. Gates for another non standard feature for your software.

I will add a patch into the CVS shortly to fix this, but based upon what you say, we can fix the problem with updating the typemaps.

Open up /SWIG/Lib/lua/lua.swg

find
=======================================
%typemap(in,checkfn="lua_isnumber") int,short,long,
             unsigned int,unsigned short,unsigned long,
             signed char,unsigned char,
             float,double,enum SWIGTYPE
%{$1 = ($type)lua_tonumber(L, $input);%}

%typemap(out) int,short,long,
             unsigned int,unsigned short,unsigned long,
             signed char,unsigned char,
             float,double,enum SWIGTYPE
%{  lua_pushnumber(L, (double) $1); SWIG_arg++;%}
=====================================
and replace with
=======================================
%typemap(in,checkfn="lua_isnumber") int,short,long,
             unsigned int,unsigned short,unsigned long,
             signed char,unsigned char,
             float,double
%{$1 = ($type)lua_tonumber(L, $input);%}

%typemap(out) int,short,long,
             unsigned int,unsigned short,unsigned long,
             signed char,unsigned char,
             float,double
%{  lua_pushnumber(L, (double) $1); SWIG_arg++;%}

// enums have to be handled slightly differently
/* VC++ .net will not allow a cast from double to enum directly
        therefore cast to an int first.
        Thanks to Jason Rego <[hidden email]> for finding this
*/
%typemap(in,checkfn="lua_isnumber") enum SWIGTYPE
%{$1 = ($type)(int)lua_tonumber(L, $input);%}

%typemap(out) enum SWIGTYPE
%{  lua_pushnumber(L, (double)(int)($1)); SWIG_arg++;%}
===============================================
Try this & let me know if it works. If so, I will commit this into the CVS.

Thanks,
Mark Gossage







-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|

RE: using typedefed enums in lua

Jason Rego
In reply to this post by Jason Rego
Yes, this works.  Thanks so much for timely help on this matter.

-----Original Message-----
From: 'mark gossage' [mailto:[hidden email]]
Sent: Tuesday, January 17, 2006 12:44 AM
To: Jason Rego
Cc: [hidden email]
Subject: RE: [Swig-user] using typedefed enums in lua

Hello Jason,

Thank You Mr. Gates for another non standard feature for your software.

I will add a patch into the CVS shortly to fix this, but based upon what you
say, we can fix the problem with updating the typemaps.

Open up /SWIG/Lib/lua/lua.swg

find
=======================================
%typemap(in,checkfn="lua_isnumber") int,short,long,
             unsigned int,unsigned short,unsigned long,
             signed char,unsigned char,
             float,double,enum SWIGTYPE
%{$1 = ($type)lua_tonumber(L, $input);%}

%typemap(out) int,short,long,
             unsigned int,unsigned short,unsigned long,
             signed char,unsigned char,
             float,double,enum SWIGTYPE
%{  lua_pushnumber(L, (double) $1); SWIG_arg++;%}
=====================================
and replace with
=======================================
%typemap(in,checkfn="lua_isnumber") int,short,long,
             unsigned int,unsigned short,unsigned long,
             signed char,unsigned char,
             float,double
%{$1 = ($type)lua_tonumber(L, $input);%}

%typemap(out) int,short,long,
             unsigned int,unsigned short,unsigned long,
             signed char,unsigned char,
             float,double
%{  lua_pushnumber(L, (double) $1); SWIG_arg++;%}

// enums have to be handled slightly differently
/* VC++ .net will not allow a cast from double to enum directly
        therefore cast to an int first.
        Thanks to Jason Rego <[hidden email]> for finding this
*/
%typemap(in,checkfn="lua_isnumber") enum SWIGTYPE
%{$1 = ($type)(int)lua_tonumber(L, $input);%}

%typemap(out) enum SWIGTYPE
%{  lua_pushnumber(L, (double)(int)($1)); SWIG_arg++;%}
===============================================
Try this & let me know if it works. If so, I will commit this into the CVS.

Thanks,
Mark Gossage






-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user