hdef.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. #ifndef HW_DEF_H_
  2. #define HW_DEF_H_
  3. #include "hplatform.h"
  4. #include "hbase.h"
  5. typedef float float32;
  6. typedef double float64;
  7. typedef unsigned char BYTE;
  8. typedef unsigned short WORD;
  9. typedef int BOOL;
  10. typedef void* handle;
  11. typedef union {
  12. bool b;
  13. char ch;
  14. char* str;
  15. long long num;
  16. float f;
  17. double lf;
  18. void* ptr;
  19. } var;
  20. #ifdef _MSC_VER
  21. typedef int pid_t;
  22. typedef int gid_t;
  23. typedef int uid_t;
  24. #endif
  25. typedef int (*method_t)(void* userdata);
  26. typedef void (*procedure_t)(void* userdata);
  27. #ifndef MAX_PATH
  28. #define MAX_PATH 260
  29. #endif
  30. #ifndef NULL
  31. #define NULL 0
  32. #endif
  33. #ifndef TRUE
  34. #define TRUE 1
  35. #endif
  36. #ifndef FALSE
  37. #define FALSE 0
  38. #endif
  39. #ifndef INFINITE
  40. #define INFINITE (uint32_t)-1
  41. #endif
  42. #ifndef CR
  43. #define CR '\r'
  44. #endif
  45. #ifndef LF
  46. #define LF '\n'
  47. #endif
  48. #ifndef CRLF
  49. #define CRLF "\r\n"
  50. #endif
  51. #ifndef IN
  52. #define IN
  53. #endif
  54. #ifndef OUT
  55. #define OUT
  56. #endif
  57. #ifndef OPTIONAL
  58. #define OPTIONAL
  59. #endif
  60. #ifndef REQUIRED
  61. #define REQUIRED
  62. #endif
  63. #ifndef REPEATED
  64. #define REPEATED
  65. #endif
  66. #ifndef ABS
  67. #define ABS(n) ((n) < 0 ? -(n) : (n))
  68. #endif
  69. #ifndef MAX
  70. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  71. #endif
  72. #ifndef MIN
  73. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  74. #endif
  75. #ifndef LIMIT
  76. #define LIMIT(lower, v, upper) ((v) < (lower) ? (lower) : (v) > (upper) ? (upper) : (v))
  77. #endif
  78. #ifndef BITSET
  79. #define BITSET(p, n) (*(p) |= (1u << (n)))
  80. #endif
  81. #ifndef BITCLEAR
  82. #define BITCLEAR(p, n) (*(p) &= ~(1u << (n)))
  83. #endif
  84. #ifndef BITGET
  85. #define BITGET(i, n) ((i) & (1u << (n)))
  86. #endif
  87. #ifndef LOWER
  88. #define LOWER(c) ((c) | 0x20)
  89. #endif
  90. #ifndef UPPER
  91. #define UPPER(c) ((c) & ~0x20)
  92. #endif
  93. #ifndef IS_NUM
  94. #define IS_NUM(c) ((c) >= '0' && (c) <= '9')
  95. #endif
  96. #ifndef IS_ALPHA
  97. #define IS_ALPHA(c) (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'F'))
  98. #endif
  99. #ifndef IS_ALPHANUM
  100. #define IS_ALPHANUM(c) (IS_NUM(c) || IS_ALPHA(c))
  101. #endif
  102. #ifndef IS_HEX
  103. #define IS_HEX(c) (IS_NUM(c) || ((c) >= 'a' && (c) <= 'f') || ((c) >= 'A' && (c) <= 'F'))
  104. #endif
  105. #ifndef ARRAY_SIZE
  106. #define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a)))
  107. #endif
  108. #ifndef SAFE_FREE
  109. #define SAFE_FREE(p) do {if (p) {free(p); (p) = NULL;}} while(0)
  110. #endif
  111. #ifndef SAFE_DELETE
  112. #define SAFE_DELETE(p) do {if (p) {delete (p); (p) = NULL;}} while(0)
  113. #endif
  114. #ifndef SAFE_DELETE_ARRAY
  115. #define SAFE_DELETE_ARRAY(p) do {if (p) {delete[] (p); (p) = NULL;}} while(0)
  116. #endif
  117. #ifndef SAFE_RELEASE
  118. #define SAFE_RELEASE(p) do {if (p) {(p)->release(); (p) = NULL;}} while(0)
  119. #endif
  120. #ifndef MAKE_FOURCC
  121. #define MAKE_FOURCC(a, b, c, d) \
  122. ( ((uint32)d) | ( ((uint32)c) << 8 ) | ( ((uint32)b) << 16 ) | ( ((uint32)a) << 24 ) )
  123. #endif
  124. #ifndef OS_WIN
  125. #ifndef MAKEWORD
  126. #define MAKEWORD(h, l) ( (((WORD)h) << 8) | (l & 0xff) )
  127. #endif
  128. #ifndef HIBYTE
  129. #define HIBYTE(w) ( (BYTE)(((WORD)w) >> 8) )
  130. #endif
  131. #ifndef LOBYTE
  132. #define LOBYTE(w) ( (BYTE)(w & 0xff) )
  133. #endif
  134. #ifndef MAKELONG
  135. #define MAKELONG(h, l) ( ((int32_t)h) << 16 | (l & 0xffff) )
  136. #endif
  137. #ifndef HIWORD
  138. #define HIWORD(n) ( (WORD)(((int32_t)n) >> 16) )
  139. #endif
  140. #ifndef LOWORD
  141. #define LOWORD(n) ( (WORD)(n & 0xffff) )
  142. #endif
  143. #endif
  144. #ifndef MAKEINT64
  145. #define MAKEINT64(h, l) ( ((int64_t)h) << 32 | (l & 0xffffffff) )
  146. #endif
  147. #ifndef HIINT
  148. #define HIINT(n) ( (int32_t)(((int64_t)n) >> 32) )
  149. #endif
  150. #ifndef LOINT
  151. #define LOINT(n) ( (int32_t)(n & 0xffffffff) )
  152. #endif
  153. #define FLOAT_PRECISION 1e-6
  154. #define FLOAT_EQUAL_ZERO(f) (ABS(f) < FLOAT_PRECISION)
  155. #define STRINGIFY(x) STRINGIFY_HELPER(x)
  156. #define STRINGIFY_HELPER(x) #x
  157. #define STRINGCAT(x, y) STRINGCAT_HELPER(x, y)
  158. #define STRINGCAT_HELPER(x, y) x##y
  159. #ifndef offsetof
  160. #define offsetof(type, mmeber) \
  161. ((size_t)(&((type*)0)->member))
  162. #endif
  163. #ifndef offsetofend
  164. #define offsetofend(type, member) \
  165. (offsetof(type, member) + sizeof(((type*)0)->member))
  166. #endif
  167. #ifndef container_of
  168. #define container_of(ptr, type, member) \
  169. ((type*)((char*)(ptr) - offsetof(type, member)))
  170. #endif
  171. #ifndef prefetch
  172. #ifdef __GNUC__
  173. #define prefetch(x) __builtin_prefetch(x)
  174. #else
  175. #define prefetch(x) (void)0
  176. #endif
  177. #endif
  178. // __cplusplus
  179. #ifdef __cplusplus
  180. #include <string>
  181. #include <map>
  182. typedef std::map<std::string, std::string> keyval_t;
  183. #ifndef BEGIN_NAMESPACE
  184. #define BEGIN_NAMESPACE(ns) namespace ns {
  185. #endif
  186. #ifndef END_NAMESPACE
  187. #define END_NAMESPACE(ns) } // ns
  188. #endif
  189. #ifndef EXTERN_C
  190. #define EXTERN_C extern "C"
  191. #endif
  192. #ifndef BEGIN_EXTERN_C
  193. #define BEGIN_EXTERN_C extern "C" {
  194. #endif
  195. #ifndef END_EXTERN_C
  196. #define END_EXTERN_C } // extern "C"
  197. #endif
  198. #ifndef ENUM
  199. #define ENUM(e) enum e
  200. #endif
  201. #ifndef STRUCT
  202. #define STRUCT(s) struct s
  203. #endif
  204. #ifndef DEFAULT
  205. #define DEFAULT(x) = x
  206. #endif
  207. #else
  208. #define BEGIN_NAMESPACE(ns)
  209. #define END_NAMESPACE(ns)
  210. #define EXTERN_C extern
  211. #define BEGIN_EXTERN_C
  212. #define END_EXTERN_C
  213. #ifndef ENUM
  214. #define ENUM(e)\
  215. typedef enum e e;\
  216. enum e
  217. #endif
  218. #ifndef STRUCT
  219. #define STRUCT(s)\
  220. typedef struct s s;\
  221. struct s
  222. #endif
  223. #ifndef DEFAULT
  224. #define DEFAULT(x)
  225. #endif
  226. #endif // __cplusplus
  227. #endif // HW_DEF_H_