tcMenu
MenuIterator.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 https://www.thecoderscorner.com (Dave Cherry).
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 
13 #ifndef _MENUITERATOR_H
14 #define _MENUITERATOR_H
15 
16 #ifndef MAX_MENU_DEPTH
17 # define MAX_MENU_DEPTH 4
18 #endif // MAX_MENU_DEPTH
19 
20 // forward reference of menu item
21 class MenuItem;
22 
26 typedef void (*MenuVisitorFn)(MenuItem* item);
27 
38 
47 inline MenuItem* getParentRoot(MenuItem* current) { return getParentRootAndVisit(current, nullptr); }
48 
55 
61 MenuItem* getMenuItemById(menuid_t id);
62 
69 
75 int offsetOfItem(MenuItem* itemToFind);
76 
83 uint8_t itemCount(MenuItem* item, bool includeNonVisble = false);
84 
85 
91 public:
97  virtual bool matches(MenuItem* item)=0;
98 };
99 
104 private:
105  uint8_t remoteNo;
106 public:
111  explicit RemoteNoMenuItemPredicate(int remoteNo) { this->remoteNo = remoteNo; }
112 
118  bool matches(MenuItem* item) override;
119 
124  void setRemoteNo(uint8_t newRemoteNum) { remoteNo = newRemoteNum; }
125 };
126 
127 // the modes that can be passed to the type predicate
128 #define TM_REGULAR 0
129 #define TM_INVERTED 1
130 #define TM_REGULAR_LOCAL_ONLY 8
131 #define TM_INVERTED_LOCAL_ONLY 9
132 #define TM_EXTRA_INCLUDE_SUBMENUS 16
133 #define TM_BIT_INVERT 0U
134 #define TM_BIT_LOCAL_ONLY 3U
135 #define TM_BIT_INCLUDE_SUBMENU 4U
136 
146 private:
147  MenuType filterType;
148  uint8_t mode;
149 public:
154  MenuItemTypePredicate(MenuType filterType, uint8_t mode = 0) {
155  this->filterType = filterType;
156  this->mode = mode;
157  }
158 
164  bool matches(MenuItem* item) override;
165 };
166 
174 private:
175  MenuItem* currentItem;
176  MenuItemPredicate* predicate;
177  MenuItem* parentItems[MAX_MENU_DEPTH];
178  uint8_t level;
179  bool processingSubMenu;
180 public:
181  MenuItemIterator() {
182  reset();
183  predicate = nullptr;
184  }
185 
190  void setPredicate(MenuItemPredicate* predicate) { this->predicate = predicate; }
191 
195  void reset();
196 
202  MenuItem* nextItem();
203 
212 };
213 
214 #endif
MenuType
Definition: MenuItems.h:238
int offsetOfItem(MenuItem *itemToFind)
Definition: MenuIterator.cpp:189
void(* MenuVisitorFn)(MenuItem *item)
Definition: MenuIterator.h:26
MenuItem * getSubMenuFor(MenuItem *current)
Definition: MenuIterator.cpp:219
MenuItem * getParentRootAndVisit(MenuItem *current, MenuVisitorFn visitor)
Definition: MenuIterator.cpp:48
uint8_t itemCount(MenuItem *item, bool includeNonVisble=false)
Definition: MenuIterator.cpp:201
int offsetOfCurrentActive(MenuItem *root)
Definition: MenuIterator.cpp:195
MenuItem * getParentRoot(MenuItem *current)
Definition: MenuIterator.h:47
MenuItem * getMenuItemById(menuid_t id)
Definition: MenuIterator.cpp:93
Definition: MenuItems.h:329
Definition: MenuIterator.h:173
void setPredicate(MenuItemPredicate *predicate)
Definition: MenuIterator.h:190
void reset()
Definition: MenuIterator.cpp:110
MenuItem * currentParent()
Definition: MenuIterator.cpp:165
MenuItem * nextItem()
Definition: MenuIterator.cpp:116
Definition: MenuIterator.h:90
virtual bool matches(MenuItem *item)=0
Definition: MenuIterator.h:145
MenuItemTypePredicate(MenuType filterType, uint8_t mode=0)
Definition: MenuIterator.h:154
bool matches(MenuItem *item) override
Definition: MenuIterator.cpp:179
Definition: MenuIterator.h:103
bool matches(MenuItem *item) override
Definition: MenuIterator.cpp:170
void setRemoteNo(uint8_t newRemoteNum)
Definition: MenuIterator.h:124
RemoteNoMenuItemPredicate(int remoteNo)
Definition: MenuIterator.h:111