ADC  1.0
 All Classes Files Functions Variables Macros Pages
RingBuffer.cpp
Go to the documentation of this file.
1 #include "RingBuffer.h"
2 
3 // I CAN'T GET NEW[] AND DELETE[] TO WORK....
4 /*RingBuffer::RingBuffer()
5 {
6  RingBuffer(DEFAULT_BUFFER_SIZE);
7 }
8 
9 RingBuffer::RingBuffer(int buffer_size) {
10  if(buffer_size%2) { // not a power of 2
11  size = (int)ceil(1.443*log(buffer_size));
12  }
13  b_size = buffer_size;
14  elems = new int[1024];
15 }
16 
17 RingBuffer::~RingBuffer()
18 {
19  delete[] elems;
20 }*/
21 
22 
24 {
25  //ctor
26 }
27 
29 {
30  // dtor
31 }
32 
34  return (b_end == (b_start ^ b_size));
35 }
36 
38  return (b_end == b_start);
39 }
40 
41 void RingBuffer::write(int value) {
42  elems[b_end&(b_size-1)] = value;
43  if (isFull()) { /* full, overwrite moves start pointer */
44  b_start = increase(b_start);
45  }
46  b_end = increase(b_end);
47 }
48 
50  int result = elems[b_start&(b_size-1)];
51  b_start = increase(b_start);
52  return result;
53 }
54 
55 // increases the pointer modulo 2*size-1
56 int RingBuffer::increase(int p) {
57  return (p + 1)&(2*b_size-1);
58 }
int read()
Read a value from the buffer.
Definition: RingBuffer.cpp:49
RingBuffer()
Default constructor, buffer has a size DEFAULT_BUFFER_SIZE.
Definition: RingBuffer.cpp:23
int isEmpty()
Returns 1 (true) if the buffer is empty.
Definition: RingBuffer.cpp:37
virtual ~RingBuffer()
Definition: RingBuffer.cpp:28
void write(int value)
Write a value into the buffer.
Definition: RingBuffer.cpp:41
int isFull()
Returns 1 (true) if the buffer is full.
Definition: RingBuffer.cpp:33