Open sofian opened 9 months ago
This could possibly be an extra Plaquette-dependent library.
I started to implement a class but there are problems, putting it here for later:
class UnitList : public ArrayList<Unit*> {
public:
UnitList(size_t initialCapacity = 8) : ArrayList<Unit*>(initialCapacity) {}
~UnitList() {
Serial.println("UnitList destructor");
for (size_t i=0; i<size(); i++) {
Serial.println("Deleting unit");
Serial.println((unsigned long)array[i], HEX);
delete array[i];
}
// delete[] array;
}
/**
* @brief Retrieves the item at a specific index in the ArrayList.
*
* This operator retrieves the item at the specified index in the ArrayList. If the index is less than the count of items in the ArrayList,
* it returns the item at the index. If the index is out of bounds, it returns a default-constructed instance of type T.
*
* @param index The index of the item to retrieve.
* @return The item at the specified index, or a default-constructed instance of type T if the index is out of bounds.
*/
Node& operator[](size_t index) const {
index = constrain(index, 0, count-1);
return *((Node*)array[index]);
}
};
Currently we are forced to declare each Plaquette object individually, therefore preventing us from using the power of arrays. For example, imagine I would like to control 10 LEDs individually using 10 oscillators.
In order to implement this properly and manage proper data allocation, there needs to be a deeper thinking into how to allocate and free memory. This includes the need to implement some kind of smart pointers.
Since it is a bit "advanced" and since we already have static arrays of Nodes/Units in Plaquette, it could also perhaps be included in a separate library.
Another note: dynamic allocation is not that bad on Arduino platforms if you only use the new operator but do not need to free the memory later (ie. no delete) which is usually the case with Plaquette.