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.9KB

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