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 26 27 28 29 30
|
/* Some way to signal the real type of an argument. Such a type is analogous to
printf's format string -- that's one way to signal type too. */
/* User-defined structures will need to be handled through a proxy function */
typedef enum TypeLabel { Char_T, SChar_T, UChar_T, Int_T, UInt_T, /* ... */ } TypeLabel;
void increment(TypeLabel c, void* x) {
# if ! defined BRANCH
# define BRANCH(label, type) do { \
/* Manipulate x here */ \
case label: *((type*)x) = (*(type*)x) + 1; return; \
} while(0)
switch (c) {
BRANCH(Char_T , char);
BRANCH(SChar_T, signed char);
BRANCH(UChar_T, unsigned char);
BRANCH(Int_T , int);
BRANCH(UInt_T , unsigned int);
/* ... */
}
# else
# error "BRANCH already defined"
# endif
}
# include <stdio.h>
int main(void) {
int bar = 3;
increment(Int_T, &bar);
printf("%d\n", bar);
}
|