pybind / pybind/pybind11

Best way to provide translation for structures with optional unions

Open
#1,996 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
18k
Forks
2.3k
Avg merge
5d 17h
Merged PRs (30d)
10

Description

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!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the custom type caster discussion linked in the issue and tests/test_tagbased_polymorphic.cpp to compare the available extension approaches. Define how PktRegData should translate in both directions, including exposing only the valid union members, and verify that the chosen approach supports loading from and returning to Python.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, python
Domain
api, developer-experience
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.