libcm is a C development framework with an emphasis on audio signal processing applications.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cmDListTpl.h 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //( { file_desc:"Template 'include' code for using cmDList as a template." kw:[container] }
  2. // The following two macros must be defined prior to including this code:
  3. // #define cmSFX(a) a##_MySuffix
  4. // #define cmTYPE My_Type
  5. // Also define cmGEN_HDR to generate the .h code and/or
  6. // cmGEN_CODE to generate the .c code
  7. #ifdef cmGEN_HDR
  8. typedef int (*cmSFX(cmDListFunc))( void* arg, const cmTYPE* v0, const cmTYPE* v1 );
  9. cmDlRC_t cmSFX(cmDListAlloc)( cmCtx_t* ctx, cmDListH_t* hp, cmSFX(cmDListFunc) func, void* funcArg );
  10. cmDlRC_t cmSFX(cmDListInsert)( cmDListH_t h, const cmTYPE* recd, bool resyncFl );
  11. cmDlRC_t cmSFX(cmDListDelete)( cmDListH_t h, const cmTYPE* recd, bool resyncFl );
  12. cmDlRC_t cmSFX(cmDListAllocIndex)( cmDListH_t h, unsigned indexId, cmSFX(cmDListFunc) f, void* farg );
  13. const cmTYPE* cmSFX(cmDListIterGet)( cmDListIterH_t iH );
  14. const cmTYPE* cmSFX(cmDListIterPrev)( cmDListIterH_t iH );
  15. const cmTYPE* cmSFX(cmDListIterNext)( cmDListIterH_t iH );
  16. const cmTYPE* cmSFX(cmDListIterFind)( cmDListIterH_t iH, const cmTYPE* key);
  17. #endif // cmGEN_HDR
  18. #ifdef cmGEN_CODE
  19. typedef struct
  20. {
  21. cmSFX(cmDListFunc) func;
  22. void* funcArg;
  23. } cmSFX(_cmDListArg);
  24. // This function is called when the index identified by indexId is about to be deleted.
  25. // It is used to cleanup the arg record created by cmSFX(cmDListIndexAlloc()).
  26. void cmSFX(_cmDListIndexOnFree)( unsigned indexId, void* arg )
  27. {
  28. cmMemFree(arg);
  29. }
  30. // Proxy function used to cast generic compare function to the user defined compare function.
  31. int cmSFX(_cmDListCmp)( void* arg, const void* v0, unsigned vn0, const void* v1, unsigned vn1 )
  32. {
  33. assert(vn0==vn1);
  34. cmSFX(_cmDListArg)* a = (cmSFX(_cmDListArg)*)arg;
  35. return a->func(a->funcArg,(const cmTYPE*)v0,(const cmTYPE*)v1);
  36. }
  37. cmDlRC_t cmSFX(cmDListAlloc)( cmCtx_t* ctx, cmDListH_t* hp, cmSFX(cmDListFunc) func, void* funcArg )
  38. {
  39. cmDlRC_t rc;
  40. cmSFX(_cmDListArg)* a = NULL;
  41. if( func != NULL )
  42. {
  43. // allocate a record to redirect the compare function callback
  44. a = cmMemAllocZ(cmSFX(_cmDListArg),1);
  45. a->func = func;
  46. a->funcArg = funcArg;
  47. }
  48. if((rc = cmDListAlloc(ctx,hp,cmSFX(_cmDListCmp),a)) != kOkDlRC )
  49. return rc;
  50. if( func != NULL )
  51. if((rc = cmDListIndexSetFreeFunc(*hp,0,cmSFX(_cmDListIndexOnFree))) != kOkDlRC )
  52. cmDListFree(hp);
  53. return rc;
  54. }
  55. cmDlRC_t cmSFX(cmDListIndexAlloc)( cmDListH_t h, unsigned indexId, cmSFX(cmDListFunc) func, void* funcArg )
  56. {
  57. cmDlRC_t rc;
  58. // allocate a record to redirect the compare function callback
  59. cmSFX(_cmDListArg)* a = cmMemAllocZ(cmSFX(_cmDListArg),1);
  60. a->func = func;
  61. a->funcArg = funcArg;
  62. // allocate the index
  63. if((rc = cmDListIndexAlloc(h,indexId,cmSFX(_cmDListCmp),a)) != kOkDlRC )
  64. {
  65. cmMemFree(a);
  66. goto errLabel;
  67. }
  68. // set the index clean up handler
  69. if((rc = cmDListIndexSetFreeFunc(h,indexId,cmSFX(_cmDListIndexOnFree))) != kOkDlRC )
  70. cmDListIndexFree(h,indexId);
  71. errLabel:
  72. return rc;
  73. }
  74. cmDlRC_t cmSFX(cmDListInsert)( cmDListH_t h, const cmTYPE* recd, bool resyncFl )
  75. { return cmDListInsert(h,recd,sizeof(cmTYPE),resyncFl); }
  76. cmDlRC_t cmSFX(cmDListDelete)( cmDListH_t h, const cmTYPE* recd, bool resyncFl )
  77. { return cmDListDelete(h,recd,resyncFl); }
  78. const cmTYPE* cmSFX(cmDListIterGet)( cmDListIterH_t iH )
  79. { return (const cmTYPE*)cmDListIterGet(iH,NULL);}
  80. const cmTYPE* cmSFX(cmDListIterPrev)( cmDListIterH_t iH )
  81. { return (const cmTYPE*)cmDListIterPrev(iH,NULL); }
  82. const cmTYPE* cmSFX(cmDListIterNext)( cmDListIterH_t iH )
  83. { return (const cmTYPE*)cmDListIterNext(iH,NULL); }
  84. const cmTYPE* cmSFX(cmDListIterFind)( cmDListIterH_t iH, const cmTYPE* key)
  85. { return (const cmTYPE*)cmDListIterFind(iH,key,sizeof(cmTYPE),NULL); }
  86. #endif // cmGEN_CODE
  87. //)
  88. #undef cmSFX
  89. #undef cmTYPE