About Store Forum Documentation Contact



Post Reply 
EE containers & C++11 ranged for loops
Author Message
miro@3dea Offline
Member

Post: #1
EE containers & C++11 ranged for loops
If anyone miss C++11 functionality with EE containers, here is a little helper class that can be used:
Code:
template <typename T, template <typename> class Container>
struct ArrayIter
{
public:
  ArrayIter(Ptr array, Int idx) :
     _array(array),
     _idx(idx)
  {}

  bool operator!=(const ArrayIter & right) const { return _idx != right._idx; }
  const ArrayIter<T, Container> & operator++() { ++_idx; return *this; }

  T & operator*() const;

protected:
  Ptr _array;
  Int  _idx;
};


template <typename T, template <typename> class Ancestor>
struct Array : public Ancestor<T>
{
  typedef ArrayIter<T, Ancestor> Iterator;

  const Iterator begin() const { return Iterator(this, 0); }
        Iterator begin()       { return Iterator(this, 0); }
  const Iterator end()   const { return Iterator(this, elms()); }
        Iterator end()         { return Iterator(this, elms()); }
};


template <typename T, template <typename> class Container>
T & ArrayIter<T, Container>::operator*() const
{
  return static_cast<Container<T> *>(_array)->operator[](_idx);
}

so you can use it like this:
Code:
typedef Array<Player, EE::Game::ObjMemx> ObjPlayers;
ObjPlayers players;
for (auto & player : players) player.drawPrepare();

C++11 is supported (not yet fully though) with VS2012. We are using it for a couple of months already for developing EE and I strongly encourage you to do so if you would like it too..
10-18-2012 09:40 AM
Find all posts by this user Quote this message in a reply
Zervox Offline
Member

Post: #2
RE: EE containers & C++11 ranged for loops
Great snippet. : )
10-18-2012 10:55 AM
Find all posts by this user Quote this message in a reply
yvanvds Offline
Member

Post: #3
RE: EE containers & C++11 ranged for loops
I vote +1
10-19-2012 11:52 PM
Find all posts by this user Quote this message in a reply
Post Reply