Skip to content

Use C11 _Generic instead of void * in public API #13

Closed
@emersion

Description

@emersion

The public API allows the same functions to be used on different kinds of objects. This makes it pretty error-prone: it's easy to call a function on an invalid type without any compile-time error (either an aml type not accepted by the function in which case it's a runtime error, or a completely foreign type in which case it's just fireworks). C11 _Generic could be used to indicate which types are accepted by a function, e.g:

int _aml_get_fd(const void* obj);

#define aml_get_fd(obj) _Generic(obj, \
    const struct aml*: _aml_get_fd, \
    const struct aml_handler*: _aml_get_fd, \
)(obj)

(Note that in general I am not a fan of functions accepting multiple types of arguments, and would just recommend duplicating the functions into multiple wrappers.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions