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:
42 
44  inline CMDRSCHAINEDLIST() { this->pFirst = NULL; this->pCurrentItem = NULL; }
48  void AddItem(T *inpt);
51  void NextCurrent();
52 };
53 
60 #define CMDRSCHAIN_ENUMERATE(T, list, func) CMDRSCHAINEDLISTITEM<T> *pCurr = list.pFirst; while (pCurr != NULL) { func(pCurr->Obj); pCurr = pCurr->pNext; }
61 
62 // This function appends element into chain.
63 template<class T> void CMDRSCHAINEDLIST<T>::AddItem(T *t)
64 {
65  CMDRSCHAINEDLISTITEM<T> *pCurr = this->pFirst;
66 
67  if (pCurr == NULL)
68  {
69  this->pFirst = new CMDRSCHAINEDLISTITEM<T>();
70  this->pCurrentItem = this->pFirst;
71  pCurr = this->pFirst;
72  }
73  else
74  {
75  while (pCurr->pNext != NULL)
76  pCurr = pCurr->pNext;
77 
78  pCurr->pNext = new CMDRSCHAINEDLISTITEM<T>();
79  pCurr = pCurr->pNext;
80  }
81 
82  pCurr->pNext = NULL;
83  pCurr->Obj = t;
84 }
85 
86 // This function move the current item to the next in the chain.
87 template<class T> void CMDRSCHAINEDLIST<T>::NextCurrent()
88 {
89  if (this->pFirst == NULL)
90  return;
91 
92  this->pCurrentItem = this->pCurrentItem->pNext;
93 
94  if (this->pCurrentItem == NULL)
95  this->pCurrentItem = this->pFirst;
96 
97  return;
98 }
99 #endif
void NextCurrent()
Definition: Chain.hpp:87
void AddItem(T *inpt)
Definition: Chain.hpp:63
CMDRSCHAINEDLISTITEM< T > * pCurrentItem
Definition: Chain.hpp:41
CMDRSCHAINEDLISTITEM< T > * pFirst
Definition: Chain.hpp:38
CMDRSCHAINEDLISTITEM * pNext
Definition: Chain.hpp:13