Arduino Micro Controllers

The Partridge Family were neither partridges nor a family. Discuss.
albinopapa
Posts: 3790
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Arduino Micro Controllers

Post by albinopapa » August 6th, 2019, 4:09 am

You don't need the m_count since the count is part of Array's type, therefore you also don't need the constructor Array(). The size() function can just return the template parameter count or if supported, you can make a static constexpr m_count = count and return the constexpr m_count. I'd also make the size() function constexpr if supported by the Arduino compiler as well and maybe noexcept lol.

Code: Select all

template<typename T, unsigned int count>
class Array
{
   static constexpr unsigned int m_count = count;
public:
   // convenience functions
   constexpr T& operator[]( unsigned int _index )noexcept{
      assert( _index < m_count && "Index out of range" );
      return elements[ _index ];
   }
   constexpr T const& operator[]( unsigned int _index )const noexcept{
      assert( _index < m_count && "Index out of range" );
      return elements[ _index ];
   }
   constexpr unsigned int size()const noexcept {return m_count;}
   T elements[ count ];
};
At least this way you can use the Array class like an array:
Array<int, 10> ints_A;
Array<int, 10> ints_B;

Old way: ints_A.elements[ 0 ] = 3;
New way: ints_B[ 0 ] = 3;
Plus, this way you get some debugging help if you try accessing out of bounds.

Now you just need to make an ArrayIterator, ArrayConstIterator, Array::begin() and Array::end() so you can use range based for loops.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

MrGodin
Posts: 719
Joined: November 30th, 2013, 7:40 pm
Location: Merville, British Columbia Canada

Re: Arduino Micro Controllers

Post by MrGodin » August 7th, 2019, 1:04 am

Thanks buddy, I'll use this ;)
Curiosity killed the cat, satisfaction brought him back

MrGodin
Posts: 719
Joined: November 30th, 2013, 7:40 pm
Location: Merville, British Columbia Canada

Re: Arduino Micro Controllers

Post by MrGodin » August 7th, 2019, 1:19 am

So i left everything connected to my ATX power supply then went to work. I came back and the fan was running (as expected) and all is well. Once the code is uploaded to the Arduino It'll run so long as it has power. If the power goes out ... the code still resides in the Arduino chip. Cool or what ;)..
Curiosity killed the cat, satisfaction brought him back

MrGodin
Posts: 719
Joined: November 30th, 2013, 7:40 pm
Location: Merville, British Columbia Canada

Re: Arduino Micro Controllers

Post by MrGodin » August 7th, 2019, 1:25 am

This compiled on the Arduino IDE

Code: Select all

template<typename T, unsigned int count>
class Array
{
   static constexpr unsigned int m_count = count;
public:
   // convenience functions
   constexpr T& operator[]( unsigned int _index )noexcept{
      assert( _index < m_count && "Index out of range" );
      return elements[ _index ];
   }
  
   
   constexpr unsigned int size()const noexcept {return m_count;}
   T elements[ count ];
};
Curiosity killed the cat, satisfaction brought him back

albinopapa
Posts: 3790
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Arduino Micro Controllers

Post by albinopapa » August 7th, 2019, 6:23 am

Cool, thanks for sharing.

I like writing library stuff like this, so if you ever want a container with iterators, let me know if you don't feel like writing it out yourself.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Post Reply