hdef.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 IN
  53. #define IN
  54. #endif
  55. #ifndef OUT
  56. #define OUT
  57. #endif
  58. #ifndef OPTIONAL
  59. #define OPTIONAL
  60. #endif
  61. #ifndef REQUIRED
  62. #define REQUIRED
  63. #endif
  64. #ifndef REPEATED
  65. #define REPEATED
  66. #endif
  67. #ifndef ABS
  68. #define ABS(n) ((n) < 0 ? -(n) : (n))
  69. #endif
  70. #ifndef MAX
  71. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  72. #endif
  73. #ifndef MIN
  74. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  75. #endif
  76. #ifndef LIMIT
  77. #define LIMIT(lower, v, upper) ((v) < (lower) ? (lower) : (v) > (upper) ? (upper) : (v))
  78. #endif
  79. #ifndef BITSET
  80. #define BITSET(p, n) (*(p) |= (1u << (n)))
  81. #endif
  82. #ifndef BITCLR
  83. #define BITCLR(p, n) (*(p) &= ~(1u << (n)))
  84. #endif
  85. #ifndef BITGET
  86. #define BITGET(i, n) ((i) & (1u << (n)))
  87. #endif
  88. // ASCII:
  89. // [0, 0x20) control-charaters
  90. // [0x20, 0x7F) printable-charaters
  91. //
  92. // 0x0A => LF
  93. // 0x0D => CR
  94. // 0x20 => SPACE
  95. // 0x7F => DEL
  96. //
  97. // [0x09, 0x0D] => \t\n\v\f\r
  98. // [0x30, 0x39] => 0~9
  99. // [0x41, 0x5A] => A~Z
  100. // [0x61, 0x7A] => a~z
  101. #ifndef CR
  102. #define CR '\r'
  103. #endif
  104. #ifndef LF
  105. #define LF '\n'
  106. #endif
  107. #ifndef CRLF
  108. #define CRLF "\r\n"
  109. #endif
  110. #ifndef LOWER
  111. #define LOWER(c) ((c) | 0x20)
  112. #endif
  113. #ifndef UPPER
  114. #define UPPER(c) ((c) & ~0x20)
  115. #endif
  116. #ifndef IS_NUM
  117. #define IS_NUM(c) ((c) >= '0' && (c) <= '9')
  118. #endif
  119. #ifndef IS_UPPER
  120. #define IS_UPPER(c) (((c) >= 'A' && (c) <= 'Z'))
  121. #endif
  122. #ifndef IS_LOWER
  123. #define IS_LOWER(c) (((c) >= 'a' && (c) <= 'z'))
  124. #endif
  125. #ifndef IS_ALPHA
  126. #define IS_ALPHA(c) (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z'))
  127. #endif
  128. #ifndef IS_ALPHANUM
  129. #define IS_ALPHANUM(c) (IS_NUM(c) || IS_ALPHA(c))
  130. #endif
  131. #ifndef IS_HEX
  132. #define IS_HEX(c) (IS_NUM(c) || ((c) >= 'a' && (c) <= 'f') || ((c) >= 'A' && (c) <= 'F'))
  133. #endif
  134. #ifndef IS_CNTRL
  135. #define IS_CNTRL(c) ((c) >= 0 && (c) < 0x20)
  136. #endif
  137. #ifndef IS_GRAPH
  138. #define IS_GRAPH(c) ((c) >= 0x20 && (c) < 0x7F)
  139. #endif
  140. #ifndef ARRAY_SIZE
  141. #define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a)))
  142. #endif
  143. #ifndef SAFE_FREE
  144. #define SAFE_FREE(p) do {if (p) {free(p); (p) = NULL;}} while(0)
  145. #endif
  146. #ifndef SAFE_DELETE
  147. #define SAFE_DELETE(p) do {if (p) {delete (p); (p) = NULL;}} while(0)
  148. #endif
  149. #ifndef SAFE_DELETE_ARRAY
  150. #define SAFE_DELETE_ARRAY(p) do {if (p) {delete[] (p); (p) = NULL;}} while(0)
  151. #endif
  152. #ifndef SAFE_RELEASE
  153. #define SAFE_RELEASE(p) do {if (p) {(p)->release(); (p) = NULL;}} while(0)
  154. #endif
  155. #ifndef MAKE_FOURCC
  156. #define MAKE_FOURCC(a, b, c, d) \
  157. ( ((uint32)d) | ( ((uint32)c) << 8 ) | ( ((uint32)b) << 16 ) | ( ((uint32)a) << 24 ) )
  158. #endif
  159. #ifndef OS_WIN
  160. #ifndef MAKEWORD
  161. #define MAKEWORD(h, l) ( (((WORD)h) << 8) | (l & 0xff) )
  162. #endif
  163. #ifndef HIBYTE
  164. #define HIBYTE(w) ( (BYTE)(((WORD)w) >> 8) )
  165. #endif
  166. #ifndef LOBYTE
  167. #define LOBYTE(w) ( (BYTE)(w & 0xff) )
  168. #endif
  169. #ifndef MAKELONG
  170. #define MAKELONG(h, l) ( ((int32_t)h) << 16 | (l & 0xffff) )
  171. #endif
  172. #ifndef HIWORD
  173. #define HIWORD(n) ( (WORD)(((int32_t)n) >> 16) )
  174. #endif
  175. #ifndef LOWORD
  176. #define LOWORD(n) ( (WORD)(n & 0xffff) )
  177. #endif
  178. #endif
  179. #ifndef MAKEINT64
  180. #define MAKEINT64(h, l) ( ((int64_t)h) << 32 | (l & 0xffffffff) )
  181. #endif
  182. #ifndef HIINT
  183. #define HIINT(n) ( (int32_t)(((int64_t)n) >> 32) )
  184. #endif
  185. #ifndef LOINT
  186. #define LOINT(n) ( (int32_t)(n & 0xffffffff) )
  187. #endif
  188. #define FLOAT_PRECISION 1e-6
  189. #define FLOAT_EQUAL_ZERO(f) (ABS(f) < FLOAT_PRECISION)
  190. #define STRINGIFY(x) STRINGIFY_HELPER(x)
  191. #define STRINGIFY_HELPER(x) #x
  192. #define STRINGCAT(x, y) STRINGCAT_HELPER(x, y)
  193. #define STRINGCAT_HELPER(x, y) x##y
  194. #ifndef offsetof
  195. #define offsetof(type, mmeber) \
  196. ((size_t)(&((type*)0)->member))
  197. #endif
  198. #ifndef offsetofend
  199. #define offsetofend(type, member) \
  200. (offsetof(type, member) + sizeof(((type*)0)->member))
  201. #endif
  202. #ifndef container_of
  203. #define container_of(ptr, type, member) \
  204. ((type*)((char*)(ptr) - offsetof(type, member)))
  205. #endif
  206. #ifndef prefetch
  207. #ifdef __GNUC__
  208. #define prefetch(x) __builtin_prefetch(x)
  209. #else
  210. #define prefetch(x) (void)0
  211. #endif
  212. #endif
  213. // __cplusplus
  214. #ifdef __cplusplus
  215. #include <string>
  216. #include <map>
  217. typedef std::map<std::string, std::string> keyval_t;
  218. #ifndef BEGIN_NAMESPACE
  219. #define BEGIN_NAMESPACE(ns) namespace ns {
  220. #endif
  221. #ifndef END_NAMESPACE
  222. #define END_NAMESPACE(ns) } // ns
  223. #endif
  224. #ifndef EXTERN_C
  225. #define EXTERN_C extern "C"
  226. #endif
  227. #ifndef BEGIN_EXTERN_C
  228. #define BEGIN_EXTERN_C extern "C" {
  229. #endif
  230. #ifndef END_EXTERN_C
  231. #define END_EXTERN_C } // extern "C"
  232. #endif
  233. #ifndef ENUM
  234. #define ENUM(e) enum e
  235. #endif
  236. #ifndef STRUCT
  237. #define STRUCT(s) struct s
  238. #endif
  239. #ifndef DEFAULT
  240. #define DEFAULT(x) = x
  241. #endif
  242. #else
  243. #define BEGIN_NAMESPACE(ns)
  244. #define END_NAMESPACE(ns)
  245. #define EXTERN_C extern
  246. #define BEGIN_EXTERN_C
  247. #define END_EXTERN_C
  248. #ifndef ENUM
  249. #define ENUM(e)\
  250. typedef enum e e;\
  251. enum e
  252. #endif
  253. #ifndef STRUCT
  254. #define STRUCT(s)\
  255. typedef struct s s;\
  256. struct s
  257. #endif
  258. #ifndef DEFAULT
  259. #define DEFAULT(x)
  260. #endif
  261. #endif // __cplusplus
  262. #endif // HW_DEF_H_