<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Hello,</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
I wanted to add that an anonymous structs 'foo' located in another struct 'bar' SIP can handle when giving 'foo' a type name and initializing them in 'bar'.</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
That means I was wrong, when handling a union the anonymity should be taken away too. This unfortunately still leaves the problem with the error described in my last mail, when a union is located inside a struct.</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
M.T.<br>
</div>
<div id="appendonsend"></div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>Von:</b> Marian Thomsen <marian.th@outlook.de><br>
<b>Gesendet:</b> Dienstag, 1. Dezember 2020 12:04<br>
<b>An:</b> Phil Thompson <phil@riverbankcomputing.com><br>
<b>Cc:</b> pyqt@riverbankcomputing.com <pyqt@riverbankcomputing.com><br>
<b>Betreff:</b> AW: AW: SIP translate Union</font>
<div> </div>
</div>
<style type="text/css" style="display:none">
<!--
p
{margin-top:0;
margin-bottom:0}
-->
</style>
<div dir="ltr">
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
Thank you very much, the last example helped me a lot!</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
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.</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
Here I tried by wrapping it inside a struct, but get the error message: 'field 'u' has incomplete type 'mu'.<br>
</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
test_union.h<br>
</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<pre style="background-color:#ffffff; color:#080808; font-family:'JetBrains Mono',monospace; font-size:9.8pt"><span style="color:#0033b3">struct </span><span style="color:#008080">myType</span>{<br><br> <span style="color:#008080">enum </span>{<span style="color:#871094; font-style:italic">CHAR</span>, <span style="color:#871094; font-style:italic">INT</span>, <span style="color:#871094; font-style:italic">DOUBLE</span>} <span style="color:#660e7a">tag</span>;<br> <span style="color:#0033b3">union</span>{ // tested naming it 'mu', no difference<br> <span style="color:#8c8c8c; font-style:italic">/* tag = CHAR */<br></span><span style="color:#8c8c8c; font-style:italic"> </span><span style="color:#0033b3">char </span><span style="color:#660e7a">char_value</span>;<br> <span style="color:#8c8c8c; font-style:italic">/* tag = INT */<br></span><span style="color:#8c8c8c; font-style:italic"> </span><span style="color:#0033b3">int </span><span style="color:#660e7a">int_value</span>;<br> <span style="color:#8c8c8c; font-style:italic">/* tag = DOUBLE */<br></span><span style="color:#8c8c8c; font-style:italic"> </span><span style="color:#0033b3">double </span><span style="color:#660e7a">double_value</span>;<br> }<span style="color:#660e7a">u</span>;<br> <span style="color:#0033b3">void </span><span style="color:#00627a">print_my_type</span>() <span style="color:#0033b3">const</span>;<br>};<br><br><br><span style="font-family:calibri,arial,helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0); background-color:rgb(255,255,255)">The SIP file:</span><br><br><pre style="background-color:#ffffff; color:#080808; font-family:'JetBrains Mono',monospace; font-size:9.8pt">struct myType{<br><br>%TypeHeaderCode<br>#include <test_union.h><br>%End<br><br> enum tag {CHAR, INT, DOUBLE};<br><br> struct mu_wrapper /PyName=mu/<br> {<br><br> %TypeHeaderCode<br> #include <test_union.h><br><br> struct mu_wrapper<br> {<br> union mu u;<br> };<br> %End<br><br> /* tag = CHAR */<br> char char_value {<br> %GetCode<br> sipPy = PyUnicode_FromString(&(sipCpp->u.char_value));<br> %End<br> %SetCode<br> if (PyUnicode_Check(sipPy))<br> sipCpp->u.char_value;<br> else<br> sipErr = 1;<br> %End<br> };<br><br> /* tag = INT */<br> int int_value {<br> %GetCode<br> sipPy = PyLong_FromLong(sipCpp->u.int_value);<br> %End<br> %SetCode<br> if (PyLong_Check(sipPy))<br> sipCpp->u.int_value;<br> else<br> sipErr = 1;<br> %End<br> };<br><br> /* tag = DOUBLE */<br> double double_value {<br> %GetCode<br> sipPy = PyFloat_FromDouble(sipCpp->u.double_value);<br> %End<br> %SetCode<br> if (PyFloat_Check(sipPy))<br> sipCpp->u.double_value;<br> else<br> sipErr = 1;<br> %End<br> };<br> };<br><br> void print_my_type() const;<br>};</pre><br></pre>
Thank you for your help!</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
M.T.<br>
</div>
<div id="x_appendonsend"></div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<hr tabindex="-1" style="display:inline-block; width:98%">
<div id="x_divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" color="#000000" style="font-size:11pt"><b>Von:</b> Phil Thompson <phil@riverbankcomputing.com><br>
<b>Gesendet:</b> Freitag, 27. November 2020 11:52<br>
<b>An:</b> Marian Thomsen <marian.th@outlook.de><br>
<b>Cc:</b> pyqt@riverbankcomputing.com <pyqt@riverbankcomputing.com><br>
<b>Betreff:</b> Re: AW: SIP translate Union</font>
<div> </div>
</div>
<div class="x_BodyFragment"><font size="2"><span style="font-size:11pt">
<div class="x_PlainText">On 26/11/2020 08:08, Marian Thomsen wrote:<br>
> Thank you for the advice!<br>
> <br>
> Unfortunately I cant get a simple example to work.<br>
> I have a simple union in the header test_union.h<br>
> <br>
> <br>
> union {<br>
> int test1;<br>
> int test2;<br>
> } u;<br>
> <br>
> And this is the best I got so far in the .sip file:<br>
> <br>
> <br>
> struct union{<br>
> <br>
> %TypeHeaderCode<br>
> #include <test_union.h><br>
> %End<br>
> <br>
> int *test1 {<br>
> %GetCode<br>
> sipPy = PyLong_FromLongLong(sipCpp->test1)<br>
> if(sipPy == NULL)<br>
> sipPy = NULL;<br>
> %End<br>
> %SetCode<br>
> if (PyLong_Check(sipPy))<br>
> sipCpp->test1;<br>
> else<br>
> sipErr = 1;<br>
> %End<br>
> };<br>
> int *test2 {<br>
> %GetCode<br>
> sipPy = PyLong_FromLongLong(sipCpp->test2);<br>
> if(sipPy == NULL)<br>
> sipPy = NULL;<br>
> %End<br>
> %SetCode<br>
> if (PyLong_Check(sipPy))<br>
> sipCpp->test2;<br>
> else<br>
> sipErr = 1;<br>
> %End<br>
> };<br>
> }u;<br>
> <br>
> I get a syntax error in the last line and have tried to write it so<br>
> that sip would understand it, but also with something like:<br>
> <br>
> <br>
> typedef struct union union;<br>
> union u{ ... };<br>
> <br>
> I got no luck so far. Am I completly wrong?<br>
> <br>
> Some advice would be appreciated<br>
<br>
First of all, the contents of your test_union.h file is the definition <br>
of a variable, not a type. It should be...<br>
<br>
union u {<br>
int test1;<br>
int test2;<br>
};<br>
<br>
At the moment you can't wrap a union directly. You have to wrap it in a <br>
struct and pretend to Python that the struct is the union. Also there is <br>
a bug in the C code generator which generates invalid code (but the <br>
equivalent C++ code is Ok). So try something like the following...<br>
<br>
struct u_wrapper /PyName=u/<br>
{<br>
%TypeHeaderCode<br>
#include<test_union.h><br>
<br>
struct u_wrapper<br>
{<br>
union u wrapped;<br>
};<br>
%End<br>
<br>
int test1 {<br>
%GetCode<br>
sipPy = PyLong_FromLongLong(sipCpp->wrapped.test1);<br>
%End<br>
%SetCode<br>
if (PyLong_Check(sipPy))<br>
sipCpp->wrapped.test1;<br>
else<br>
sipErr = 1;<br>
%End<br>
};<br>
};<br>
<br>
Phil<br>
</div>
</span></font></div>
</div>
</body>
</html>