-
Notifications
You must be signed in to change notification settings - Fork 4
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
Implement support for dynamic arrays of Node/Units and smart pointers #91
Labels
Comments
This could possibly be an extra Plaquette-dependent library. |
sofian
added a commit
that referenced
this issue
Feb 27, 2024
sofian
changed the title
How to deal with arrays/lists/vectors of Nodes?
Implement support for dynamic arrays of Node/Units and smart pointers
Feb 28, 2024
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]);
}
};
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
The text was updated successfully, but these errors were encountered: