[Swig-user] enum constants vs. functions returning an enum value
Chris Shoemaker
c.shoemaker at cox.net
Wed Aug 16 21:19:11 EDT 2006
On Wed, Aug 16, 2006 at 12:06:39AM -0500, John Lenz wrote:
> On 08/15/06 23:14, Chris Shoemaker wrote:
> >
> > And I was wrong about that last part. G-wrap does it right from C.
> >
> > So, SWIG uses scm_c_define_gsubr() to convert the enum value to a
> > _function_ returning the enum value.
> >
> > Is there some reason why it doesn't include:
> >
> > scm_c_define("GNC-OWNER-EMPLOYEE",
> > scm_long2_num(gswig_const_GNC_OWNER_EMPLOYEE));
> >
> > inside the scm_init_FOO_module() function? That treatment of enum
> > values would allow: (eq? GNC-OWNER-EMPLOYEE (gncOwnerGetType employee))
> >
> > which seems more natural to me.
> >
> > I've looked at the "enum SWIGTYPE" typemaps for guile, but I didn't
> > see any mechanism for generating code into the scm_init_FOO_module()
> > function. Is that even possible? Thanks.
>
> Not from a typemap (because typemaps assume a function around them), but
> it is possible. The way it would have to work is Source/Lib/guile.cxx
> would have to be modified to either produce a constant directly in a
> static variable, and then insert the code to register it into the %init
> section, or leave the enum function generated as is in the wrapper
> function, and then add code that does
>
> (define GNC-OWNER-EMPLOYEE (GNC-OWNER-EMPLOYEE))
>
> This is the easist solution, you just need to add that code into the
> %scheme { } section of the .i file. There are two ways I see to do that
>
> 1) Run swig -xml or swig -sexp (probably want sexp) and parse the output
> to generate all the (define ...) statements into a file, and then
> include that generated file in the .i file
>
> 2) Modify SWIG to (optionally) emit the code itself (like on an
> enumasvar feature). This wouldn't be too difficult... just override the
> enumvalueDeclaration(Node *n) function in guile.cxx. Just copy it from
> lang.cxx, and add a few lines like
>
> if (Getflag(n, "feature:enumasvar") {
> Printf(scmtext, "(define %s (%s))", name);
> }
>
3?) [Possible short-term solution for the lazy and impatient]
Manually insert "scm_c_eval_string("(set! GNC-OWNER-EMPLOYEE
(GNC-OWNER-EMPLOYEE))" into %init {} section of .i files for all enum
values.
(To explain why not %scheme {}, I should note that I'm not using
-scmstub.)
> If you are interested, I could add this feature to guile (and probably
> add it to chicken at the same time). This solution would require you to
> use the CVS version of SWIG until the next release is made...
Assuming this behavior makes as much sense to other people as it does
to me, I think the idea of using a static variable and a
scm_c_define() is a good change. And we can wait for a formal release
by using the short-term solution above. The only thing I wonder about
is... wouldn't that be a not-backward-compatible change to swig?
-chris
More information about the gnucash-devel
mailing list