Thank you for you detailed response and for the code.
Out of curiosity, why a struct instead of a class, and why make everything public ? Just asking...
I modified it a bit, included addItem.
MPLArray (modified)
#ifndef _MPL_ARRAY_H
#define _MPL_ARRAY_H
#include <stddef.h>
#include <stdint.h>
template< typename _Type, uint16_t _MaxCount >
class MPLArray{
public:
MPLArray() {
_count = 0;
};
void addItem(_Type& newItem, bool* itemAdded = NULL, uint16_t* itemIdx = NULL) {
if (!isFull()) {
t_Data[_count] = newItem;
if (itemAdded != NULL) {
*itemAdded = true;
}
if (itemIdx!= NULL) {
*itemIdx = _count;
}
_count++;
}
else {
*itemAdded = false;
}
};
bool isFull() const {
return _count < _MaxCount;
};
uint16_t count() const {
return _count;
};
uint16_t maxCount() const {
return _MaxCount;
};
uint16_t size() const {
return sizeof( _Type ) * _MaxCount;
};
_Type& operator[]( uint16_t u_Index ) {
return this->t_Data[ u_Index ];
};
operator _Type*() {
return this->t_Data;
};
operator const _Type*() const {
return this->t_Data;
};
private:
_Type t_Data[ _MaxCount ];
uint16_t _count;
};
#endif
Boards.h
#ifndef _BOARDS_H
#define _BOARDS_H
#include "MPLArray.h"
template <int MAX_BOARD_CNT>
class Boards : public MPLArray<int, MAX_BOARD_CNT> {
public:
Boards() {};
void addBoard(int boardId) {
MPLArray<int, MAX_BOARD_CNT>::addItem(boardId);
};
// other Boards-specific code...
};
#endif
Main sketch:
#include "Boards.h"
Boards<100> boards;
int freeRam () {
extern int __heap_start, *__brkval;
int v;
return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
};
void setup() {
Serial.begin(115200);
Serial.println(boards.maxCount());
Serial.println(boards.count());
boards.addBoard(5);
boards.addBoard(10);
Serial.println(boards.count());
Serial.println(freeRam());
}
void loop() {
}
Count = 100
Code size: 2704
Freeram: 1578