plooc.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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 __PROTECTED_LOW_OVERHEAD_OBJECT_ORIENTED_C_H__
  18. #define __PROTECTED_LOW_OVERHEAD_OBJECT_ORIENTED_C_H__
  19. /*============================ INCLUDES ======================================*/
  20. #if (!defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L) && \
  21. !defined(__cplusplus)
  22. //! you have to define this by yourselves
  23. #else
  24. #include <stdbool.h>
  25. #include <stdint.h>
  26. #endif
  27. /*! \NOTE the uint_fast8_t used in this header file is defined in stdint.h
  28. if you don't have stdint.h supported in your toolchain, you should
  29. define uint_fast8_t all by yourself with following rule:
  30. a. if the target processor is 8 bits, define it as uint8_t
  31. b. if the target processor is 16 bits, define it as uint16_t
  32. c. if the target processor is 32 bits, define it as uint32_t
  33. d. if the target processor is 64 bits, define it as either uint32_t or
  34. uint64_t
  35. */
  36. #if defined(__clang__)
  37. #pragma clang diagnostic push
  38. #pragma clang diagnostic ignored "-Wunknown-warning-option"
  39. #pragma clang diagnostic ignored "-Wreserved-identifier"
  40. #pragma clang diagnostic ignored "-Wtypedef-redefinition"
  41. #pragma clang diagnostic ignored "-Wmissing-declarations"
  42. #pragma clang diagnostic ignored "-Wempty-body"
  43. #pragma clang diagnostic ignored "-Wmicrosoft-anon-tag"
  44. #elif ((__ARMCC_VERSION >= 5000000) && (__ARMCC_VERSION < 6000000))
  45. /*! arm compiler 5 */
  46. #pragma push
  47. #pragma diag_suppress 1, 64, 174, 177, 188, 68, 513, 144, 2525
  48. #elif defined(__IAR_SYSTEMS_ICC__)
  49. /*! IAR */
  50. #elif defined(__GNUC__)
  51. #pragma GCC diagnostic push
  52. #pragma GCC diagnostic ignored "-Wmissing-declarations"
  53. #pragma GCC diagnostic ignored "-Wempty-body"
  54. #pragma GCC diagnostic ignored "-Wpragmas"
  55. #pragma GCC diagnostic ignored "-Wformat="
  56. #pragma GCC diagnostic ignored "-Wmissing-braces"
  57. #endif
  58. #ifdef __cplusplus
  59. extern "C" {
  60. #endif
  61. /*============================ MACROS ========================================*/
  62. #ifndef __cplusplus
  63. #ifndef plooc_private
  64. #define plooc_private static
  65. #endif
  66. #ifndef plooc_public
  67. #define plooc_public
  68. #endif
  69. #endif
  70. /*============================ MACROFIED FUNCTIONS
  71. * ===========================*/
  72. /*! \note add which macro to support multiple inheriting and implementations
  73. *!
  74. *! declare_interface( i_lv0_abc_t )
  75. *! declare_interface( i_lv0_efg_t )
  76. *! def_interface( i_lv0_abc_t )
  77. *! ...
  78. *! end_def_interface( i_lv0_abc_t )
  79. *!
  80. *! def_interface( i_lv0_efg_t )
  81. *! ...
  82. *! end_def_interface( i_lv0_efg_t )
  83. *!
  84. *! declare_interface( i_lv1_t )
  85. *! def_interface( i_lv1_t, which( inherit( i_lv0_abc_t )
  86. *! inherit( i_lv0_efg_t ) ) )
  87. *! ...
  88. *! end_def_interface( i_lv1_t )
  89. */
  90. #define __declare_interface(__name) typedef struct __name __name;
  91. #define __declare_structure(__name) typedef struct __name __name;
  92. #if (!defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L) && \
  93. !defined(__cplusplus)
  94. //! \name interface definition
  95. //! @{
  96. #define __def_interface(__name) \
  97. /*typedef struct __name __name;*/ \
  98. struct __name {
  99. #define __end_def_interface(__name) \
  100. } \
  101. ;
  102. //! @}
  103. //! \name structure definition
  104. //! @{
  105. #define __def_structure(__name) \
  106. /*typedef struct __name __name; */ \
  107. struct __name {
  108. #define __end_def_structure(__name) \
  109. } \
  110. ;
  111. //! @}
  112. #else
  113. //! \name interface definition
  114. //! @{
  115. #define __def_interface(__name, ...) \
  116. typedef struct __name __name; \
  117. __VA_ARGS__ \
  118. struct __name {
  119. #define __end_def_interface(__name) \
  120. } \
  121. ;
  122. //! @}
  123. //! \name structure definition
  124. //! @{
  125. #define __def_structure(__name, ...) \
  126. typedef struct __name __name; \
  127. __VA_ARGS__ \
  128. struct __name {
  129. #define __end_def_structure(__name) \
  130. } \
  131. ;
  132. //! @}
  133. #endif
  134. //! \brief macro for inheritance
  135. #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
  136. #define __IMPLEMENT_EX(__TYPE, __NAME) __TYPE __NAME;
  137. #else
  138. #define __IMPLEMENT_EX(__TYPE, __NAME) \
  139. union { \
  140. __TYPE __NAME; \
  141. __TYPE; \
  142. };
  143. #endif
  144. #define __INHERIT_EX(__TYPE, __NAME) __TYPE __NAME;
  145. #define INHERIT_EX(__TYPE, __NAME) __INHERIT_EX(__TYPE, __NAME)
  146. #define __INHERIT(__TYPE) INHERIT_EX(__TYPE, use_as__##__TYPE)
  147. #define INHERIT(__TYPE) __INHERIT(__TYPE)
  148. /*! \note You can only use IMPLEMENT when defining INTERFACE. For Implement
  149. * interface when defining class, you should use DEF_CLASS_IMPLEMENT
  150. * instead.
  151. */
  152. #define __IMPLEMENT(__INTERFACE) \
  153. __IMPLEMENT_EX(__INTERFACE, use_as__##__INTERFACE)
  154. #define IMPLEMENT(__INTERFACE) __IMPLEMENT(__INTERFACE)
  155. /*! \note if you have used INHERIT or IMPLEMENT to define a class / INTERFACE,
  156. you can use OBJ_CONVERT_AS to extract the reference to the inherited
  157. object.
  158. \*/
  159. #define __OBJ_CONVERT_AS(__OBJ, __INTERFACE) (__OBJ.use_as__##__INTERFACE)
  160. #define OBJ_CONVERT_AS(__OBJ, __INTERFACE) \
  161. __OBJ_CONVERT_AS((__OBJ), __INTERFACE)
  162. #define __REF_OBJ_AS(__OBJ, __TYPE) (&(__OBJ.use_as__##__TYPE))
  163. #define REF_OBJ_AS(__OBJ, __TYPE) __REF_OBJ_AS((__OBJ), __TYPE)
  164. #if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || \
  165. defined(__cplusplus)
  166. /*! \brief You can use __PLOOC_EVAL() to dynamically select the right API which
  167. *! has the right number of parameters (no more than 8).
  168. */
  169. //! @{
  170. #define __PLOOC_VA_NUM_ARGS_IMPL(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, \
  171. _11, _12, _13, _14, _15, _16, __N, ...) \
  172. __N
  173. #define __PLOOC_VA_NUM_ARGS(...) \
  174. __PLOOC_VA_NUM_ARGS_IMPL(0, ##__VA_ARGS__, 16, 15, 14, 13, 12, 11, 10, 9, \
  175. 8, 7, 6, 5, 4, 3, 2, 1, 0)
  176. #define __16_PLOOC_EVAL(__FUNC, __NO_ARGS) __FUNC##__NO_ARGS
  177. #define __15_PLOOC_EVAL(__FUNC, __NO_ARGS) __16_PLOOC_EVAL(__FUNC, __NO_ARGS)
  178. #define __14_PLOOC_EVAL(__FUNC, __NO_ARGS) __15_PLOOC_EVAL(__FUNC, __NO_ARGS)
  179. #define __13_PLOOC_EVAL(__FUNC, __NO_ARGS) __14_PLOOC_EVAL(__FUNC, __NO_ARGS)
  180. #define __12_PLOOC_EVAL(__FUNC, __NO_ARGS) __13_PLOOC_EVAL(__FUNC, __NO_ARGS)
  181. #define __11_PLOOC_EVAL(__FUNC, __NO_ARGS) __12_PLOOC_EVAL(__FUNC, __NO_ARGS)
  182. #define __10_PLOOC_EVAL(__FUNC, __NO_ARGS) __11_PLOOC_EVAL(__FUNC, __NO_ARGS)
  183. #define __9_PLOOC_EVAL(__FUNC, __NO_ARGS) __10_PLOOC_EVAL(__FUNC, __NO_ARGS)
  184. #define __8_PLOOC_EVAL(__FUNC, __NO_ARGS) __9_PLOOC_EVAL(__FUNC, __NO_ARGS)
  185. #define __7_PLOOC_EVAL(__FUNC, __NO_ARGS) __8_PLOOC_EVAL(__FUNC, __NO_ARGS)
  186. #define __6_PLOOC_EVAL(__FUNC, __NO_ARGS) __7_PLOOC_EVAL(__FUNC, __NO_ARGS)
  187. #define __5_PLOOC_EVAL(__FUNC, __NO_ARGS) __6_PLOOC_EVAL(__FUNC, __NO_ARGS)
  188. #define __4_PLOOC_EVAL(__FUNC, __NO_ARGS) __5_PLOOC_EVAL(__FUNC, __NO_ARGS)
  189. #define __3_PLOOC_EVAL(__FUNC, __NO_ARGS) __4_PLOOC_EVAL(__FUNC, __NO_ARGS)
  190. #define __2_PLOOC_EVAL(__FUNC, __NO_ARGS) __3_PLOOC_EVAL(__FUNC, __NO_ARGS)
  191. #define __1_PLOOC_EVAL(__FUNC, __NO_ARGS) __2_PLOOC_EVAL(__FUNC, __NO_ARGS)
  192. #define __0_PLOOC_EVAL(__FUNC, __NO_ARGS) __1_PLOOC_EVAL(__FUNC, __NO_ARGS)
  193. #define __PLOOC_EVAL(__FUNC, ...) \
  194. __0_PLOOC_EVAL(__FUNC, __PLOOC_VA_NUM_ARGS(__VA_ARGS__))
  195. //! @}
  196. #endif
  197. /*----------------------------------------------------------------------------*
  198. * new standard (lower case) *
  199. *----------------------------------------------------------------------------*/
  200. #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
  201. #define def_interface(__name) __def_interface(__name)
  202. #define define_interface(__name) __def_interface(__name)
  203. #define def_structure(__name) __def_structure(__name)
  204. #define define_structure(__name) __def_structure(__name)
  205. #define def_params(__code) __code
  206. #define define_params(__code) __code
  207. #define end_def_params()
  208. #define end_define_params()
  209. #define def_members(__code) __code
  210. #define define_members(__code) __code
  211. #define end_def_members()
  212. #define end_define_members()
  213. #else
  214. #define def_interface(__name, ...) __def_interface(__name, __VA_ARGS__)
  215. #define define_interface(__name, ...) __def_interface(__name, __VA_ARGS__)
  216. #define def_structure(__name, ...) __def_structure(__name, __VA_ARGS__)
  217. #define define_structure(__name, ...) __def_structure(__name, __VA_ARGS__)
  218. #define def_params(...) __VA_ARGS__
  219. #define define_params(...) __VA_ARGS__
  220. #define end_def_params(...)
  221. #define end_define_params(...)
  222. #define def_members(...) __VA_ARGS__
  223. #define define_members(...) __VA_ARGS__
  224. #define end_def_members(...)
  225. #define end_define_members(...)
  226. #endif
  227. #define implement(__type) IMPLEMENT(__type)
  228. #define implement_ex(__type, __name) __IMPLEMENT_EX(__type, __name)
  229. #define inherit_ex(__type, __name) INHERIT_EX(__type, __name)
  230. #define inherit(__type) INHERIT(__type)
  231. #define ref_interface(__INTERFACE) const __INTERFACE* ptMethod;
  232. #define convert_obj_as(__obj, __type) OBJ_CONVERT_AS(__obj, __type)
  233. #define obj_convert_as(__obj, __type) \
  234. OBJ_CONVERT_AS(__obj, __type) /* obsolete */
  235. #define ref_obj_as(__obj, __type) REF_OBJ_AS(__obj, __type)
  236. #define end_def_interface(__name) __end_def_interface(__name)
  237. #define end_define_interface(__name) __end_def_interface(__name)
  238. #define dcl_interface(__name) __declare_interface(__name)
  239. #define declare_interface(__name) __declare_interface(__name)
  240. #define end_def_structure(__name) __end_def_structure(__name)
  241. #define end_define_structure(__name) __end_def_structure(__name)
  242. #define dcl_structure(__name) __declare_structure(__name)
  243. #define declare_structure(__name) __declare_structure(__name)
  244. #define this_interface(__INTERFACE) convert_obj_as(this, __INTERFACE)
  245. #define base_obj(__type) convert_obj_as(this, __type)
  246. /*============================ TYPES
  247. * =========================================*/
  248. //! \name interface: u32_property_t
  249. //! @{
  250. dcl_interface(u32_property_t)
  251. def_interface(u32_property_t) bool (*Set)(uint32_t wValue);
  252. uint32_t (*Get)(void);
  253. end_def_interface(u32_property_t)
  254. //! @}
  255. //! \name interface: u16_property_t
  256. //! @{
  257. dcl_interface(u16_property_t)
  258. def_interface(u16_property_t) bool (*Set)(uint_fast16_t wValue);
  259. uint_fast16_t (*Get)(void);
  260. end_def_interface(u16_property_t)
  261. //! @}
  262. //! \name interface: u8_property_t
  263. //! @{
  264. dcl_interface(u8_property_t)
  265. def_interface(u8_property_t) bool (*Set)(uint_fast8_t wValue);
  266. uint_fast8_t (*Get)(void);
  267. end_def_interface(u8_property_t)
  268. //! @}
  269. //! \name interface: i32_property_t
  270. //! @{
  271. dcl_interface(i32_property_t)
  272. def_interface(i32_property_t) bool (*Set)(int32_t wValue);
  273. int32_t (*Get)(void);
  274. end_def_interface(i32_property_t)
  275. //! @}
  276. //! \name interface: i16_property_t
  277. //! @{
  278. dcl_interface(i16_property_t)
  279. def_interface(i16_property_t) bool (*Set)(int_fast16_t wValue);
  280. int_fast16_t (*Get)(void);
  281. end_def_interface(i16_property_t)
  282. //! @}
  283. //! \name interface: u8_property_t
  284. //! @{
  285. dcl_interface(i8_property_t)
  286. def_interface(i8_property_t) bool (*Set)(int_fast8_t wValue);
  287. int_fast8_t (*Get)(void);
  288. end_def_interface(i8_property_t)
  289. //! @}
  290. //! \name interface: bool_property_t
  291. //! @{
  292. dcl_interface(bool_property_t)
  293. def_interface(bool_property_t) bool (*Set)(bool bValue);
  294. bool (*Get)(void);
  295. end_def_interface(bool_property_t)
  296. //! @}
  297. //! \name interface: bool_property_t
  298. //! @{
  299. dcl_interface(en_property_t)
  300. def_interface(en_property_t) bool (*Enable)(void);
  301. bool (*Disable)(void);
  302. end_def_interface(en_property_t)
  303. //! @}
  304. /*============================ GLOBAL VARIABLES
  305. * ==============================*/
  306. /*============================ LOCAL VARIABLES
  307. * ===============================*/
  308. /*============================ PROTOTYPES
  309. * ====================================*/
  310. #ifdef __cplusplus
  311. }
  312. #endif
  313. //#if defined(__clang__)
  314. //# pragma clang diagnostic pop
  315. //#endif
  316. #endif
  317. /* EOF */