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

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

Open
sofian opened this issue Jan 28, 2024 · 3 comments
Open
Labels

Comments

@sofian
Copy link
Collaborator

sofian commented Jan 28, 2024

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
Copy link
Collaborator Author

sofian commented Jan 28, 2024

This could possibly be an extra Plaquette-dependent library.

@sofian
Copy link
Collaborator Author

sofian commented Feb 26, 2024

@sofian 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
@sofian
Copy link
Collaborator Author

sofian commented 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
Labels
Projects
None yet
Development

No branches or pull requests

1 participant