wrappedBuffer.hpp 3.6 KB

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