Posted on 2013-03-26 15:26
C小加 阅读(3748)
评论(0) 编辑 收藏 引用 所属分类:
网络编程
ACE源码
OS_Memory.h是这样定义的。
# if defined (ACE_HAS_NEW_NOTHROW)
# define ACE_NEW_RETURN(POINTER,CONSTRUCTOR,RET_VAL) \
do { POINTER = new (ACE_nothrow) CONSTRUCTOR; \
if (POINTER == 0) { errno = ENOMEM; return RET_VAL; } \
} while (0)
# define ACE_NEW(POINTER,CONSTRUCTOR) \
do { POINTER = new(ACE_nothrow) CONSTRUCTOR; \
if (POINTER == 0) { errno = ENOMEM; return; } \
} while (0)
# define ACE_NEW_NORETURN(POINTER,CONSTRUCTOR) \
do { POINTER = new(ACE_nothrow) CONSTRUCTOR; \
if (POINTER == 0) { errno = ENOMEM; } \
} while (0)
# else
# define ACE_NEW_RETURN(POINTER,CONSTRUCTOR,RET_VAL) \
do { try { POINTER = new CONSTRUCTOR; } \
catch (ACE_bad_alloc) { ACE_del_bad_alloc errno = ENOMEM; POINTER = 0; return RET_VAL; } \
} while (0)
# define ACE_NEW(POINTER,CONSTRUCTOR) \
do { try { POINTER = new CONSTRUCTOR; } \
catch (ACE_bad_alloc) { ACE_del_bad_alloc errno = ENOMEM; POINTER = 0; return; } \
} while (0)
# define ACE_NEW_NORETURN(POINTER,CONSTRUCTOR) \
do { try { POINTER = new CONSTRUCTOR; } \
catch (ACE_bad_alloc) { ACE_del_bad_alloc errno = ENOMEM; POINTER = 0; } \
} while (0)
# endif /* ACE_HAS_NEW_NOTHROW */
#else /* ACE_NEW_THROWS_EXCEPTIONS */
# define ACE_NEW_RETURN(POINTER,CONSTRUCTOR,RET_VAL) \
do { POINTER = new CONSTRUCTOR; \
if (POINTER == 0) { errno = ENOMEM; return RET_VAL; } \
} while (0)
# define ACE_NEW(POINTER,CONSTRUCTOR) \
do { POINTER = new CONSTRUCTOR; \
if (POINTER == 0) { errno = ENOMEM; return; } \
} while (0)
# define ACE_NEW_NORETURN(POINTER,CONSTRUCTOR) \
do { POINTER = new CONSTRUCTOR; \
if (POINTER == 0) { errno = ENOMEM; } \
} while (0)
以上是用宏定义替换的表达式,功能都是用一个POINTER指针new一个CONSTRUCTOR实例,内部做了一些错误信息的标志和异常处理。
宏ifelse对平台的不同进行了分离,每个平台下都有三种new的方法,他们的区别是return。
每个宏定义都用到了do{}while(0),是因为宏只能替换一句表达式,而你用do{}while(0)可以方便的把多个表达式集中起来。