plooc_class_strict.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*****************************************************************************
  2. * Copyright(C)2009-2019 by GorgonMeducer<embedded_zhuoran@hotmail.com> *
  3. * *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); * you may
  5. *not use this file except in compliance with the License. * You may
  6. *obtain a copy of the License at *
  7. * *
  8. * http://www.apache.org/licenses/LICENSE-2.0 *
  9. * *
  10. * Unless required by applicable law or agreed to in writing, software *
  11. * distributed under the License is distributed on an "AS IS" BASIS, *
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. ** See the License for the specific language governing permissions and *
  14. * limitations under the License. *
  15. * *
  16. ****************************************************************************/
  17. //#ifndef __PLOOC_CLASS_STRICT_H__ /* deliberately comment this out! */
  18. //#define __PLOOC_CLASS_STRICT_H__ /* deliberately comment this out! */
  19. /******************************************************************************
  20. * HOW TO USE *
  21. ******************************************************************************/
  22. //! Add following content to your module header file, e.g. xxxxxx.h
  23. //#include "plooc_class.h"
  24. /*============================ INCLUDES ======================================*/
  25. //#include <stdint.h>
  26. /*! \NOTE the uint_fast8_t used in this header file is defined in stdint.h
  27. if you don't have stdint.h supported in your toolchain, you should
  28. define uint_fast8_t all by yourself with following rule:
  29. a. if the target processor is 8 bits, define it as uint8_t
  30. b. if the target processor is 16 bits, define it as uint16_t
  31. c. if the target processor is 32 bits, define it as uint32_t
  32. d. if the target processor is 64 bits, define it as either uint32_t or
  33. uint64_t
  34. */
  35. #ifdef __cplusplus
  36. extern "C" {
  37. #endif
  38. /*============================ MACROS ========================================*/
  39. #undef dcl_class
  40. #undef declare_class
  41. #undef def_class
  42. #undef define_class
  43. #undef __def_class2
  44. #undef __def_class3
  45. #undef __def_class4
  46. #undef __def_class5
  47. #undef __def_class6
  48. #undef __def_class7
  49. #undef __def_class8
  50. #undef __def_class
  51. #undef end_def_class
  52. #undef end_define_class
  53. #undef __end_def_class
  54. #undef extern_class
  55. #undef __extern_class
  56. #undef end_extern_class
  57. #undef __end_extern_class
  58. /*============================ MACROFIED FUNCTIONS
  59. * ===========================*/
  60. #if defined(__PLOOC_CLASS_IMPLEMENT__) || defined(__PLOOC_CLASS_IMPLEMENT)
  61. #define __def_class2(__name, _1) \
  62. typedef struct __name __name; \
  63. typedef struct __##__name __##__name; \
  64. struct __##__name { \
  65. __PLOOC_CONNECT2(__PLOOC_PRI_, _1) \
  66. }; \
  67. struct __name { \
  68. __PLOOC_CONNECT2(__PLOOC_EXT_, _1) \
  69. };
  70. #define __def_class3(__name, _1, _2) \
  71. typedef struct __name __name; \
  72. typedef struct __##__name __##__name; \
  73. struct __##__name { \
  74. __PLOOC_CONNECT2(__PLOOC_PRI_, _1) \
  75. __PLOOC_CONNECT2(__PLOOC_PRI_, _2) \
  76. }; \
  77. struct __name { \
  78. __PLOOC_CONNECT2(__PLOOC_EXT_, _1) \
  79. __PLOOC_CONNECT2(__PLOOC_EXT_, _2) \
  80. };
  81. #define __def_class4(__name, _1, _2, _3) \
  82. typedef struct __name __name; \
  83. typedef struct __##__name __##__name; \
  84. struct __##__name { \
  85. __PLOOC_CONNECT2(__PLOOC_PRI_, _1) \
  86. __PLOOC_CONNECT2(__PLOOC_PRI_, _2) \
  87. __PLOOC_CONNECT2(__PLOOC_PRI_, _3) \
  88. }; \
  89. struct __name { \
  90. __PLOOC_CONNECT2(__PLOOC_EXT_, _1) \
  91. __PLOOC_CONNECT2(__PLOOC_EXT_, _2) \
  92. __PLOOC_CONNECT2(__PLOOC_EXT_, _3) \
  93. };
  94. #define __def_class5(__name, _1, _2, _3, _4) \
  95. typedef struct __name __name; \
  96. typedef struct __##__name __##__name; \
  97. struct __##__name { \
  98. __PLOOC_CONNECT2(__PLOOC_PRI_, _1) \
  99. __PLOOC_CONNECT2(__PLOOC_PRI_, _2) \
  100. __PLOOC_CONNECT2(__PLOOC_PRI_, _3) \
  101. __PLOOC_CONNECT2(__PLOOC_PRI_, _4) \
  102. }; \
  103. struct __name { \
  104. __PLOOC_CONNECT2(__PLOOC_EXT_, _1) \
  105. __PLOOC_CONNECT2(__PLOOC_EXT_, _2) \
  106. __PLOOC_CONNECT2(__PLOOC_EXT_, _3) \
  107. __PLOOC_CONNECT2(__PLOOC_EXT_, _4) \
  108. };
  109. #define __def_class6(__name, _1, _2, _3, _4, _5) \
  110. typedef struct __name __name; \
  111. typedef struct __##__name __##__name; \
  112. struct __##__name { \
  113. __PLOOC_CONNECT2(__PLOOC_PRI_, _1) \
  114. __PLOOC_CONNECT2(__PLOOC_PRI_, _2) \
  115. __PLOOC_CONNECT2(__PLOOC_PRI_, _3) \
  116. __PLOOC_CONNECT2(__PLOOC_PRI_, _4) \
  117. __PLOOC_CONNECT2(__PLOOC_PRI_, _5) \
  118. }; \
  119. struct __name { \
  120. __PLOOC_CONNECT2(__PLOOC_EXT_, _1) \
  121. __PLOOC_CONNECT2(__PLOOC_EXT_, _2) \
  122. __PLOOC_CONNECT2(__PLOOC_EXT_, _3) \
  123. __PLOOC_CONNECT2(__PLOOC_EXT_, _4) \
  124. __PLOOC_CONNECT2(__PLOOC_EXT_, _5) \
  125. };
  126. #define __def_class7(__name, _1, _2, _3, _4, _5, _6) \
  127. typedef struct __name __name; \
  128. typedef struct __##__name __##__name; \
  129. struct __##__name { \
  130. __PLOOC_CONNECT2(__PLOOC_PRI_, _1) \
  131. __PLOOC_CONNECT2(__PLOOC_PRI_, _2) \
  132. __PLOOC_CONNECT2(__PLOOC_PRI_, _3) \
  133. __PLOOC_CONNECT2(__PLOOC_PRI_, _4) \
  134. __PLOOC_CONNECT2(__PLOOC_PRI_, _5) \
  135. __PLOOC_CONNECT2(__PLOOC_PRI_, _6) \
  136. }; \
  137. struct __name { \
  138. __PLOOC_CONNECT2(__PLOOC_EXT_, _1) \
  139. __PLOOC_CONNECT2(__PLOOC_EXT_, _2) \
  140. __PLOOC_CONNECT2(__PLOOC_EXT_, _3) \
  141. __PLOOC_CONNECT2(__PLOOC_EXT_, _4) \
  142. __PLOOC_CONNECT2(__PLOOC_EXT_, _5) \
  143. __PLOOC_CONNECT2(__PLOOC_EXT_, _6) \
  144. };
  145. #define __def_class8(__name, _1, _2, _3, _4, _5, _6, _7) \
  146. typedef struct __name __name; \
  147. typedef struct __##__name __##__name; \
  148. struct __##__name { \
  149. __PLOOC_CONNECT2(__PLOOC_PRI_, _1) \
  150. __PLOOC_CONNECT2(__PLOOC_PRI_, _2) \
  151. __PLOOC_CONNECT2(__PLOOC_PRI_, _3) \
  152. __PLOOC_CONNECT2(__PLOOC_PRI_, _4) \
  153. __PLOOC_CONNECT2(__PLOOC_PRI_, _5) \
  154. __PLOOC_CONNECT2(__PLOOC_PRI_, _6) \
  155. __PLOOC_CONNECT2(__PLOOC_PRI_, _7) \
  156. }; \
  157. struct __name { \
  158. __PLOOC_CONNECT2(__PLOOC_EXT_, _1) \
  159. __PLOOC_CONNECT2(__PLOOC_EXT_, _2) \
  160. __PLOOC_CONNECT2(__PLOOC_EXT_, _3) \
  161. __PLOOC_CONNECT2(__PLOOC_EXT_, _4) \
  162. __PLOOC_CONNECT2(__PLOOC_EXT_, _5) \
  163. __PLOOC_CONNECT2(__PLOOC_EXT_, _6) \
  164. __PLOOC_CONNECT2(__PLOOC_EXT_, _7) \
  165. };
  166. #define __end_def_class(...)
  167. #define __extern_class(...)
  168. #define __end_extern_class(...)
  169. #undef __class
  170. #define __class(__name) __##__name
  171. #undef class
  172. #define class(__name) __class(__name)
  173. #undef __with_class
  174. #define __with_class(__type, __src, ...) \
  175. { \
  176. class(__type)* _ = (class(__type)*)(__src); \
  177. PLOOC_UNUSED_PARAM(_); \
  178. __VA_ARGS__; \
  179. } \
  180. for (class(__type)* _ = (class(__type)*)(__src); NULL != _; _ = NULL)
  181. #undef with_class
  182. #define with_class(__type, __src, ...) __with_class(__type, __src, __VA_ARGS__)
  183. #undef __class_internal
  184. #define __class_internal(__src, __des, __type, ...) \
  185. class(__type) * (__des) = (class(__type)*)(__src); \
  186. PLOOC_UNUSED_PARAM(__des); \
  187. __with_class(__type, (__src), __VA_ARGS__)
  188. #undef class_internal
  189. #define class_internal(__src, __des, __type, ...) \
  190. __class_internal(__src, __des, __type, __VA_ARGS__)
  191. #elif defined(__PLOOC_CLASS_INHERIT__) || defined(__PLOOC_CLASS_INHERIT)
  192. #define __def_class2(__name, _1) \
  193. typedef struct __name __name; \
  194. struct __protected_##__name { \
  195. __PLOOC_CONNECT2(__PLOOC_PRO_, _1) \
  196. }; \
  197. struct __name { \
  198. __PLOOC_CONNECT2(__PLOOC_EXT_, _1) \
  199. };
  200. #define __def_class3(__name, _1, _2) \
  201. typedef struct __name __name; \
  202. struct __protected_##__name { \
  203. __PLOOC_CONNECT2(__PLOOC_PRO_, _1) \
  204. __PLOOC_CONNECT2(__PLOOC_PRO_, _2) \
  205. }; \
  206. struct __name { \
  207. __PLOOC_CONNECT2(__PLOOC_EXT_, _1) \
  208. __PLOOC_CONNECT2(__PLOOC_EXT_, _2) \
  209. };
  210. #define __def_class4(__name, _1, _2, _3) \
  211. typedef struct __name __name; \
  212. struct __protected_##__name { \
  213. __PLOOC_CONNECT2(__PLOOC_PRO_, _1) \
  214. __PLOOC_CONNECT2(__PLOOC_PRO_, _2) \
  215. __PLOOC_CONNECT2(__PLOOC_PRO_, _3) \
  216. }; \
  217. struct __name { \
  218. __PLOOC_CONNECT2(__PLOOC_EXT_, _1) \
  219. __PLOOC_CONNECT2(__PLOOC_EXT_, _2) \
  220. __PLOOC_CONNECT2(__PLOOC_EXT_, _3) \
  221. };
  222. #define __def_class5(__name, _1, _2, _3, _4) \
  223. typedef struct __name __name; \
  224. struct __protected_##__name { \
  225. __PLOOC_CONNECT2(__PLOOC_PRO_, _1) \
  226. __PLOOC_CONNECT2(__PLOOC_PRO_, _2) \
  227. __PLOOC_CONNECT2(__PLOOC_PRO_, _3) \
  228. __PLOOC_CONNECT2(__PLOOC_PRO_, _4) \
  229. }; \
  230. struct __name { \
  231. __PLOOC_CONNECT2(__PLOOC_EXT_, _1) \
  232. __PLOOC_CONNECT2(__PLOOC_EXT_, _2) \
  233. __PLOOC_CONNECT2(__PLOOC_EXT_, _3) \
  234. __PLOOC_CONNECT2(__PLOOC_EXT_, _4) \
  235. };
  236. #define __def_class6(__name, _1, _2, _3, _4, _5) \
  237. typedef struct __name __name; \
  238. struct __protected_##__name { \
  239. __PLOOC_CONNECT2(__PLOOC_PRO_, _1) \
  240. __PLOOC_CONNECT2(__PLOOC_PRO_, _2) \
  241. __PLOOC_CONNECT2(__PLOOC_PRO_, _3) \
  242. __PLOOC_CONNECT2(__PLOOC_PRO_, _4) \
  243. __PLOOC_CONNECT2(__PLOOC_PRO_, _5) \
  244. }; \
  245. struct __name { \
  246. __PLOOC_CONNECT2(__PLOOC_EXT_, _1) \
  247. __PLOOC_CONNECT2(__PLOOC_EXT_, _2) \
  248. __PLOOC_CONNECT2(__PLOOC_EXT_, _3) \
  249. __PLOOC_CONNECT2(__PLOOC_EXT_, _4) \
  250. __PLOOC_CONNECT2(__PLOOC_EXT_, _5) \
  251. };
  252. #define __def_class7(__name, _1, _2, _3, _4, _5, _6) \
  253. typedef struct __name __name; \
  254. struct __protected_##__name { \
  255. __PLOOC_CONNECT2(__PLOOC_PRO_, _1) \
  256. __PLOOC_CONNECT2(__PLOOC_PRO_, _2) \
  257. __PLOOC_CONNECT2(__PLOOC_PRO_, _3) \
  258. __PLOOC_CONNECT2(__PLOOC_PRO_, _4) \
  259. __PLOOC_CONNECT2(__PLOOC_PRO_, _5) \
  260. __PLOOC_CONNECT2(__PLOOC_PRO_, _6) \
  261. }; \
  262. struct __name { \
  263. __PLOOC_CONNECT2(__PLOOC_EXT_, _1) \
  264. __PLOOC_CONNECT2(__PLOOC_EXT_, _2) \
  265. __PLOOC_CONNECT2(__PLOOC_EXT_, _3) \
  266. __PLOOC_CONNECT2(__PLOOC_EXT_, _4) \
  267. __PLOOC_CONNECT2(__PLOOC_EXT_, _5) \
  268. __PLOOC_CONNECT2(__PLOOC_EXT_, _6) \
  269. };
  270. #define __def_class8(__name, _1, _2, _3, _4, _5, _6, _7) \
  271. typedef struct __name __name; \
  272. struct __protected_##__name { \
  273. __PLOOC_CONNECT2(__PLOOC_PRO_, _1) \
  274. __PLOOC_CONNECT2(__PLOOC_PRO_, _2) \
  275. __PLOOC_CONNECT2(__PLOOC_PRO_, _3) \
  276. __PLOOC_CONNECT2(__PLOOC_PRO_, _4) \
  277. __PLOOC_CONNECT2(__PLOOC_PRO_, _5) \
  278. __PLOOC_CONNECT2(__PLOOC_PRO_, _6) \
  279. __PLOOC_CONNECT2(__PLOOC_PRO_, _7) \
  280. }; \
  281. struct __name { \
  282. __PLOOC_CONNECT2(__PLOOC_EXT_, _1) \
  283. __PLOOC_CONNECT2(__PLOOC_EXT_, _2) \
  284. __PLOOC_CONNECT2(__PLOOC_EXT_, _3) \
  285. __PLOOC_CONNECT2(__PLOOC_EXT_, _4) \
  286. __PLOOC_CONNECT2(__PLOOC_EXT_, _5) \
  287. __PLOOC_CONNECT2(__PLOOC_EXT_, _6) \
  288. __PLOOC_CONNECT2(__PLOOC_EXT_, _7) \
  289. };
  290. #define __end_def_class(...)
  291. #define __extern_class(...)
  292. #define __end_extern_class(...)
  293. #undef __class_protected
  294. #define __class_protected(__name) struct __protected_##__name
  295. #undef class_protected
  296. #define class_protected(__name) __class_protected(__name)
  297. #undef __with_protected
  298. #define __with_protected(__type, __src, ...) \
  299. { \
  300. class_protected(__type)* _ = (class_protected(__type)*)(__src); \
  301. PLOOC_UNUSED_PARAM(_); \
  302. __VA_ARGS__; \
  303. } \
  304. for (class_protected(__type)* _ = (class_protected(__type)*)(__src); \
  305. NULL != _; _ = NULL)
  306. #undef with_protected
  307. #define with_protected(__type, __src, ...) \
  308. __with_protected(__type, __src, __VA_ARGS__)
  309. #undef __protected_internal
  310. #define __protected_internal(__src, __des, __type, ...) \
  311. class_protected(__type) * (__des) = (class_protected(__type)*)(__src); \
  312. PLOOC_UNUSED_PARAM(__des); \
  313. __with_protected(__type, __src, __VA_ARGS__)
  314. #undef protected_internal
  315. #define protected_internal(__src, __des, __type, ...) \
  316. __protected_internal(__src, __des, __type, __VA_ARGS__)
  317. #else /* __PLOOC_CLASS_EXTERN */
  318. #define __def_class2(__name, _1) \
  319. typedef struct __name __name; \
  320. struct __name { \
  321. __PLOOC_CONNECT2(__PLOOC_EXT_, _1) \
  322. };
  323. #define __def_class3(__name, _1, _2) \
  324. typedef struct __name __name; \
  325. struct __name { \
  326. __PLOOC_CONNECT2(__PLOOC_EXT_, _1) \
  327. __PLOOC_CONNECT2(__PLOOC_EXT_, _2) \
  328. };
  329. #define __def_class4(__name, _1, _2, _3) \
  330. typedef struct __name __name; \
  331. struct __name { \
  332. __PLOOC_CONNECT2(__PLOOC_EXT_, _1) \
  333. __PLOOC_CONNECT2(__PLOOC_EXT_, _2) \
  334. __PLOOC_CONNECT2(__PLOOC_EXT_, _3) \
  335. };
  336. #define __def_class5(__name, _1, _2, _3, _4) \
  337. typedef struct __name __name; \
  338. struct __name { \
  339. __PLOOC_CONNECT2(__PLOOC_EXT_, _1) \
  340. __PLOOC_CONNECT2(__PLOOC_EXT_, _2) \
  341. __PLOOC_CONNECT2(__PLOOC_EXT_, _3) \
  342. __PLOOC_CONNECT2(__PLOOC_EXT_, _4) \
  343. };
  344. #define __def_class6(__name, _1, _2, _3, _4, _5) \
  345. typedef struct __name __name; \
  346. struct __name { \
  347. __PLOOC_CONNECT2(__PLOOC_EXT_, _1) \
  348. __PLOOC_CONNECT2(__PLOOC_EXT_, _2) \
  349. __PLOOC_CONNECT2(__PLOOC_EXT_, _3) \
  350. __PLOOC_CONNECT2(__PLOOC_EXT_, _4) \
  351. __PLOOC_CONNECT2(__PLOOC_EXT_, _5) \
  352. };
  353. #define __def_class7(__name, _1, _2, _3, _4, _5, _6) \
  354. typedef struct __name __name; \
  355. struct __name { \
  356. __PLOOC_CONNECT2(__PLOOC_EXT_, _1) \
  357. __PLOOC_CONNECT2(__PLOOC_EXT_, _2) \
  358. __PLOOC_CONNECT2(__PLOOC_EXT_, _3) \
  359. __PLOOC_CONNECT2(__PLOOC_EXT_, _4) \
  360. __PLOOC_CONNECT2(__PLOOC_EXT_, _5) \
  361. __PLOOC_CONNECT2(__PLOOC_EXT_, _6) \
  362. };
  363. #define __def_class8(__name, _1, _2, _3, _4, _5, _6, _7) \
  364. typedef struct __name __name; \
  365. struct __name { \
  366. __PLOOC_CONNECT2(__PLOOC_EXT_, _1) \
  367. __PLOOC_CONNECT2(__PLOOC_EXT_, _2) \
  368. __PLOOC_CONNECT2(__PLOOC_EXT_, _3) \
  369. __PLOOC_CONNECT2(__PLOOC_EXT_, _4) \
  370. __PLOOC_CONNECT2(__PLOOC_EXT_, _5) \
  371. __PLOOC_CONNECT2(__PLOOC_EXT_, _6) \
  372. __PLOOC_CONNECT2(__PLOOC_EXT_, _7) \
  373. };
  374. #define __end_def_class(...)
  375. #define __extern_class(...) \
  376. __PLOOC_EVAL(__def_class, __VA_ARGS__) \
  377. (__VA_ARGS__)
  378. #define __end_extern_class(...)
  379. #endif
  380. #define def_class(...) \
  381. __PLOOC_EVAL(__def_class, __VA_ARGS__) \
  382. (__VA_ARGS__)
  383. #define define_class(...) def_class(__VA_ARGS__)
  384. #define end_def_class(...) __end_def_class(__VA_ARGS__)
  385. #define end_define_class(...) end_def_class(__VA_ARGS__)
  386. #define declare_class(__name) typedef struct __name __name;
  387. #define dcl_class(__name) declare_class(__name)
  388. #define extern_class(...) __extern_class(__VA_ARGS__)
  389. #define end_extern_class(...) __end_extern_class(__VA_ARGS__)
  390. #undef __PLOOC_CLASS_IMPLEMENT__
  391. #undef __PLOOC_CLASS_INHERIT__
  392. #undef __PLOOC_CLASS_IMPLEMENT
  393. #undef __PLOOC_CLASS_INHERIT
  394. /*============================ TYPES
  395. * =========================================*/
  396. /*============================ GLOBAL VARIABLES
  397. * ==============================*/
  398. /*============================ LOCAL VARIABLES
  399. * ===============================*/
  400. /*============================ PROTOTYPES
  401. * ====================================*/
  402. //#endif /* deliberately comment this out! */
  403. #ifdef __cplusplus
  404. }
  405. #endif