Pages: [1]   Go Down
Author Topic: container for Objects like c++ vector?  (Read 8183 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 16
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey All,

In the avr version of c++, is there a way to have a list container to store Objects?  Say I have and RGBLed library for controlling RGB leds declared like this:

RGBLed led1(redPin,greenPin,bluePin);

I want to have a list of these.  In regular c++ I would use a vector declared like this:

vector<RGBLed> leds;

..then I could iterate though them and select certain elements in the list, etc.

Can I do something like this with avr c++ or Arduino library?
« Last Edit: November 07, 2010, 12:38:30 pm by fkeymo » Logged

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 13
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You'll need to use an array.
Remember the memory constraint when creating arrays of object.
Code:
//change class for your class in change the initialization list
class arr[] = {
  class(1,2),
  class(1,3)
};
Logged

Netherlands
Offline Offline
Newbie
*
Karma: 0
Posts: 30
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I realise this may be too late to help the original poster, but since searching for 'vector' (as I did) brings you here, I'll post this for others:

It's relatively simple to write your own Vector class if you need basic functionality (no iterators, removing items once added etc.). The following class allows you to define your own Vector<AnyType>, push_back() objects and recall them with the [] notation.

Code:
// Minimal class to replace std::vector
template<typename Data>
class Vector {
   size_t d_size; // Stores no. of actually stored objects
   size_t d_capacity; // Stores allocated capacity
   Data *d_data; // Stores data
   public:
     Vector() : d_size(0), d_capacity(0), d_data(0) {}; // Default constructor
     Vector(Vector const &other) : d_size(other.d_size), d_capacity(other.d_capacity), d_data(0) { d_data = (Data *)malloc(d_capacity*sizeof(Data)); memcpy(d_data, other.d_data, d_size*sizeof(Data)); }; // Copy constuctor
     ~Vector() { free(d_data); }; // Destructor
     Vector &operator=(Vector const &other) { free(d_data); d_size = other.d_size; d_capacity = other.d_capacity; d_data = (Data *)malloc(d_capacity*sizeof(Data)); memcpy(d_data, other.d_data, d_size*sizeof(Data)); return *this; }; // Needed for memory management
     void push_back(Data const &x) { if (d_capacity == d_size) resize(); d_data[d_size++] = x; }; // Adds new value. If needed, allocates more space
     size_t size() const { return d_size; }; // Size getter
     Data const &operator[](size_t idx) const { return d_data[idx]; }; // Const getter
     Data &operator[](size_t idx) { return d_data[idx]; }; // Changeable getter
   private:
     void resize() { d_capacity = d_capacity ? d_capacity*2 : 1; Data *newdata = (Data *)malloc(d_capacity*sizeof(Data)); memcpy(newdata, d_data, d_size * sizeof(Data)); free(d_data); d_data = newdata; };// Allocates double the old space
};

Note that due to memory constraints it would be unwise to store large objects. Also note that due to differences between free and delete[], the destructor of objects stored in a Vector might not be called. Be careful when storing objects which contain allocated memory.

An example of usage is given below. This loops through the numbers 2 to 100, storing all prime numbers in the Vector "prime" and meanwhile having the led-light (on the Uno) blink them all out (so first it blinks twice, then 3 times, etc).

Code:
// Example of Vector usage: store prime numbers and blink LED
void setup() {                
  // initialize the digital pin as an output.
  // Pin 13 has an LED connected on most Arduino boards:
  pinMode(13, OUTPUT);    
}

void blink(size_t n, size_t d = 1000) {
  for (size_t idx = 0; idx < n; ++idx) {
    digitalWrite(13, HIGH);
    delay(d/2);
    digitalWrite(13, LOW);
    delay(d/2);
  }
}

void loop() {
  Vector<size_t> prime;
  for (size_t num = 2; num < 100; ++num) {
    boolean isPrime = true;
    for (size_t idx = 0; idx < prime.size(); ++idx)
      if (!(num % prime[idx])) {
        isPrime = false;
        break;
      }
    if (!isPrime)
      continue;
    prime.push_back(num);
    blink(num);
    delay(1000);
  }
}

Edit: Added memory management for copy constructor and assignment-operator. We wouldn't want that to fail...
« Last Edit: December 25, 2010, 04:33:21 am by Gido » Logged

Pages: [1]   Go Up
Jump to: