var_export.h 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-06-04 WillianChan first version
  9. * 2021-06-08 WillianChan support to MS VC++ compiler
  10. */
  11. #ifndef _VAR_EXPORT_H__
  12. #define _VAR_EXPORT_H__
  13. #include <rtthread.h>
  14. /* exported object */
  15. struct ve_exporter
  16. {
  17. const char *module; /* module name */
  18. const char *identifier; /* module identifier */
  19. rt_base_t value; /* module value */
  20. };
  21. typedef struct ve_exporter ve_exporter_t;
  22. /* module object */
  23. struct ve_module
  24. {
  25. const ve_exporter_t *begin; /* the first module of the same name */
  26. const ve_exporter_t *end; /* the last module of the same */
  27. };
  28. typedef struct ve_module ve_module_t;
  29. /* iterator object */
  30. struct ve_iterator
  31. {
  32. const ve_exporter_t *exp_index; /* iterator index */
  33. const ve_exporter_t *exp_end; /* iterate over exporter */
  34. };
  35. typedef struct ve_iterator ve_iterator_t;
  36. #define VE_NOT_FOUND (0xFFFFFFFFu) /* not found */
  37. /* exporter's export command */
  38. #if defined(__GNUC__)
  39. #define VAR_EXPORT(module, identi, value) \
  40. const char _vexp_##identi##_module[] RT_SECTION(".rodata.vexp") = #module; \
  41. const char _vexp_##identi##_identi[] RT_SECTION(".rodata.vexp") = #identi; \
  42. RT_USED const struct ve_exporter _vexp_##module##identi \
  43. RT_SECTION(#module".VarExpTab."#identi) = \
  44. { \
  45. _vexp_##identi##_module, \
  46. _vexp_##identi##_identi, \
  47. value, \
  48. }
  49. #elif defined(_MSC_VER)
  50. #pragma section("VarExpTab$f",read)
  51. #define VAR_EXPORT(module, identi, value) \
  52. const char _vexp_##identi##_module[] RT_SECTION(".rodata.vexp") = #module; \
  53. const char _vexp_##identi##_identi[] RT_SECTION(".rodata.vexp") = #identi; \
  54. __declspec(allocate("VarExpTab$f")) \
  55. RT_USED const struct ve_exporter _vexp_##module##identi = \
  56. { \
  57. _vexp_##identi##_module, \
  58. _vexp_##identi##_identi, \
  59. value, \
  60. }
  61. #else
  62. #define VAR_EXPORT(module, identi, value) \
  63. const char _vexp_##identi##_module[] RT_SECTION(".rodata.vexp") = #module; \
  64. const char _vexp_##identi##_identi[] RT_SECTION(".rodata.vexp") = #identi; \
  65. RT_USED const struct ve_exporter _vexp_##module##identi \
  66. RT_SECTION("1."#module".VarExpTab."#identi) = \
  67. { \
  68. _vexp_##identi##_module, \
  69. _vexp_##identi##_identi, \
  70. value, \
  71. }
  72. #endif
  73. /* initialize var export */
  74. int ve_exporter_init(void);
  75. /* initialize module */
  76. int ve_module_init(ve_module_t *mod, const char *module);
  77. /* initialize iterator */
  78. void ve_iter_init(ve_module_t *mod, ve_iterator_t *iter);
  79. /* iterate backward */
  80. const ve_exporter_t *ve_iter_next(ve_iterator_t *iter);
  81. /* get the value by identifier */
  82. rt_base_t ve_value_get(ve_module_t *mod, const char *identifier);
  83. /* check if this value exists in the module*/
  84. rt_bool_t ve_value_exist(ve_module_t *mod, const char *identifier);
  85. #endif /* _VAR_EXPORT_H__ */