plooc_class_simple.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*****************************************************************************
  2. * Copyright(C)2009-2019 by GorgonMeducer<embedded_zhuoran@hotmail.com> *
  3. * and SimonQian<simonqian@simonqian.com> * with
  4. *support from HenryLong<henry_long@163.com> *
  5. * *
  6. * Licensed under the Apache License, Version 2.0 (the "License"); * you may
  7. *not use this file except in compliance with the License. * You may
  8. *obtain a copy of the License at *
  9. * *
  10. * http://www.apache.org/licenses/LICENSE-2.0 *
  11. * *
  12. * Unless required by applicable law or agreed to in writing, software *
  13. * distributed under the License is distributed on an "AS IS" BASIS, *
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. ** See the License for the specific language governing permissions and *
  16. * limitations under the License. *
  17. * *
  18. ****************************************************************************/
  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 private_member
  40. #undef protected_member
  41. #undef public_member
  42. /*============================ MACROFIED FUNCTIONS
  43. * ===========================*/
  44. #ifndef __PLOOC_CLASS_SIMPLE_H__
  45. #define __PLOOC_CLASS_SIMPLE_H__
  46. #define __def_simple_class(__name) struct __name
  47. #define def_simple_class(__name) __def_simple_class(__name)
  48. #define define_simple_class(__name) def_simple_class(__name)
  49. #define declare_simple_class(__name) typedef struct __name __name;
  50. #define dcl_simple_class(__name) declare_simple_class(__name)
  51. #endif /* __PLOOC_CLASS_SIMPLE_H__ */
  52. #if defined(__OOC_DEBUG__)
  53. #define private_member(...) PLOOC_VISIBLE(__VA_ARGS__)
  54. #define protected_member(...) PLOOC_VISIBLE(__VA_ARGS__)
  55. #define public_member(...) PLOOC_VISIBLE(__VA_ARGS__)
  56. #elif defined(__PLOOC_CLASS_IMPLEMENT__) || defined(__PLOOC_CLASS_IMPLEMENT)
  57. #define private_member(...) PLOOC_VISIBLE(__VA_ARGS__)
  58. #define protected_member(...) PLOOC_VISIBLE(__VA_ARGS__)
  59. #define public_member(...) PLOOC_VISIBLE(__VA_ARGS__)
  60. #elif defined(__PLOOC_CLASS_INHERIT__) || defined(__PLOOC_CLASS_INHERIT)
  61. #define private_member(...) PLOOC_INVISIBLE(__VA_ARGS__)
  62. #define protected_member(...) PLOOC_VISIBLE(__VA_ARGS__)
  63. #define public_member(...) PLOOC_VISIBLE(__VA_ARGS__)
  64. #else /* __PLOOC_CLASS_EXTERN */
  65. #define private_member(...) PLOOC_INVISIBLE(__VA_ARGS__)
  66. #define protected_member(...) PLOOC_INVISIBLE(__VA_ARGS__)
  67. #define public_member(...) PLOOC_VISIBLE(__VA_ARGS__)
  68. #endif
  69. // code below is just try to be compatible with plooc_class_strict
  70. #undef declare_class
  71. #undef dcl_class
  72. #undef def_class
  73. #undef define_class
  74. #undef __def_class
  75. #undef end_def_class
  76. #undef end_define_class
  77. #undef __end_def_class
  78. #undef extern_class
  79. #undef __extern_class
  80. #undef end_extern_class
  81. #undef __end_extern_class
  82. #define __end_def_class(...)
  83. #define __def_class(__name, ...) \
  84. typedef struct __name __name; \
  85. struct __name { \
  86. __VA_ARGS__ \
  87. };
  88. #if defined(__PLOOC_CLASS_IMPLEMENT__) || defined(__PLOOC_CLASS_IMPLEMENT)
  89. #undef __class
  90. #define __class(__name) __name
  91. #undef class
  92. #define class(__name) __class(__name)
  93. #undef __with_class
  94. #define __with_class(__type, __src, ...) \
  95. { \
  96. class(__type)* _ = (class(__type)*)(__src); \
  97. PLOOC_UNUSED_PARAM(_); \
  98. __VA_ARGS__; \
  99. } \
  100. for (class(__type)* _ = (class(__type)*)(__src); NULL != _; _ = NULL)
  101. #undef with_class
  102. #define with_class(__type, __src, ...) __with_class(__type, __src, __VA_ARGS__)
  103. #undef __class_internal
  104. #define __class_internal(__src, __des, __type, ...) \
  105. class(__type) * (__des) = (class(__type)*)(__src); \
  106. PLOOC_UNUSED_PARAM(__des); \
  107. __with_class(__type, (__src), __VA_ARGS__)
  108. #undef class_internal
  109. #define class_internal(__src, __des, __type, ...) \
  110. __class_internal(__src, __des, __type, __VA_ARGS__)
  111. #define __extern_class(...)
  112. #define __end_extern_class(...)
  113. #elif defined(__PLOOC_CLASS_INHERIT__) || defined(__PLOOC_CLASS_INHERIT)
  114. #undef __class_protected
  115. #define __class_protected(__name) __name
  116. #undef class_protected
  117. #define class_protected(__name) __class_protected(__name)
  118. #undef __with_protected
  119. #define __with_protected(__type, __src, ...) \
  120. { \
  121. class_protected(__type)* _ = (class_protected(__type)*)(__src); \
  122. PLOOC_UNUSED_PARAM(_); \
  123. __VA_ARGS__; \
  124. }
  125. #undef with_protected
  126. #define with_protected(__type, __src, ...) \
  127. __with_protected(__type, __src, __VA_ARGS__)
  128. #undef __protected_internal
  129. #define __protected_internal(__src, __des, __type, ...) \
  130. class_protected(__type) * (__des) = (class_protected(__type)*)(__src); \
  131. PLOOC_UNUSED_PARAM(__des); \
  132. __with_protected(__type, __src, __VA_ARGS__)
  133. #undef protected_internal
  134. #define protected_internal(__src, __des, __type, ...) \
  135. __protected_internal(__src, __des, __type, __VA_ARGS__)
  136. #define __extern_class(...)
  137. #define __end_extern_class(...)
  138. #else /* __PLOOC_CLASS_EXTERN */
  139. #define __extern_class(...) __def_class(__VA_ARGS__)
  140. #define __end_extern_class(...)
  141. #endif
  142. #undef which
  143. #define which(...) PLOOC_VISIBLE(__VA_ARGS__)
  144. #define def_class(__name, ...) __def_class(__name, __VA_ARGS__)
  145. #define define_class(__name, ...) def_class(__name, __VA_ARGS__)
  146. #define end_def_class(...) __end_def_class(__VA_ARGS__)
  147. #define end_define_class(...) end_def_class(__VA_ARGS__)
  148. #define dcl_class(__name) typedef struct __name __name;
  149. #define declare_class(__name) typedef struct __name __name;
  150. #define extern_class(__name, ...) __extern_class(__name, __VA_ARGS__)
  151. #define end_extern_class(__name, ...) __end_extern_class(__name, __VA_ARGS__)
  152. #undef __PLOOC_CLASS_IMPLEMENT__
  153. #undef __PLOOC_CLASS_INHERIT__
  154. #undef __PLOOC_CLASS_IMPLEMENT
  155. #undef __PLOOC_CLASS_INHERIT
  156. /*============================ TYPES
  157. * =========================================*/
  158. /*============================ GLOBAL VARIABLES
  159. * ==============================*/
  160. /*============================ LOCAL VARIABLES
  161. * ===============================*/
  162. /*============================ PROTOTYPES
  163. * ====================================*/
  164. #ifdef __cplusplus
  165. }
  166. #endif