hdef.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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 NABS
  68. #define NABS(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. typedef unsigned char BYTE;
  161. typedef unsigned short WORD;
  162. typedef int BOOL;
  163. typedef void* HANDLE;
  164. #ifndef MAKEWORD
  165. #define MAKEWORD(h, l) ( (((WORD)h) << 8) | (l & 0xff) )
  166. #endif
  167. #ifndef HIBYTE
  168. #define HIBYTE(w) ( (BYTE)(((WORD)w) >> 8) )
  169. #endif
  170. #ifndef LOBYTE
  171. #define LOBYTE(w) ( (BYTE)(w & 0xff) )
  172. #endif
  173. #ifndef MAKELONG
  174. #define MAKELONG(h, l) ( ((int32_t)h) << 16 | (l & 0xffff) )
  175. #endif
  176. #ifndef HIWORD
  177. #define HIWORD(n) ( (WORD)(((int32_t)n) >> 16) )
  178. #endif
  179. #ifndef LOWORD
  180. #define LOWORD(n) ( (WORD)(n & 0xffff) )
  181. #endif
  182. #endif
  183. #ifndef MAKEINT64
  184. #define MAKEINT64(h, l) ( ((int64_t)h) << 32 | (l & 0xffffffff) )
  185. #endif
  186. #ifndef HIINT
  187. #define HIINT(n) ( (int32_t)(((int64_t)n) >> 32) )
  188. #endif
  189. #ifndef LOINT
  190. #define LOINT(n) ( (int32_t)(n & 0xffffffff) )
  191. #endif
  192. #define FLOAT_PRECISION 1e-6
  193. #define FLOAT_EQUAL_ZERO(f) (ABS(f) < FLOAT_PRECISION)
  194. #define STRINGIFY(x) STRINGIFY_HELPER(x)
  195. #define STRINGIFY_HELPER(x) #x
  196. #define STRINGCAT(x, y) STRINGCAT_HELPER(x, y)
  197. #define STRINGCAT_HELPER(x, y) x##y
  198. #ifndef offsetof
  199. #define offsetof(type, member) \
  200. ((size_t)(&((type*)0)->member))
  201. #endif
  202. #ifndef offsetofend
  203. #define offsetofend(type, member) \
  204. (offsetof(type, member) + sizeof(((type*)0)->member))
  205. #endif
  206. #ifndef container_of
  207. #define container_of(ptr, type, member) \
  208. ((type*)((char*)(ptr) - offsetof(type, member)))
  209. #endif
  210. #ifndef prefetch
  211. #ifdef __GNUC__
  212. #define prefetch(x) __builtin_prefetch(x)
  213. #else
  214. #define prefetch(x) (void)0
  215. #endif
  216. #endif
  217. // __cplusplus
  218. #ifdef __cplusplus
  219. #include <string>
  220. #include <map>
  221. typedef std::map<std::string, std::string> keyval_t;
  222. #ifndef BEGIN_NAMESPACE
  223. #define BEGIN_NAMESPACE(ns) namespace ns {
  224. #endif
  225. #ifndef END_NAMESPACE
  226. #define END_NAMESPACE(ns) } // ns
  227. #endif
  228. #ifndef EXTERN_C
  229. #define EXTERN_C extern "C"
  230. #endif
  231. #ifndef BEGIN_EXTERN_C
  232. #define BEGIN_EXTERN_C extern "C" {
  233. #endif
  234. #ifndef END_EXTERN_C
  235. #define END_EXTERN_C } // extern "C"
  236. #endif
  237. #ifndef ENUM
  238. #define ENUM(e) enum e
  239. #endif
  240. #ifndef STRUCT
  241. #define STRUCT(s) struct s
  242. #endif
  243. #ifndef DEFAULT
  244. #define DEFAULT(x) = x
  245. #endif
  246. #else
  247. #define BEGIN_NAMESPACE(ns)
  248. #define END_NAMESPACE(ns)
  249. #define EXTERN_C extern
  250. #define BEGIN_EXTERN_C
  251. #define END_EXTERN_C
  252. #ifndef ENUM
  253. #define ENUM(e)\
  254. typedef enum e e;\
  255. enum e
  256. #endif
  257. #ifndef STRUCT
  258. #define STRUCT(s)\
  259. typedef struct s s;\
  260. struct s
  261. #endif
  262. #ifndef DEFAULT
  263. #define DEFAULT(x)
  264. #endif
  265. #endif // __cplusplus
  266. #endif // HV_DEF_H_