c_cpp c中句柄的示例宏

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp c中句柄的示例宏相关的知识,希望对你有一定的参考价值。

#include "stdio.h"
#include "stdlib.h"

#define XenObject_HANDLE unsigned char type;

typedef struct
{
    XenObject_HANDLE
} XenObject;

#ifndef XenObject_TYPE
#define XenObject_TYPE(xo) xo->type
#endif

#define XenObject_SETTYPE(xo, t) xo->type = t 

typedef struct
{
  XenObject_HANDLE
  int num;
} XenInt;

#define XenObject_CAST_INT(xo) (XenInt*)xo
#define XenInt_CAST_OBJ(xoi) (XenObject*)xoi
#define XenInt_GET_INT(xoi) xoi->num
#define XenInt_SET_INT(xoi, i) xoi->num = i

int main(void) {
  printf("Hello World\n");
  XenObject g = {1};
  XenObject *h = &g;
  XenObject_SETTYPE(h, 6);
  XenInt* p = malloc(sizeof(XenInt));
  XenInt_SET_INT(p, 5);
  printf("The type is %d\n", XenObject_TYPE(h));
  printf("The value of the int is %d\n", XenInt_GET_INT(p));
  h = XenInt_CAST_OBJ(p);
  XenObject_SETTYPE(h, 2);
  printf("The type is %d\n", XenObject_TYPE(h));
  return 0;
}

以上是关于c_cpp c中句柄的示例宏的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp CPP的字符串化宏示例

c_cpp C - 错误句柄

c_cpp 在C中使用宏来促进操作

c_cpp C - Zed的调试宏

c_cpp C ++ Setter Getter宏

c_cpp 宏定义