AW: AW: SIP translate Union
Marian Thomsen
marian.th at outlook.de
Tue Dec 1 11:04:24 GMT 2020
Thank you very much, the last example helped me a lot!
I feel like I'm asking too much, but if you have some time I'm stuck translating an (anonymos) union inside a struct. The anonymity does not seem to be the problem (I tested by naming it), but the location inside the struct. Another aproach was to use the existing struct as a wrapper, but I couldn't get it to work.
Here I tried by wrapping it inside a struct, but get the error message: 'field 'u' has incomplete type 'mu'.
test_union.h
struct myType{
enum {CHAR, INT, DOUBLE} tag;
union{ // tested naming it 'mu', no difference
/* tag = CHAR */
char char_value;
/* tag = INT */
int int_value;
/* tag = DOUBLE */
double double_value;
}u;
void print_my_type() const;
};
The SIP file:
struct myType{
%TypeHeaderCode
#include <test_union.h>
%End
enum tag {CHAR, INT, DOUBLE};
struct mu_wrapper /PyName=mu/
{
%TypeHeaderCode
#include <test_union.h>
struct mu_wrapper
{
union mu u;
};
%End
/* tag = CHAR */
char char_value {
%GetCode
sipPy = PyUnicode_FromString(&(sipCpp->u.char_value));
%End
%SetCode
if (PyUnicode_Check(sipPy))
sipCpp->u.char_value;
else
sipErr = 1;
%End
};
/* tag = INT */
int int_value {
%GetCode
sipPy = PyLong_FromLong(sipCpp->u.int_value);
%End
%SetCode
if (PyLong_Check(sipPy))
sipCpp->u.int_value;
else
sipErr = 1;
%End
};
/* tag = DOUBLE */
double double_value {
%GetCode
sipPy = PyFloat_FromDouble(sipCpp->u.double_value);
%End
%SetCode
if (PyFloat_Check(sipPy))
sipCpp->u.double_value;
else
sipErr = 1;
%End
};
};
void print_my_type() const;
};
Thank you for your help!
M.T.
________________________________
Von: Phil Thompson <phil at riverbankcomputing.com>
Gesendet: Freitag, 27. November 2020 11:52
An: Marian Thomsen <marian.th at outlook.de>
Cc: pyqt at riverbankcomputing.com <pyqt at riverbankcomputing.com>
Betreff: Re: AW: SIP translate Union
On 26/11/2020 08:08, Marian Thomsen wrote:
> Thank you for the advice!
>
> Unfortunately I cant get a simple example to work.
> I have a simple union in the header test_union.h
>
>
> union {
> int test1;
> int test2;
> } u;
>
> And this is the best I got so far in the .sip file:
>
>
> struct union{
>
> %TypeHeaderCode
> #include <test_union.h>
> %End
>
> int *test1 {
> %GetCode
> sipPy = PyLong_FromLongLong(sipCpp->test1)
> if(sipPy == NULL)
> sipPy = NULL;
> %End
> %SetCode
> if (PyLong_Check(sipPy))
> sipCpp->test1;
> else
> sipErr = 1;
> %End
> };
> int *test2 {
> %GetCode
> sipPy = PyLong_FromLongLong(sipCpp->test2);
> if(sipPy == NULL)
> sipPy = NULL;
> %End
> %SetCode
> if (PyLong_Check(sipPy))
> sipCpp->test2;
> else
> sipErr = 1;
> %End
> };
> }u;
>
> I get a syntax error in the last line and have tried to write it so
> that sip would understand it, but also with something like:
>
>
> typedef struct union union;
> union u{ ... };
>
> I got no luck so far. Am I completly wrong?
>
> Some advice would be appreciated
First of all, the contents of your test_union.h file is the definition
of a variable, not a type. It should be...
union u {
int test1;
int test2;
};
At the moment you can't wrap a union directly. You have to wrap it in a
struct and pretend to Python that the struct is the union. Also there is
a bug in the C code generator which generates invalid code (but the
equivalent C++ code is Ok). So try something like the following...
struct u_wrapper /PyName=u/
{
%TypeHeaderCode
#include<test_union.h>
struct u_wrapper
{
union u wrapped;
};
%End
int test1 {
%GetCode
sipPy = PyLong_FromLongLong(sipCpp->wrapped.test1);
%End
%SetCode
if (PyLong_Check(sipPy))
sipCpp->wrapped.test1;
else
sipErr = 1;
%End
};
};
Phil
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20201201/6afbe52f/attachment.htm>
More information about the PyQt
mailing list