SampleQueue.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #pragma once
  2. #include <bfc/platform/types.h>
  3. #include <deque>
  4. #include "../nu/AutoLock.h"
  5. template <class SampleData>
  6. class SampleQueue
  7. {
  8. public:
  9. void PushFree(SampleData *new_sample)
  10. {
  11. queue_guard.Lock();
  12. free_queue.push_front(new_sample);
  13. queue_guard.Unlock();
  14. }
  15. void PushProcessed(SampleData *new_sample)
  16. {
  17. queue_guard.Lock();
  18. processed_queue.push_front(new_sample);
  19. queue_guard.Unlock();
  20. }
  21. // will return 0 if none ready
  22. SampleData *PopProcessed()
  23. {
  24. SampleData *sample=0;
  25. queue_guard.Lock();
  26. if (!processed_queue.empty())
  27. {
  28. sample = processed_queue.back();
  29. processed_queue.pop_back();
  30. }
  31. queue_guard.Unlock();
  32. return sample;
  33. }
  34. SampleData *PopFree()
  35. {
  36. SampleData *sample=0;
  37. queue_guard.Lock();
  38. if (!free_queue.empty())
  39. {
  40. sample = free_queue.back();
  41. free_queue.pop_back();
  42. }
  43. queue_guard.Unlock();
  44. if (!sample)
  45. sample = new SampleData;
  46. return sample;
  47. }
  48. void Trim()
  49. {
  50. queue_guard.Lock();
  51. //free_queue.deleteAll();
  52. auto it_f = free_queue.begin();
  53. while (it_f != free_queue.end())
  54. {
  55. SampleData* p = *it_f;
  56. delete p;
  57. it_f = free_queue.erase(it_f);
  58. }
  59. //processed_queue.deleteAll();
  60. auto it_p = processed_queue.begin();
  61. while (it_p != processed_queue.end())
  62. {
  63. SampleData* p = *it_p;
  64. delete p;
  65. it_p = processed_queue.erase(it_p);
  66. }
  67. queue_guard.Unlock();
  68. }
  69. private:
  70. std::deque<SampleData*> free_queue;
  71. std::deque<SampleData*> processed_queue;
  72. Nullsoft::Utility::LockGuard queue_guard;
  73. };