tbegin.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. This file contains helper definitions that allow compile time generic templates
  3. for ANSI C to be implemented and used easily.
  4. Usage for implementing templates:
  5. In the header file that contains the template, do as in the following example:
  6. #include <some_system_library.h>
  7. #include <another_system_library.h>
  8. #include <etc.h>
  9. #include <tbegin.h>
  10. #ifdef TEMPLATE_OK
  11. #ifndef TEMPLATE_IMPLEMENT
  12. struct TEMPLATE_STRUCT {
  13. TEMPLATE_T foo;
  14. };
  15. TEMPLATE_FUNC(foo)(TEMPLATE_NAME * self, TEMPLATE_T elem);
  16. #else
  17. TEMPLATE_FUNC(foo)(TEMPLATE_NAME * self, TEMPLATE_T elem) {
  18. self->foo = elem;
  19. }
  20. #endif
  21. #endif
  22. #include <tend.h>
  23. Then, to use a template, include in some header file or C file: :
  24. #define TEMPLATE_T int
  25. #define TEMPLATE_NAME MyFoo
  26. #include tfoo.h
  27. This will generate the function prototypes and structs needed.
  28. Then in some C file, write:
  29. #define TEMPLATE_T int
  30. #define TEMPLATE_NAME MyMatrix
  31. #define TEMPLATE_IMPLEMENT
  32. #include tmatrix.h
  33. This will generate the implementation of the functions.
  34. Optionally the following template parameters may also be defined.
  35. TEMPLATE_ALLOC(SIZE) : custom malloc-compatible allocation function
  36. TEMPLATE_REALLOC(PTR, SIZE) : custom realloc-compatible allocation function
  37. TEMPLATE_FREE(PTR) : custom free-compatible allocation function
  38. TEMPLATE_PREFIX : prefix for the functions gererated.
  39. Defaults to TEMPLATE_NAME.
  40. The technique used to implement this kind of ANSI C templates is also known as
  41. "X-Macro", since it relies on the availability certain macros that are yet
  42. undefined in this file and must be supplied by the programmer.
  43. */
  44. /* Type of elememts. */
  45. #ifndef TEMPLATE_T
  46. #error Please define TEMPLATE_T as an element type to use in this template.
  47. #undef TEMPLATE_OK
  48. #endif
  49. /* Additional, yet optional element type. */
  50. #ifdef TEMPLATE_NEEDS_U
  51. #ifndef TEMPLATE_U
  52. #error Please define TEMPLATE_U as an element type to use in this template.
  53. #undef TEMPLATE_OK
  54. #endif
  55. #endif
  56. /* Additional, yet optional element type. */
  57. #ifdef TEMPLATE_NEEDS_V
  58. #ifndef TEMPLATE_V
  59. #error Please define TEMPLATE_V as an element type to use in this template.
  60. #undef TEMPLATE_OK
  61. #endif
  62. #endif
  63. /* Additional, yet optional element type. */
  64. #ifdef TEMPLATE_NEEDS_W
  65. #ifndef TEMPLATE_W
  66. #error Please define TEMPLATE_W as an element type to use this template.
  67. #undef TEMPLATE_OK
  68. #endif
  69. #endif
  70. /* Name of generated struct. */
  71. #ifndef TEMPLATE_NAME
  72. #error Please define TEMPLATE_NAME to be the name of the main templated type.
  73. #undef TEMPLATE_OK
  74. #endif
  75. /* Prefix for generated function names. Defaults to TEMPLATE_NAME. */
  76. #ifndef TEMPLATE_PREFIX
  77. #define TEMPLATE_PREFIX TEMPLATE_NAME
  78. #endif
  79. #ifndef TEMPLATE_ALLOC
  80. #define TEMPLATE_ALLOC(SIZE) calloc(SIZE, 1)
  81. #endif
  82. #ifndef TEMPLATE_FREE
  83. #define TEMPLATE_FREE(MEM) free(MEM)
  84. #endif
  85. /* needed C library functions */
  86. #include <stdlib.h>
  87. /* Helper macros. */
  88. #define TSTR_HELPER(S) #S
  89. /* Stringify macro */
  90. #define TSTR(S) TSTR_HELPER(S)
  91. #define TJOIN_HELPER(A, B) A##B
  92. /* Token pasting (joining) macro. */
  93. #define TJOIN(A,B) TJOIN_HELPER(A,B)
  94. #define TSTRUCT(NAME) TJOIN(NAME, _struct)
  95. #define TFUNC(NAME, FUNC) TJOIN(NAME, FUNC)
  96. /* Specific helper macros. */
  97. #define TEMPLATE_STRUCT TSTRUCT(TEMPLATE_NAME)
  98. #define TEMPLATE_FUNC(FUNC) TFUNC(TEMPLATE_PREFIX, FUNC)
  99. #ifndef FALSE
  100. #define FALSE 0
  101. #endif
  102. #ifndef TRUE
  103. #define TRUE (!FALSE)
  104. #endif
  105. /* Don't proceed unless the essential template parameters are set. */
  106. #if defined(TEMPLATE_NAME) && defined(TEMPLATE_T)
  107. #define TEMPLATE_OK 1
  108. #else
  109. #undef TEMPLATE_OK
  110. #endif