1
1

wrappedBuffer.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. virtual void put(T item);
  11. virtual void put(T item[], unsigned int count);
  12. void pop_back();
  13. void reset();
  14. /**
  15. * @return total size appended since instanciation or clear()
  16. **/
  17. unsigned int totalSize() const;
  18. unsigned int size() const;
  19. /**
  20. * @return Current size (cannot be greater than SIZE
  21. **/
  22. std::basic_string<T> toString() const;
  23. T* toArray(T arr[SIZE]) const;
  24. protected:
  25. T buffer[SIZE];
  26. int curR;
  27. int curW;
  28. unsigned int written;
  29. };
  30. template<typename T, int SIZE>
  31. WrappedBuffer<T, SIZE>::WrappedBuffer(): curR(0), curW(-1)
  32. { }
  33. template<typename T, int SIZE>
  34. WrappedBuffer<T, SIZE>::~WrappedBuffer()
  35. { }
  36. template<typename T, int SIZE>
  37. void WrappedBuffer<T, SIZE>::reset()
  38. {
  39. curR = 0;
  40. curW = -1;
  41. written = 0;
  42. }
  43. template<typename T, int SIZE>
  44. unsigned int WrappedBuffer<T, SIZE>::totalSize() const
  45. { return written; }
  46. template<typename T, int SIZE>
  47. void WrappedBuffer<T, SIZE>::put(T item)
  48. {
  49. written++;
  50. if (curW +1 == SIZE)
  51. {
  52. curR = 1;
  53. curW = 0;
  54. buffer[0] = item;
  55. }
  56. else if (curW == -1)
  57. {
  58. curR = SIZE;
  59. buffer[curW = 0] = item;
  60. }
  61. else
  62. {
  63. buffer[++curW] = item;
  64. if (curR == curW)
  65. {
  66. if (++curR > SIZE)
  67. curR = 0;
  68. }
  69. }
  70. }
  71. template<typename T, int SIZE>
  72. void WrappedBuffer<T, SIZE>::put(T items[], unsigned int count)
  73. {
  74. unsigned int newSize = size() + count;
  75. if (!count)
  76. return;
  77. written += count;
  78. while (count > SIZE)
  79. {
  80. count -= SIZE;
  81. items += SIZE;
  82. }
  83. if (curW + count >= SIZE)
  84. {
  85. if (curW +1 != SIZE)
  86. {
  87. memcpy(&buffer[curW +1], items, sizeof(T) * (SIZE - curW -1));
  88. items += (SIZE - curW -1);
  89. count -= (SIZE - curW -1);
  90. }
  91. curW = -1;
  92. }
  93. memcpy(&buffer[curW +1], items, sizeof(T) * count);
  94. curW += count;
  95. if (curW == SIZE)
  96. {
  97. curW = 0;
  98. curR = 1;
  99. }
  100. else if (newSize >= SIZE)
  101. curR = (curW +1) % SIZE;
  102. }
  103. template<typename T, int SIZE>
  104. void WrappedBuffer<T, SIZE>::pop_back()
  105. {
  106. unsigned int oldSize = size();
  107. written--;
  108. if (oldSize == 0)
  109. return;
  110. else if (oldSize == 1)
  111. {
  112. curW = -1;
  113. curR = 0;
  114. }
  115. else if (--curW < 0)
  116. {
  117. curW += SIZE;
  118. }
  119. }
  120. template<typename T, int SIZE>
  121. unsigned int WrappedBuffer<T, SIZE>::size() const
  122. {
  123. if (curW == -1)
  124. return 0;
  125. return (curR > curW) ? (SIZE - curR + curW +1) : (curW - curR +1);
  126. }
  127. template<typename T, int SIZE>
  128. std::basic_string<T> WrappedBuffer<T, SIZE>::toString() const
  129. {
  130. const unsigned int size = this->size();
  131. std::basic_string<T> result;
  132. if (!size)
  133. return result;
  134. result.reserve(size);
  135. const int from = (curR == SIZE) ? 0 : curR;
  136. int i = 0;
  137. unsigned int j =0;
  138. for (i = from; (curW >= from && i <= curW) || (curW < from && i < SIZE); ++i)
  139. {
  140. j++;
  141. result += buffer[i];
  142. }
  143. i = 0;
  144. while (i <= curW && j < size)
  145. {
  146. result += buffer[i++];
  147. j++;
  148. }
  149. return result;
  150. }
  151. template<typename T, int SIZE>
  152. T* WrappedBuffer<T, SIZE>::toArray(T arr[SIZE]) const
  153. {
  154. if (!arr)
  155. arr = new T[SIZE]();
  156. //TODO
  157. return arr;
  158. }