Preprocessor data retrieval MACRO
Given the following structure:
struct nmslist_elem_s {
nmptr data;
struct nmslist_elem_s *next;
};
typedef struct nmslist_elem_s nmslist_elem;
Where:
typedef void* nmptr;
Is it possible to write a MACRO that retrieves the data from the element and cast it to the right type:
MACRO(type, element)
that expands to *((type*)element->data)
.
For example for int, I would need something like this: *((int*)(element->data))
.
Later edit: Yes they work, I was 'eating' some "(" and ")". This works:
#define NM开发者_StackOverflow社区SLIST_DATA(type,elem) (*((type*)((elem)->data)))
#define NMSLIST_DATA_REF(type,elem) ((type*)((elem)->data))
#define RETRIEVE(type, element) *((type*)((element)->data))
RETRIEVE(int, nmptr)
// expands to
*((int*)((nmptr)->data))
(untested, but it should work)
精彩评论