1
0

Remaining.h 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #ifndef NULLSOFT_REMAININGH
  2. #define NULLSOFT_REMAININGH
  3. #include <assert.h>
  4. #include <memory.h>
  5. /* this class is used to store leftover samples */
  6. class Remaining
  7. {
  8. public:
  9. Remaining()
  10. : store(0), size(0), used(0)
  11. {}
  12. void Allocate(unsigned long _size)
  13. {
  14. assert(_size);
  15. used=0;
  16. size=_size;
  17. if (store)
  18. delete [] store;
  19. store = new unsigned char [size];
  20. }
  21. /* Saves the incoming data and updates the pointer positions */
  22. template <class storage_t>
  23. void UpdatingWrite(storage_t *&data, unsigned long &bytes)
  24. {
  25. unsigned long bytesToWrite = min(bytes, SizeRemaining());
  26. Write(data, bytesToWrite);
  27. assert(bytesToWrite);
  28. data = (storage_t *)((char *)data + bytesToWrite);
  29. bytes -= bytesToWrite;
  30. }
  31. void Write(void *data, unsigned long bytes)
  32. {
  33. unsigned char *copy = (unsigned char *)store;
  34. copy+=used;
  35. memcpy(copy, data, bytes);
  36. used+=bytes;
  37. }
  38. unsigned long SizeRemaining()
  39. {
  40. return size-used;
  41. }
  42. bool Empty()
  43. {
  44. return !used;
  45. }
  46. bool Full()
  47. {
  48. return size == used;
  49. }
  50. void *GetData()
  51. {
  52. return (void *)store;
  53. }
  54. void Flush()
  55. {
  56. used=0;
  57. }
  58. unsigned char *store;
  59. long size, used;
  60. };
  61. #endif