使用c2hs与结构和匿名联合接口


100

如何将这部分C代码编码到.chs文件中,以便c2hs可以将其转换为相对不错的东西?

typedef enum {
    MONOME_BUTTON_UP        = 0x00,
    MONOME_BUTTON_DOWN      = 0x01,
    MONOME_ENCODER_DELTA    = 0x02,
    MONOME_ENCODER_KEY_UP   = 0x03,
    MONOME_ENCODER_KEY_DOWN = 0x04,
    MONOME_TILT             = 0x05,

    /* update this if you add event types */
    MONOME_EVENT_MAX        = 0x06
} monome_event_type_t;

typedef struct monome monome_t; /* opaque data type */
typedef struct monome_event monome_event_t;

typedef void (*monome_event_callback_t)
    (const monome_event_t *event, void *data);

struct monome_event {
    monome_t *monome;
    monome_event_type_t event_type;

    /* __extension__ for anonymous unions in gcc */
    __extension__ union {
        struct {
            unsigned int x;
            unsigned int y;
        } grid;

        struct {
            unsigned int number;
            int delta;
        } encoder;

        struct {
            unsigned int sensor;
            int x;
            int y;
            int z;
        } tilt;
    };
};

12
首先尝试解决问题,然后提出有关语言和/或工具的特定问题,对您来说更有效率。问题是任何混乱的核心,并且避免了特定应用程序不必要的复杂性,甚至更好。例如,您可以询问一个简单的结构和/或一个简单的联合,然后将该知识应用于您的问题。
Thomas M. DuBuisson,2014年

2
@ ThomasM.DuBuisson我认为您的观点很合理。我将对此做进一步的工作。

@unsymbol您有问题的答案吗?请考虑在此处添加它:)
alfakini 2015年

1
您好unsymbol,对此有什么消息吗?你能正常工作吗?
卡斯珀·图勒·汉森

Answers:


1

怎么做:更改代码,以便为成员命名。内存中的布局相同,因此将是二进制兼容的。您将必须为lib的每个版本执行此补丁。

struct monome_event {
    monome_t *monome;
    monome_event_type_t event_type;

    /* __extension__ for anonymous unions in gcc */
    __extension__ union {
        struct me_grid {
            unsigned int x;
            unsigned int y;
        } grid;

        struct me_encoder {
            unsigned int number;
            int delta;
        } encoder;

        struct me_tilt {
            unsigned int sensor;
            int x;
            int y;
            int z;
        } tilt;
    };
};
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.