libcm is a C development framework with an emphasis on audio signal processing applications.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

cmDListTpl.h 3.6KB

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