hdef.h 5.9 KB

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