简体   繁体   中英

How to translate a union inside a struct from c++ to python with SIP?

I answered this question explaining how to translate a union from c++ to python with SIP (unions are not automatically supported).

When I use this type of wrapping for a union inside an existing struct I get the following error message when I try to build with sip-install (I renamed 'wrapped_u' to 'u' and 'test_u' to 'type_u'):

error: field 'u' has incomplete type 'type_u'

and

note: forward declaration of 'union type_u' .

Here it the c++ header file:

struct myType{

    enum {CHAR, INT, DOUBLE} tag;
    union type_u{
        /* tag = CHAR */
        char char_value;
        /* tag = INT */
        int int_value;
        /* tag = DOUBLE */
        double double_value;
    };
    type_u u;
    void print_my_type() const;
};

and this is the SIP file:

struct myType{

    enum tag {CHAR, INT, DOUBLE};

    struct union_wrapper /PyName=type_u/
    {
        %TypeHeaderCode
        #include <test_struct_union.h>

        struct union_wrapper
        {
             union type_u 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;
};

Does someone know how to solve this?

Thank you in advance!

Johnny

Unions are supported since Sip version 6.0.0.

Thank you for the quick integration Riverbank Computing.

Hopefully the manual way to do the translation will not be necessary any more.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM