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(int 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 };
122 
123 // the modes that can be passed to the type predicate
124 #define TM_REGULAR 0
125 #define TM_INVERTED 1
126 #define TM_REGULAR_LOCAL_ONLY 8
127 #define TM_INVERTED_LOCAL_ONLY 9
128 #define TM_EXTRA_INCLUDE_SUBMENUS 16
129 #define TM_BIT_INVERT 0U
130 #define TM_BIT_LOCAL_ONLY 3U
131 #define TM_BIT_INCLUDE_SUBMENU 4U
132 
142 private:
143  MenuType filterType;
144  uint8_t mode;
145 public:
150  MenuItemTypePredicate(MenuType filterType, uint8_t mode = 0) {
151  this->filterType = filterType;
152  this->mode = mode;
153  }
154 
160  bool matches(MenuItem* item) override;
161 };
162 
170 private:
171  MenuItem* currentItem;
172  MenuItemPredicate* predicate;
173  MenuItem* parentItems[MAX_MENU_DEPTH];
174  uint8_t level;
175  bool processingSubMenu;
176 public:
177  MenuItemIterator() {
178  reset();
179  predicate = nullptr;
180  }
181 
186  void setPredicate(MenuItemPredicate* predicate) { this->predicate = predicate; }
187 
191  void reset();
192 
198  MenuItem* nextItem();
199 
207  MenuItem* currentParent();
208 };
209 
210 #endif
Definition: MenuItems.h:324
virtual bool matches(MenuItem *item)=0
Definition: MenuIterator.h:169
MenuItem * getSubMenuFor(MenuItem *current)
Definition: MenuIterator.cpp:182
Definition: MenuIterator.h:141
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:237
Definition: MenuIterator.h:92
uint8_t itemCount(MenuItem *item, bool includeNonVisble=false)
Definition: MenuIterator.cpp:164
void(* MenuVisitorFn)(MenuItem *item)
Definition: MenuIterator.h:27
Definition: MenuIterator.h:105
MenuItemTypePredicate(MenuType filterType, uint8_t mode=0)
Definition: MenuIterator.h:150
MenuItem * getParentRootAndVisit(MenuItem *current, MenuVisitorFn visitor)
Definition: MenuIterator.cpp:42
MenuItem * getParentRoot(MenuItem *current)
Definition: MenuIterator.h:48
void setPredicate(MenuItemPredicate *predicate)
Definition: MenuIterator.h:186
MenuItem * getMenuItemById(int id)
Definition: MenuIterator.cpp:69