« Return to Thread: Q: SWIG and dynamic attributes

Re: Q: SWIG and dynamic attributes

by wsfulton :: Rate this Message:

Reply to Author | View in Thread

Anton D Kachalov wrote:

> Hello, all!
>
> I have a question.
> I develop a small library that has structure represented a few types in
> union such as:
>
> struct my_type {
>    enum types;
>    union {
>       int i;
>       float f;
>       char *s;
>    } u;
> }
>
> Some functions just return or pass-in "struct my_type". I would like to
> make a bindings to Python/Ruby (and probably Perl) for that functions.
> Moreover, I would like to convert "struct my_type" to native types of
> destination scripting languages, e.g. lists/arrays, integers, strings
> via %typemap(out) struct my_type or convert back from different types by
> defining a number of same functions with different input variable types:
>
> int my_func(int i) {
>     struct my_type t; t.type = my_type_integer;
>     t.u.i = i;
> }
>
> int my_func(float f) {
>     struct my_type t; t.type = my_type_float;
>     t.u.f = f;
> }
>
> int my_func(const char *s) {
>     struct my_type t; t.type = my_type_string;
>     t.u.s = strdup(s);
> }
> Is there a shorter way for input? Or just define function for each input
> param type?
SWIG will only wrap what you give it, so you need to provide these
functions. You can make your code a bit shorter by using the
preprocessor and a C macro as SWIG has a preprocessor.

> May I accept as input some kind of "abstract" type of target
> language and then convert content to my internal representation
> utilizing "struct my_type"?
If you can write python/ruby/perl/whatever code in C to take your
abstract type and convert it to what is required to create a my_type,
then yes. You'd need to write typemaps for struct my_type to handle this.

William

------------------------------------------------------------------------------
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user

 « Return to Thread: Q: SWIG and dynamic attributes