wrappedBuffer.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #pragma once
  2. #include <string.h>
  3. #include <string>
  4. template <typename T, int SIZE =10>
  5. class WrappedBuffer
  6. {
  7. public:
  8. WrappedBuffer();
  9. virtual ~WrappedBuffer();
  10. void put(T item);
  11. void put(T item[], unsigned int count);
  12. void pop_back();
  13. unsigned int size() const;
  14. std::basic_string<T> toString() const;
  15. T* toArray(T arr[SIZE]) const;
  16. protected:
  17. T buffer[SIZE];
  18. int curR;
  19. int curW;
  20. };
  21. template<typename T, int SIZE>
  22. WrappedBuffer<T, SIZE>::WrappedBuffer(): curR(0), curW(-1)
  23. { }
  24. template<typename T, int SIZE>
  25. WrappedBuffer<T, SIZE>::~WrappedBuffer()
  26. { }
  27. template<typename T, int SIZE>
  28. void WrappedBuffer<T, SIZE>::put(T item)
  29. {
  30. if (curW +1 == SIZE)
  31. {
  32. curR = 1;
  33. curW = 0;
  34. buffer[0] = item;
  35. }
  36. else if (curW == -1)
  37. {
  38. curR = SIZE;
  39. buffer[curW = 0] = item;
  40. }
  41. else
  42. {
  43. buffer[++curW] = item;
  44. if (curR == curW)
  45. {
  46. if (++curR > SIZE)
  47. curR = 0;
  48. }
  49. }
  50. }
  51. template<typename T, int SIZE>
  52. void WrappedBuffer<T, SIZE>::put(T items[], unsigned int count)
  53. {
  54. unsigned int newSize = size() + count;
  55. if (!count)
  56. return;
  57. while (count > SIZE)
  58. {
  59. count -= SIZE;
  60. items += SIZE;
  61. }
  62. if (curW + count >= SIZE)
  63. {
  64. if (curW +1 != SIZE)
  65. {
  66. memcpy(&buffer[curW +1], items, sizeof(T) * (SIZE - curW -1));
  67. items += (SIZE - curW -1);
  68. count -= (SIZE - curW -1);
  69. }
  70. curW = -1;
  71. }
  72. memcpy(&buffer[curW +1], items, sizeof(T) * count);
  73. curW += count;
  74. if (curW == SIZE)
  75. {
  76. curW = 0;
  77. curR = 1;
  78. }
  79. else if (newSize >= SIZE)
  80. curR = (curW +1) % SIZE;
  81. }
  82. template<typename T, int SIZE>
  83. void WrappedBuffer<T, SIZE>::pop_back()
  84. {
  85. unsigned int oldSize = size();
  86. if (oldSize == 0)
  87. return;
  88. else if (oldSize == 1)
  89. {
  90. curW = -1;
  91. curR = 0;
  92. }
  93. else if (--curW < 0)
  94. {
  95. curW += SIZE;
  96. }
  97. }
  98. template<typename T, int SIZE>
  99. unsigned int WrappedBuffer<T, SIZE>::size() const
  100. {
  101. if (curW == -1)
  102. return 0;
  103. return (curR > curW) ? (SIZE - curR + curW +1) : (curW - curR +1);
  104. }
  105. template<typename T, int SIZE>
  106. std::basic_string<T> WrappedBuffer<T, SIZE>::toString() const
  107. {
  108. std::basic_string<T> result(size(), (T) 0);
  109. const unsigned int size = this->size();
  110. int from = (curR == SIZE) ? 0 : curR;
  111. unsigned int j = 0;
  112. for (int i = from; (curW > curR && i <= curW) || (curW <= curR && i < SIZE); ++i)
  113. result[j++] = buffer[i];
  114. for (int i = 0; j < size; ++i)
  115. result[j++] = buffer[i];
  116. return result;
  117. }
  118. template<typename T, int SIZE>
  119. T* WrappedBuffer<T, SIZE>::toArray(T arr[SIZE]) const
  120. {
  121. if (!arr)
  122. arr = new T[SIZE]();
  123. //TODO
  124. return arr;
  125. }