Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add copyToArray() method as discussed in issue #52 #84

Merged
merged 3 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CircularBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ template<typename T, size_t S, typename IT = typename Helper::Index<(S <= UINT8_
void inline debugFn(Print* out, void (*printFunction)(Print*, T));
#endif

void copyToArray(T* out) const;

template<typename R>
void copyToArray(R* out, R (&convert)(const T&)) const;

private:
T buffer[S];
T *head;
Expand Down
25 changes: 25 additions & 0 deletions CircularBuffer.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,31 @@ void inline CircularBuffer<T,S,IT>::clear() {
count = 0;
}

template<typename T, size_t S, typename IT>
void inline CircularBuffer<T,S,IT>::copyToArray(T* out) const {
const T* bufEnd = buffer + capacity;
const T* outEnd = out + count;
for (const T* t = head; t < bufEnd && out < outEnd; t++, out++) {
*out = *t;
}
for (const T* t = buffer; t <= tail && out < outEnd; t++, out++) {
*out = *t;
}
}

template<typename T, size_t S, typename IT>
template<typename R>
void inline CircularBuffer<T,S,IT>::copyToArray(R* out, R (&convert)(const T&)) const {
const T* bufEnd = buffer + capacity;
const R* outEnd = out + count;
for (const T* t = head; t < bufEnd && out < outEnd; t++, out++) {
*out = convert(*t);
}
for (const T* t = buffer; t <= tail && out < outEnd; t++, out++) {
*out = convert(*t);
}
}

#ifdef CIRCULAR_BUFFER_DEBUG
#include <string.h>
template<typename T, size_t S, typename IT>
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ buffer[15]; // ['c','d','e'] returned value is unpredictable
* `available()` returns the number of elements that can be added before saturating the buffer
* `capacity()` returns the number of elements the buffer can store, for completeness only as it's user-defined and never changes **REMOVED** from `1.3.0` replaced by the read-only member variable `capacity`
* `clear()` resets the whole buffer to its initial state (pay attention though, if you had dynamically allocated objects in your buffer, memory used by such object is *not* released: iterate over the buffer contents and release object accordingly to their allocation method)
* `copyToArray(array)` copies the contents of the circular buffer to a standard array `array`. The array must be large enough to hold all the elements in the buffer.

## Advanced Usage

Expand Down Expand Up @@ -237,6 +238,7 @@ Multiple examples are available in the `examples` folder of the library:
* [Stack.ino](https://github.com/rlogiacco/CircularBuffer/blob/master/examples/Stack/Stack.ino) on the other end shows how to use the library to represent a LIFO data structure
* [Struct.ino](https://github.com/rlogiacco/CircularBuffer/blob/master/examples/Struct/Struct.ino) answers to the question _can this library store structured data?_
* [Interrupts.ino](https://github.com/rlogiacco/CircularBuffer/blob/master/examples/Interrupts/Interrupts.ino) demonstrates the use of the library in interrupt driven code
* [Arrays.ino]() demonstrates the use of the copyToArray() function.

## Limitations

Expand Down
34 changes: 34 additions & 0 deletions examples/Arrays/Arrays.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "CircularBuffer.h"

// Create a circular buffer with a capacity of 10
const int bufferSize = 10;
CircularBuffer<int, bufferSize> buffer;

void setup() {
// start serial
Serial.begin(9600);
while(!Serial); // Wait for the serial port to come online

// Add some values to the buffer
for (int i = 0; i < 10; ++i) {
buffer.push(2*i);
}

// Create an array to hold the buffer's contents
int array[bufferSize];

// Copy the buffer's contents to the array
buffer.copyToArray(array);

// Now array contains the same values as the buffer
for (int i = 0; i < bufferSize; ++i) {
Serial.print("Buffer: ");
Serial.print(buffer[i]);
Serial.print(", Array: ");
Serial.println(array[i]);
}
}

void loop() {
// Nothing to do here
}