Advertisement
Guest User

Untitled

a guest
Apr 21st, 2015
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. /*
  2. * ringbuffer.h
  3. *
  4. * Created on: 09/06/2014
  5. * Author: Wellington
  6. */
  7.  
  8. #ifndef RINGBUFFER_H_
  9. #define RINGBUFFER_H_
  10.  
  11.  
  12. template<typename T, int BUFFERSIZE>
  13. class ringbuffer {
  14. public:
  15. ringbuffer();
  16. virtual ~ringbuffer();
  17. bool empty();
  18. void push_back(T);
  19. T pop_front();
  20. void clear_buffer();
  21. private:
  22. int head;
  23. int tail;
  24. T buffer[BUFFERSIZE];
  25.  
  26.  
  27.  
  28. };
  29.  
  30. template<typename T, int BUFFERSIZE>
  31. ringbuffer<T,BUFFERSIZE>::ringbuffer() {
  32.  
  33. clear_buffer();
  34. }
  35.  
  36. template<typename T, int BUFFERSIZE>
  37. ringbuffer<T,BUFFERSIZE>::~ringbuffer() {
  38. }
  39.  
  40. template<typename T, int BUFFERSIZE>
  41. bool ringbuffer<T,BUFFERSIZE>::empty() {
  42. return head == tail;
  43. }
  44. template<typename T, int BUFFERSIZE>
  45. void ringbuffer<T,BUFFERSIZE>::clear_buffer(){
  46. _disable_interrupts();
  47.  
  48.  
  49. head = 0;
  50. tail = 0;
  51.  
  52. _enable_interrupts();
  53. }
  54.  
  55.  
  56. template<typename T, int BUFFERSIZE>
  57. void ringbuffer<T,BUFFERSIZE>::push_back(T c){
  58.  
  59. int i = (unsigned int)(head + 1);
  60.  
  61. if(i == BUFFERSIZE){
  62. i=0;
  63. }
  64. if ( i != tail ) {
  65. buffer[head] = c;
  66. head = i;
  67. }
  68. }
  69.  
  70.  
  71. template<typename T, int BUFFERSIZE>
  72. T ringbuffer<T,BUFFERSIZE>::pop_front() {
  73. T c;
  74.  
  75. _disable_interrupts();
  76.  
  77. if (head != tail) {
  78. c = buffer[tail];
  79. tail = (unsigned int)(tail + 1);
  80. if(tail==BUFFERSIZE){
  81. tail=0;
  82. }
  83.  
  84. }
  85. _enable_interrupts();
  86. return c;
  87. }
  88.  
  89.  
  90.  
  91. #endif /* RINGBUFFER_H_ */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement