Subversion Repositories dashGPS

Rev

Rev 2 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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