0

I'm using SIP version 5.4.0 and have no trouble translating a struct in c++ for python3.8. When I want to translate a union sip-install gives me a syntax error. Any ideas?

Thank you for an answer.

Johnny

  • https://www.riverbankcomputing.com/static/Docs/sip/specification_files.html does not mention `union` at all, so that explains the syntax error: unions are not supported directly. I don't know enough about SIP to offer any alternatives though; hopefully someone else will chime in. – Thomas Nov 17 '20 at 09:22
  • @Thomas thanks for a quick look, I did not see that – JohnnyWaker Nov 17 '20 at 09:51
  • 1
    Note: Unions are supported since SIP version 6.0.0 – JohnnyWaker Jan 08 '21 at 10:16

1 Answers1

0

The Trick is to wrap it in a struct and use %GetCode and %SetCode for each union element. The following example should make things clear:

The following header file:

union test_u {
    char test1;
    int test2;
    double test3;
};

is translated with this SIP file:

struct union_wrapper /PyName=test_u/
{

%TypeHeaderCode
#include<test_union.h>

struct union_wrapper
{
    union test_u wrapped_u;
};
%End

    char test1 {
    %GetCode
        sipPy = PyUnicode_FromString(&(sipCpp->wrapped_u.test1));
    %End
    %SetCode
        if (PyUnicode_Check(sipPy))
            sipCpp->wrapped_u.test1;
        else
            sipErr = 1;
    %End
    };

    int test2 {
    %GetCode
        sipPy = PyLong_FromLong(sipCpp->wrapped_u.test2);
    %End
    %SetCode
        if (PyLong_Check(sipPy))
            sipCpp->wrapped_u.test2;
        else
            sipErr = 1;
    %End
    };

    double test3 {
    %GetCode
        sipPy = PyFloat_FromDouble(sipCpp->wrapped_u.test3);
    %End
    %SetCode
        if (PyFloat_Check(sipPy))
            sipCpp->wrapped_u.test3;
        else
            sipErr = 1;
    %End
    };
};

This solution is not my achievement, but I don't know if I can name names. Thank you very much!

Johnny Walker