SofaPirate / Plaquette

Object-oriented Arduino library for creative physical computing
https://sofapirate.github.io/Plaquette/
GNU General Public License v3.0
15 stars 4 forks source link

Implement support for dynamic arrays of Node/Units and smart pointers #91

Open sofian opened 10 months ago

sofian commented 10 months ago

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.

int n = 10;

NodeArray leds(n);
NodeArray oscillators(n);

void begin() {
  for (int i=0; i<n; i++) {
    leds[i] = AnalogOut(i); // xxx this is just pseudocode as it currently will not work
    oscillators[i] = new SineOsc(random(2, 10)); // xxx this probably makes more sense but involves dynamic allocation
    // alternative:
    leds[i].pin(i);
    oscillators[i].period(random(2, 10)); // xxx this probably makes more sense but involves dynamic allocation
  }
}

void step() {
  for (int i=0; i<n; i++) {
    oscillators[i] >> leds[i];
  }
  // or even could be instead: 
  oscillators >> leds;
}

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.

sofian commented 10 months ago

This could possibly be an extra Plaquette-dependent library.

sofian commented 9 months ago

Check this out: https://github.com/braydenanderson2014/C-Arduino-Libraries/tree/main/lib/ArrayList

sofian commented 9 months ago

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]);
    }
};