hdef.h 5.5 KB

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