template.h 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #ifndef _TEMPLATE_H_
  2. #define _TEMPLATE_H_
  3. /*--------------------------------------------------------------------------------*/
  4. /* Looping and Iteration */
  5. /*--------------------------------------------------------------------------------*/
  6. /**
  7. * Template for the general structure of a loop.
  8. */
  9. #define TEMPLATE_LOOP(setup, loop_def, body) \
  10. do \
  11. { \
  12. setup; \
  13. loop_def { \
  14. body; \
  15. } \
  16. } while (0)
  17. /**
  18. * Template for looping over an array-like sequence.
  19. */
  20. #define TEMPLATE_DO_ARR_LIKE(iter_idx, type, \
  21. arr, arr_length, \
  22. iter_elem_setup, \
  23. body) \
  24. do \
  25. { \
  26. TEMPLATE_LOOP( \
  27. int iter_idx, \
  28. for(iter_idx = 0; iter_idx < (arr_length); ++iter_idx), \
  29. iter_elem_setup; \
  30. body); \
  31. } while (0)
  32. /**
  33. * Template for looping over the contents of an array.
  34. */
  35. #define TEMPLATE_DO_ARR(iter_idx, type, iter_elem, arr, arr_length, body) \
  36. do \
  37. { \
  38. TEMPLATE_DO_ARR_LIKE( \
  39. iter_idx, type, arr, arr_length, \
  40. type iter_elem = (arr)[iter_idx], \
  41. body); \
  42. } while (0)
  43. /**
  44. * Template for looping over the contents of an #ARR_DESC.
  45. */
  46. #define TEMPLATE_DO_ARR_DESC(iter_idx, type, iter_elem, arr_desc, body) \
  47. do \
  48. { \
  49. TEMPLATE_DO_ARR_LIKE( \
  50. iter_idx, type, arr_desc, (arr_desc).element_count, \
  51. type iter_elem = ARR_DESC_ELT(type, iter_idx, &(arr_desc)), \
  52. body); \
  53. } while (0)
  54. /*--------------------------------------------------------------------------------*/
  55. /* Test Definition */
  56. /*--------------------------------------------------------------------------------*/
  57. /**
  58. * Template for the general structure of a test.
  59. */
  60. #define TEMPLATE_TEST(setup, body, teardown) \
  61. do \
  62. { \
  63. setup; \
  64. body; \
  65. teardown; \
  66. } while (0)
  67. /**
  68. * Template for calling a function.
  69. *
  70. * @note Surround function arguments with the #PAREN() macro.
  71. *
  72. * @example
  73. * void my_func(int arg1, int arg2);
  74. *
  75. * TEMPLATE_CALL_FN(my_func, PAREN(3, 7));
  76. */
  77. #define TEMPLATE_CALL_FN(fn, fn_args) \
  78. fn fn_args
  79. #endif /* _TEMPLATE_H_ */