Skip to content

Best way to provide translation for structures with optional unions #1996

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
pantaryl opened this issue Nov 19, 2019 · 1 comment
Open

Comments

@pantaryl
Copy link

I have a structure that looks like the following:

/// Structure representing a register data pair of offset and value.
typedef struct PktRegDataPair
{
    uint32_t offset; ///< The register offset.
    uint32_t value;  ///< The register value.
} PktRegDataPair;

/// Structure representing different types of register data that can be found within a packet.
typedef struct PktRegData
{
    RegDataType type;     ///< Register data type.
    uint32_t    numRegs;  ///< The number of register represented.

    union
    {
        struct
        {
            uint32_t        regOffset;    ///< Starting register offset.
            const uint32_t* pData;        ///< Pointer to consecutive register values.
        } immMultiData;

        struct
        {
            const PktRegDataPair* pData; ///< Pointer to register pairs of offsets/values.
        } immMultiPairs;

        struct
        {
            uint64_t address;    ///< Address of consecutive register values/pairs of offsets/values.
            uint32_t addrOffset; ///< Offset to an existing address. Valid only if address above is zero.
            struct
            {
                uint32_t regOffset;  ///< Starting register offset.
            } data;
        } indirect;
    };
} PktRegData;

I would like to wrap this in pybind11 in such a way that when the structure is returned to Python, only the valid member variables are present. Similarly, when loaded from Python, I need to translate it into the PktRegData structure.

I'm at a loss as to the best way to do this in pybind11. I've seen documentation for custom type casters and polymorphic type hooks, but am not sure either is best for my use case. For that matter, I'm having a tough time wrapping my head around the implementation for both!

Thank you for your help!

@bstaletic
Copy link
Collaborator

I guess this requires a custom caster. One that resembles the variant_caster.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants