1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| #define COUNT_ARGS(...) COUNT_ARGS_IMPL(__VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0) #define COUNT_ARGS_IMPL(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, N, ...) N
#define OVERLOAD_MACRO(MACRO, ...) \ _Generic((__VA_ARGS__), \ default: MACRO##_1, \ )(__VA_ARGS__)
#define GENERATE_SETTERS(struct_name, ...) \ GENERATE_SETTERS_IMPL(struct_name, COUNT_ARGS(__VA_ARGS__), __VA_ARGS__)
#define GENERATE_SETTERS_IMPL(struct_name, count, ...) \ GENERATE_SETTERS_##count(struct_name, __VA_ARGS__)
#define GENERATE_SETTERS_1(struct_name, field1) \ void struct_name##_set_##field1(struct_name* obj, typeof(obj->field1) value) { \ obj->field1 = value; \ }
#define GENERATE_SETTERS_2(struct_name, field1, field2) \ GENERATE_SETTERS_1(struct_name, field1) \ GENERATE_SETTERS_1(struct_name, field2)
|