|
View:
New views
2 Messages
—
Rating Filter:
Alert me
|
|
|
Q: SWIG and dynamic attributesHello, 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? 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"? More over, my library has abstract "objects" that may have a number of "attributes" that is defined and stored as a hash vector (pairs of string and "struct my_type" values). So, I would like to have ability to access those attributes from Python/Ruby: myobj = Mymodule::Object.new("I'm a string") myobj.attrib1 = 0xdead myobj.attrib2 = "foobar" so, actually, Object class / structure doesn't have "attrib1/attrib2" fields defined. They should be dynamically allocated. I know, that in Python there is a __{set,get}attr__ methods that can be defined in the C extension. http://code.activestate.com/recipes/54352/ (this is a little bit outdated example in the comment). Is it possible to define something similar to __setattr__/__getattr__ in SWIG for Python/Ruby bindings? Or redefine "." access methods in Ruby? Or make some other hacks if itsn't possible to do within SWIG. Thank you. Rgds, Anton ------------------------------------------------------------------------------ _______________________________________________ Swig-user mailing list Swig-user@... https://lists.sourceforge.net/lists/listinfo/swig-user |
|
|
Re: Q: SWIG and dynamic attributesAnton 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? 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 |
| Free embeddable forum powered by Nabble | Forum Help |