tcMenu
MenuIterator.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 https://www.thecoderscorner.com (Nutricherry LTD).
3  * This product is licensed under an Apache license, see the LICENSE file in the top-level directory.
4  */
5 
6 #include <PlatformDetermination.h>
7 
14 #ifndef _MENUITERATOR_H
15 #define _MENUITERATOR_H
16 
17 #ifndef MAX_MENU_DEPTH
18 # define MAX_MENU_DEPTH 4
19 #endif // MAX_MENU_DEPTH
20 
21 // forward reference of menu item
22 class MenuItem;
23 
27 typedef void (*MenuVisitorFn)(MenuItem* item);
28 
39 
48 inline MenuItem* getParentRoot(MenuItem* current) { return getParentRootAndVisit(current, nullptr); }
49 
56 
62 MenuItem* getMenuItemById(menuid_t id);
63 
70 MenuItem* getItemAtPosition(MenuItem* root, uint8_t pos);
71 
78 
85 uint8_t itemCount(MenuItem* item, bool includeNonVisble = false);
86 
87 
93 public:
99  virtual bool matches(MenuItem* item)=0;
100 };
101 
106 private:
107  uint8_t remoteNo;
108 public:
113  explicit RemoteNoMenuItemPredicate(int remoteNo) { this->remoteNo = remoteNo; }
114 
120  bool matches(MenuItem* item) override;
121 
126  void setRemoteNo(uint8_t newRemoteNum) { remoteNo = newRemoteNum; }
127 };
128 
129 // the modes that can be passed to the type predicate
130 #define TM_REGULAR 0
131 #define TM_INVERTED 1
132 #define TM_REGULAR_LOCAL_ONLY 8
133 #define TM_INVERTED_LOCAL_ONLY 9
134 #define TM_EXTRA_INCLUDE_SUBMENUS 16
135 #define TM_BIT_INVERT 0U
136 #define TM_BIT_LOCAL_ONLY 3U
137 #define TM_BIT_INCLUDE_SUBMENU 4U
138 
148 private:
149  MenuType filterType;
150  uint8_t mode;
151 public:
156  MenuItemTypePredicate(MenuType filterType, uint8_t mode = 0) {
157  this->filterType = filterType;
158  this->mode = mode;
159  }
160 
166  bool matches(MenuItem* item) override;
167 };
168 
176 private:
177  MenuItem* currentItem;
178  MenuItemPredicate* predicate;
179  MenuItem* parentItems[MAX_MENU_DEPTH];
180  uint8_t level;
181  bool processingSubMenu;
182 public:
183  MenuItemIterator() {
184  reset();
185  predicate = nullptr;
186  }
187 
192  void setPredicate(MenuItemPredicate* predicate) { this->predicate = predicate; }
193 
197  void reset();
198 
204  MenuItem* nextItem();
205 
213  MenuItem* currentParent();
214 };
215 
216 #endif
Definition: MenuItems.h:329
virtual bool matches(MenuItem *item)=0
Definition: MenuIterator.h:175
MenuItem * getSubMenuFor(MenuItem *current)
Definition: MenuIterator.cpp:182
Definition: MenuIterator.h:147
int offsetOfCurrentActive(MenuItem *root)
Definition: MenuIterator.cpp:158
MenuItem * getItemAtPosition(MenuItem *root, uint8_t pos)
Definition: MenuIterator.cpp:152
RemoteNoMenuItemPredicate(int remoteNo)
Definition: MenuIterator.h:113
MenuType
Definition: MenuItems.h:242
Definition: MenuIterator.h:92
uint8_t itemCount(MenuItem *item, bool includeNonVisble=false)
Definition: MenuIterator.cpp:164
void(* MenuVisitorFn)(MenuItem *item)
Definition: MenuIterator.h:27
MenuItem * getMenuItemById(menuid_t id)
Definition: MenuIterator.cpp:69
Definition: MenuIterator.h:105
MenuItemTypePredicate(MenuType filterType, uint8_t mode=0)
Definition: MenuIterator.h:156
MenuItem * getParentRootAndVisit(MenuItem *current, MenuVisitorFn visitor)
Definition: MenuIterator.cpp:42
MenuItem * getParentRoot(MenuItem *current)
Definition: MenuIterator.h:48
void setRemoteNo(uint8_t newRemoteNum)
Definition: MenuIterator.h:126
void setPredicate(MenuItemPredicate *predicate)
Definition: MenuIterator.h:192