aboutsummaryrefslogtreecommitdiff
path: root/src/midiio/include/CircularBuffer.cpp
blob: 9f4aca687a747b381cc33b6b108ee875383bf087 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
//
// Copyright 1997-1998 by Craig Stuart Sapp, All Rights Reserved.
// Programmer:    Craig Stuart Sapp <craig@ccrma.stanford.edu>
// Creation Date: 19 December 1997
// Last Modified: Wed Jan 21 23:16:54 GMT-0800 1998
// Filename:      ...sig/maint/code/base/CircularBuffer/CircularBuffer.cpp
// Web Address:   http://sig.sapp.org/src/sigBase/CircularBuffer.cpp
// Syntax:        C++
//
// Description:   A Circular buffer designed to handle MIDI input,
//                but able to store any type of object.  Elements
//                can be read out of the buffer in two ways. 
//                (1) from a read pointer which extracts the
//                elements in order by following the write pointer,
//                and (2) from an index operator related to the
//                write pointer's location, for example,
//                object[0] is the last value written into the
//                buffer and object[-1] (or object[1]) is the
//                item written just before that.
//              
//

#ifndef _CIRCULARBUFFER_CPP_INCLUDED
#define _CIRCULARBUFFER_CPP_INCLUDED

#include "CircularBuffer.h"
#include <stdlib.h>
#include <iostream>


//////////////////////////////
//
// CircularBuffer::CircularBuffer -- Constructor.
//

template<class type>
CircularBuffer<type>::CircularBuffer(void) {
   size = 0;
   buffer = NULL;
   reset();
}


template<class type>
CircularBuffer<type>::CircularBuffer(int maxElements) {
   if (maxElements < 0) {
      std::cerr << "Error: cannot have a negative number of elements: " 
           << maxElements << std::endl;
      exit(1);
   }
   if (maxElements == 0) {
      size = 0;
      buffer = NULL;
      reset();
   } else {
      size = maxElements;
      buffer = new type[maxElements];
      reset();
   }
}


template<class type>
CircularBuffer<type>::CircularBuffer(const CircularBuffer<type>& anotherBuffer) {
   size = anotherBuffer.size;
   if (getSize() == 0) {
      buffer = NULL;
      reset();
   } else {
      buffer = new type[getSize()];
      writeIndex = anotherBuffer.writeIndex;
      readIndex = anotherBuffer.readIndex;
      itemCount = anotherBuffer.itemCount;
      for (int i=0; i<getSize(); i++) {
         buffer[i] = anotherBuffer.buffer[i];
      }
   }
}



//////////////////////////////
//
// CircularBuffer::~CircularBuffer -- Destructor.
//    deallocates buffer memory.
//

template<class type>
CircularBuffer<type>::~CircularBuffer() {
   if (buffer != NULL) {
      delete [] buffer;
   }
}



//////////////////////////////
//
// CircularBuffer::capacity -- returns the number of items which
//    can be added to the buffer.  Returns a positive number
//    if the buffer has empty locations available.  Returns 0 if the
//    buffer is 100% full.  Returns a negative number if the
//    buffer has overflowed.

template<class type>
int CircularBuffer<type>::capacity(void) const {
   return getSize() - getCount();
}



//////////////////////////////
//
// CircularBuffer::extract -- reads the next value from the buffer.
//

template<class type>
type CircularBuffer<type>::extract(void) {
   itemCount--;
   if (itemCount < 0) {
      std::cerr << "Error: no elements in buffer to extract." << std::endl;
      exit(1);
   }
   increment(readIndex);
   return buffer[readIndex];
}



//////////////////////////////
//
// CircularBuffer::getCount -- returns the number of elements
//    between the write index and the read index.
//

template<class type>
int CircularBuffer<type>::getCount(void) const {
   return itemCount;
}



//////////////////////////////
//
// CircularBuffer::getSize -- returns the allocated size of the buffer.
//

template<class type>  
int CircularBuffer<type>::getSize(void) const {
   return size;
}



//////////////////////////////
//
// CircularBuffer::insert -- add an element to the circular buffer
//

template<class type>
void CircularBuffer<type>::insert(const type& anItem) {
   itemCount++;
   increment(writeIndex);
   buffer[writeIndex] = anItem;
}



//////////////////////////////
//
// CircularBuffer::operator[] -- access an element relative to the
//    currently written element
//

template<class type>
type& CircularBuffer<type>::operator[](int index) {
   if (buffer == NULL) {
      std::cerr << "Error: buffer has no allocated space" << std::endl;
      exit(1);
   }
   int realIndex = (index < 0) ? -index : index;
   if (realIndex >= getSize()) {
      std::cerr << "Error:   Invalid access: " << realIndex << ", maximum is "
           << getSize()-1 << std::endl;
      exit(1);
   }
   realIndex = writeIndex - realIndex;

   // should need to go through this loop a max of one time:
   while (realIndex < 0) {
      realIndex += getSize();
   }
   
   return buffer[realIndex];
}



//////////////////////////////
//
// CircularBuffer::read -- an alias for the extract function.
//

template<class type>
type CircularBuffer<type>::read(void) {
   return extract();
}



//////////////////////////////
//
// CircularBuffer::reset -- throws out all previous data and
//    sets the read/write/count to initial values.  The size
//    data variable must be valid before this function is
//    called.
//

template<class type>
void CircularBuffer<type>::reset(void) {
   readIndex = writeIndex = getSize() - 1;
   itemCount = 0;
}
 
  

//////////////////////////////
//
// CircularBuffer::setSize -- warning: will throw out all previous data 
//    stored in buffer.
//

template<class type>
void CircularBuffer<type>::setSize(int aSize) {
   if (aSize < 0) {
      std::cerr << "Error: cannot have a negative buffer size: " << aSize << std::endl;
      exit(1);
   }
   if (buffer != NULL) {
      delete [] buffer;
   }

   if (aSize == 0) {
      size = aSize;
      buffer = NULL;
      reset();
   } else {
      size = aSize;
      buffer = new type[aSize];
      reset();
   }
}   



//////////////////////////////
//
// CircularBuffer::write --  an alias for the insert function.
//

template<class type>
void CircularBuffer<type>::write(const type& anElement) {
   write(anElement);
}


///////////////////////////////////////////////////////////////////////////
//
// private functions
//

//////////////////////////////
//
// CircularBuffer::increment -- adds one to specified index and
//    will automatically wrap the index when it gets too large.
//

template<class type>
void CircularBuffer<type>::increment(int& index) {
   index++;
   if (index >= getSize()) {
      index = 0;
   }
}


#endif  /* _CIRCULARBUFFER_CPP_INCLUDED */



// md5sum:	31b2e8d6efe7398a12ddb0a1b5680ca2  - CircularBuffer.cpp =css= 20030102