hdef.h 5.5 KB

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