Papierkorb / bindgen

Binding and wrapper generator for C/C++ libraries
GNU General Public License v3.0
179 stars 18 forks source link

Add support for C unions #92

Closed HertzDevil closed 3 years ago

HertzDevil commented 3 years ago

C unions are distinct from Crystal Unions for a few reasons:

This patch allows C unions to have their structures copied or their instance property methods generated. Nested structures are also handled properly. After this PR, a compound structure like

typedef struct SDL_GameControllerButtonBind
{
    SDL_GameControllerBindType bindType;

    union
    {
        int button;
        int axis;
        struct {
            int hat;
            int hat_mask;
        } hat;
    } value;

} SDL_GameControllerButtonBind;

will generate

lib Binding
  struct SDLGameControllerButtonBind
    bind_type : SDLGameControllerBindType
    value : SDLGameControllerButtonBind_Unnamed0
  end
  union SDLGameControllerButtonBind_Unnamed0
    button : Int32
    axis : Int32
    hat : SDLGameControllerButtonBind_Unnamed0_Unnamed0
  end
  struct SDLGameControllerButtonBind_Unnamed0_Unnamed0
    hat : Int32
    hat_mask : Int32
  end
end

enum SDLGameControllerBindType
  # ...
end

This PR doesn't change how Crystal wrappers are generated for anonymous types, so if SDL_GameControllerButtonBind is wrapped, the only property methods available would be #bind_type and #bind_type=, as value will be ignored and its members cannot be inlined.

HertzDevil commented 3 years ago

Both done.

Papierkorb commented 3 years ago

LGTM, Thanks! :+1: