qse/ase/cmn/macros.h

140 lines
4.6 KiB
C
Raw Normal View History

2004-12-18 09:16:31 +00:00
/*
2007-03-06 14:58:00 +00:00
* $Id: macros.h,v 1.3 2007-03-06 14:54:49 bacon Exp $
2007-02-03 10:52:36 +00:00
*
* {License}
2004-12-18 09:16:31 +00:00
*/
2006-10-24 04:31:07 +00:00
#ifndef _ASE_MACROS_H_
#define _ASE_MACROS_H_
2004-12-18 09:16:31 +00:00
2007-03-06 14:16:53 +00:00
#include <ase/cmn/types.h>
2004-12-18 09:16:31 +00:00
2005-03-29 07:29:13 +00:00
#ifdef __cplusplus
2006-10-24 04:31:07 +00:00
/*#define ASE_NULL ((ase_uint_t)0)*/
#define ASE_NULL (0)
2005-03-29 07:29:13 +00:00
#else
2006-10-24 04:31:07 +00:00
#define ASE_NULL ((void*)0)
2005-03-29 07:29:13 +00:00
#endif
2006-10-24 04:31:07 +00:00
#define ASE_CHAR_EOF ((ase_cint_t)-1)
2004-12-18 09:16:31 +00:00
2006-11-29 02:54:17 +00:00
#define ASE_SIZEOF(n) (sizeof(n))
#define ASE_COUNTOF(n) (sizeof(n)/sizeof(n[0]))
2006-11-29 02:39:10 +00:00
#define ASE_OFFSETOF(type,member) ((ase_size_t)&((type*)0)->member)
2005-04-18 11:28:07 +00:00
2006-10-24 04:31:07 +00:00
#define ASE_TYPE_IS_SIGNED(type) (((type)0) > ((type)-1))
#define ASE_TYPE_IS_UNSIGNED(type) (((type)0) < ((type)-1))
2007-02-07 14:08:07 +00:00
#define ASE_TYPE_SIGNED_MAX(type) \
((type)~((type)1 << (ASE_SIZEOF(type) * 8 - 1)))
#define ASE_TYPE_UNSIGNED_MAX(type) ((type)(~(type)0))
#define ASE_TYPE_SIGNED_MIN(type) \
((type)((type)1 << (ASE_SIZEOF(type) * 8 - 1)))
#define ASE_TYPE_UNSIGNED_MIN(type) ((type)0)
2006-10-24 04:31:07 +00:00
#define ASE_TYPE_MAX(type) \
2007-02-07 14:08:07 +00:00
((ASE_TYPE_IS_SIGNED(type)? ASE_TYPE_SIGNED_MAX(type): ASE_TYPE_UNSIGNED_MAX(type)))
2006-10-24 04:31:07 +00:00
#define ASE_TYPE_MIN(type) \
2007-02-07 14:08:07 +00:00
((ASE_TYPE_IS_SIGNED(type)? ASE_TYPE_SIGNED_MIN(type): ASE_TYPE_UNSIGNED_MIN(type)))
2005-03-30 07:26:38 +00:00
2006-11-29 02:54:17 +00:00
#define ASE_IS_POWOF2(x) (((x) & ((x) - 1)) == 0)
2007-02-07 14:08:07 +00:00
2006-10-24 04:31:07 +00:00
#define ASE_SWAP(x,y,original_type,casting_type) \
2005-07-30 05:11:39 +00:00
do { \
x = (original_type)((casting_type)(x) ^ (casting_type)(y)); \
y = (original_type)((casting_type)(y) ^ (casting_type)(x)); \
x = (original_type)((casting_type)(x) ^ (casting_type)(y)); \
} while (0)
2007-02-07 14:08:07 +00:00
2006-10-24 04:31:07 +00:00
#define ASE_ABS(x) ((x) < 0? -(x): (x))
2005-03-30 10:48:17 +00:00
2006-10-24 04:31:07 +00:00
#define ASE_LOOP_CONTINUE(id) goto __loop_ ## id ## _begin__;
#define ASE_LOOP_BREAK(id) goto __loop_ ## id ## _end__;
#define ASE_LOOP_BEGIN(id) __loop_ ## id ## _begin__: {
#define ASE_LOOP_END(id) ASE_LOOP_CONTINUE(id) } __loop_ ## id ## _end__:;
2004-12-18 09:16:31 +00:00
2006-10-24 04:31:07 +00:00
#define ASE_REPEAT(n,blk) \
2005-04-02 13:14:33 +00:00
do { \
2007-03-06 14:29:27 +00:00
ase_size_t __ase_repeat_x1__ = (ase_size_t)(n); \
ase_size_t __ase_repeat_x2__ = __ase_repeat_x1__ >> 4; \
__ase_repeat_x1__ &= 15; \
while (__ase_repeat_x1__-- > 0) { blk; } \
while (__ase_repeat_x2__-- > 0) { \
2005-04-02 14:28:18 +00:00
blk; blk; blk; blk; blk; blk; blk; blk; \
blk; blk; blk; blk; blk; blk; blk; blk; \
} \
2005-04-02 13:14:33 +00:00
} while (0);
2006-11-29 03:02:07 +00:00
#define ASE_MQ_I(val) #val
#define ASE_MQ(val) ASE_MQ_I(val)
2006-10-24 04:31:07 +00:00
#define ASE_MC(ch) ((ase_mchar_t)ch)
#define ASE_MS(str) ((const ase_mchar_t*)str)
#define ASE_MT(txt) (txt)
2006-05-06 12:52:36 +00:00
2006-11-29 03:02:07 +00:00
#define ASE_WQ_I(val) (L ## #val)
#define ASE_WQ(val) ASE_WQ_I(val)
2006-11-28 11:34:34 +00:00
#define ASE_WC(ch) ((ase_wchar_t)L ## ch)
#define ASE_WS(str) ((const ase_wchar_t*)L ## str)
#define ASE_WT(txt) (L ## txt)
2006-05-06 12:52:36 +00:00
2006-10-24 04:31:07 +00:00
#if defined(ASE_CHAR_IS_MCHAR)
#define ASE_C(ch) ASE_MC(ch)
#define ASE_S(str) ASE_MS(str)
#define ASE_T(txt) ASE_MT(txt)
2005-01-06 15:31:59 +00:00
#else
2006-10-24 04:31:07 +00:00
#define ASE_C(ch) ASE_WC(ch)
#define ASE_S(str) ASE_WS(str)
#define ASE_T(txt) ASE_WT(txt)
2005-01-06 15:31:59 +00:00
#endif
2004-12-18 09:16:31 +00:00
2007-02-18 15:09:19 +00:00
#if defined(__GNUC__)
#define ASE_BEGIN_PACKED_STRUCT(x) struct x {
2007-02-18 15:51:07 +00:00
#define ASE_END_PACKED_STRUCT() } __attribute__((packed));
2007-02-18 15:09:19 +00:00
#else
2007-02-18 15:51:07 +00:00
#define ASE_BEGIN_PACKED_STRUCT(x) struct x {
#define ASE_END_PACKED_STRUCT() };
2007-02-18 15:09:19 +00:00
#endif
2007-03-06 14:29:27 +00:00
#ifdef NDEBUG
2007-03-06 14:58:00 +00:00
#define ASE_ASSERT(expr) ((void)0)
#define ASE_ASSERTX(expr,desc) ((void)0)
2007-03-06 14:29:27 +00:00
#else
#ifdef __cplusplus
extern "C" {
#endif
void ase_assert_abort (void);
void ase_assert_printf (const ase_char_t* fmt, ...);
int ase_assert_failed (
const ase_char_t* expr, const ase_char_t* desc,
const ase_char_t* file, ase_size_t line);
#ifdef __cplusplus
}
#endif
2007-03-06 14:58:00 +00:00
#define ASE_ASSERT(expr) (void)((expr) || \
2007-03-06 14:29:27 +00:00
(ase_assert_failed (ASE_T(#expr), ASE_NULL, ASE_T(__FILE__), __LINE__), 0))
2007-03-06 14:58:00 +00:00
#define ASE_ASSERTX(expr,desc) (void)((expr) || \
2007-03-06 14:29:27 +00:00
(ase_assert_failed (ASE_T(#expr), ASE_T(desc), ASE_T(__FILE__), __LINE__), 0))
#endif
2007-02-24 14:32:44 +00:00
#define ASE_MALLOC(mmgr,size) (mmgr)->malloc((mmgr)->custom_data, size)
#define ASE_REALLOC(mmgr,ptr,size) (mmgr)->realloc((mmgr)->custom_data, ptr, size)
#define ASE_FREE(mmgr,ptr) (mmgr)->free((mmgr)->custom_data, ptr)
#define ASE_ISUPPER(ccls,c) (ccls)->is_upper((ccls)->custom_data,c)
#define ASE_ISLOWER(ccls,c) (ccls)->is_lower((ccls)->custom_data,c)
#define ASE_ISALPHA(ccls,c) (ccls)->is_alpha((ccls)->custom_data,c)
#define ASE_ISDIGIT(ccls,c) (ccls)->is_digit((ccls)->custom_data,c)
#define ASE_ISXDIGIT(ccls,c) (ccls)->is_xdigit((ccls)->custom_data,c)
#define ASE_ISALNUM(ccls,c) (ccls)->is_alnum((ccls)->custom_data,c)
#define ASE_ISSPACE(ccls,c) (ccls)->is_space((ccls)->custom_data,c)
#define ASE_ISPRINT(ccls,c) (ccls)->is_print((ccls)->custom_data,c)
#define ASE_ISGRAPH(ccls,c) (ccls)->is_graph((ccls)->custom_data,c)
#define ASE_ISCNTRL(ccls,c) (ccls)->is_cntrl((ccls)->custom_data,c)
#define ASE_ISPUNCT(ccls,c) (ccls)->is_punct((ccls)->custom_data,c)
#define ASE_TOUPPER(ccls,c) (ccls)->to_upper((ccls)->custom_data,c)
#define ASE_TOLOWER(ccls,c) (ccls)->to_lower((ccls)->custom_data,c)
2007-02-22 14:46:43 +00:00
2004-12-18 09:16:31 +00:00
#endif