Commanders
Arduino buttons/bus library
Chain.hpp
1 //-------------------------------------------------------------------
2 #ifndef __CMDRSChain_H__
3 #define __CMDRSChain_H__
4 //-------------------------------------------------------------------
5 
7 template<class T> class CMDRSCHAINEDLISTITEM
8 {
9 public:
11  T *Obj;
14 
16  inline CMDRSCHAINEDLISTITEM<T>() { this->pNext = NULL; }
17 };
18 
33 template<class T> class CMDRSCHAINEDLIST
34 {
35 public:
40 
42  inline CMDRSCHAINEDLIST() { this->pFirst = NULL; this->pCurrentItem = NULL; }
46  void AddItem(T *input);
49  void NextCurrent();
50 };
51 
58 #define CMDRSCHAIN_ENUMERATE(T, list, function) CMDRSCHAINEDLISTITEM<T> *pCurr = list.pFirst; while (pCurr != NULL) { func(pCurr->Obj); pCurr = pCurr->pNext; }
59 
60 // This function appends element into chain.
61 template<class T> void CMDRSCHAINEDLIST<T>::AddItem(T *t)
62 {
63  CMDRSCHAINEDLISTITEM<T> *pCurr = this->pFirst;
64 
65  if (pCurr == NULL)
66  {
67  this->pFirst = new CMDRSCHAINEDLISTITEM<T>();
68  this->pCurrentItem = this->pFirst;
69  pCurr = this->pFirst;
70  }
71  else
72  {
73  while (pCurr->pNext != NULL)
74  pCurr = pCurr->pNext;
75 
76  pCurr->pNext = new CMDRSCHAINEDLISTITEM<T>();
77  pCurr = pCurr->pNext;
78  }
79 
80  pCurr->pNext = NULL;
81  pCurr->Obj = t;
82 }
83 
84 // This function move the current item to the next in the chain.
85 template<class T> void CMDRSCHAINEDLIST<T>::NextCurrent()
86 {
87  if (this->pFirst == NULL)
88  return;
89 
90  this->pCurrentItem = this->pCurrentItem->pNext;
91 
92  if (this->pCurrentItem == NULL)
93  this->pCurrentItem = this->pFirst;
94 
95  return;
96 }
97 #endif
void NextCurrent()
Definition: Chain.hpp:85
void AddItem(T *input)
Definition: Chain.hpp:61
CMDRSCHAINEDLISTITEM< T > * pCurrentItem
Definition: Chain.hpp:39
CMDRSCHAINEDLISTITEM< T > * pFirst
Definition: Chain.hpp:37
CMDRSCHAINEDLISTITEM * pNext
Definition: Chain.hpp:13