DUDS
Distributed Update of Data from Something
MenuAccess.hpp
Go to the documentation of this file.
1 /*
2  * This file is part of the DUDS project. It is subject to the BSD-style
3  * license terms in the LICENSE file found in the top-level directory of this
4  * distribution and at https://github.com/jjackowski/duds/blob/master/LICENSE.
5  * No part of DUDS, including this file, may be copied, modified, propagated,
6  * or distributed except according to the terms contained in the LICENSE file.
7  *
8  * Copyright (C) 2019 Jeff Jackowski
9  */
10 #ifndef MENUACCESS_HPP
11 #define MENUACCESS_HPP
12 
13 #include <duds/ui/menu/Menu.hpp>
15 
16 namespace duds { namespace ui { namespace menu {
17 
32 class MenuAccess {
37 public:
42  MenuAccess(const std::shared_ptr<Menu> &m) : menu(m.get()) {
43  if (menu) {
44  menu->exclusiveLock();
45  }
46  }
51  MenuAccess(Menu &m) : menu(&m) {
52  menu->exclusiveLock();
53  }
58  retire();
59  }
63  void retire() noexcept {
64  if (menu) {
65  menu->exclusiveUnlock();
66  }
67  }
71  const std::string &title() const {
72  return menu->title();
73  }
78  void title(const std::string &newTitle) const {
79  menu->title(newTitle);
80  }
84  std::size_t size() const {
85  return menu->size();
86  }
90  bool haveToggles() const {
91  return menu->haveToggles();
92  }
96  void clear() const {
97  menu->clear();
98  }
104  const MenuItemSptr &item(std::size_t index) const {
105  return *(menu->iterator(index));
106  }
112  void append(MenuItemSptr &&mi) const {
113  menu->append(std::move(mi));
114  }
120  void append(const MenuItemSptr &mi) const {
121  menu->append(mi);
122  }
132  void insert(std::size_t index, MenuItemSptr &&mi) const {
133  menu->insert(index, std::move(mi));
134  }
143  void insert(std::size_t index, const MenuItemSptr &mi) const {
144  menu->insert(index, mi);
145  }
152  void remove(const MenuItemSptr &mi) const {
153  menu->remove(mi);
154  }
160  void remove(std::size_t index) const {
161  menu->remove(index);
162  }
171  void changeVisibility(std::size_t index, bool vis) const {
172  item(index)->changeVisibility(vis);
173  }
179  void hide(std::size_t index) const {
180  changeVisibility(index, false);
181  }
187  void show(std::size_t index) const {
188  changeVisibility(index, true);
189  }
198  void changeEnabledState(std::size_t index, bool en) const {
199  item(index)->changeEnabledState(en);
200  }
206  void disable(std::size_t index) const {
207  changeEnabledState(index, false);
208  }
214  void enable(std::size_t index) const {
215  changeEnabledState(index, true);
216  }
226  bool toggle(std::size_t index) const {
227  return item(index)->toggle();
228  }
239  void changeToggle(std::size_t index, bool state) const {
240  item(index)->changeToggle(state);
241  }
250  void clearToggle(std::size_t index) const {
251  changeToggle(index, false);
252  }
261  void setToggle(std::size_t index) const {
262  changeToggle(index, true);
263  }
269  const std::string &label(std::size_t index) const {
270  return item(index)->label();
271  }
278  void label(std::size_t index, const std::string &label) {
279  item(index)->label(label);
280  }
287  const std::string &value(std::size_t index) const {
288  return item(index)->value();
289  }
296  void value(std::size_t index, const std::string &value) {
297  item(index)->value(value);
298  }
299 };
300 
301 } } }
302 
303 #endif // #ifndef MENUACCESS_HPP
void setToggle(std::size_t index) const
Sets the toggle state of an item on the menu.
Definition: MenuAccess.hpp:261
void title(const std::string &newTitle) const
Change the title of the menu.
Definition: MenuAccess.hpp:78
void show(std::size_t index) const
Shows an item on the menu that was hidden.
Definition: MenuAccess.hpp:187
void exclusiveUnlock()
Performs a recursive exclusive unlock on block.
Definition: Menu.cpp:41
bool haveToggles() const
True if the menu has at least one MenuItem that is a toggle.
Definition: Menu.hpp:180
void insert(std::size_t index, MenuItemSptr &&mi) const
Inserts a new item into the menu.
Definition: MenuAccess.hpp:132
void retire() noexcept
Gives up access to the menu.
Definition: MenuAccess.hpp:63
void value(std::size_t index, const std::string &value)
Changes a menu item&#39;s optional value.
Definition: MenuAccess.hpp:296
const std::string & label(std::size_t index) const
Returns a menu item&#39;s currently set label.
Definition: MenuAccess.hpp:269
std::size_t size() const
Returns the number of items in the menu.
Definition: MenuAccess.hpp:84
Menu * menu
The menu to operate upon.
Definition: MenuAccess.hpp:36
const std::string & value(std::size_t index) const
Returns a menu item&#39;s currently set value.
Definition: MenuAccess.hpp:287
move_impl move(unsigned int c, unsigned int r)
Display stream manipulator that moves the display cursor to the given location.
void append(std::shared_ptr< MenuItem > &&mi)
Appends a new item to the end of the menu.
Definition: Menu.cpp:114
MenuAccess(Menu &m)
Creates a new MenuAccess object to modify the given Menu.
Definition: MenuAccess.hpp:51
Stores the data that defines a menu and provides thread-safe access to that data. ...
Definition: Menu.hpp:45
bool toggle(std::size_t index) const
Toggles the toggle state of an item on the menu.
Definition: MenuAccess.hpp:226
void disable(std::size_t index) const
Disables an item on the menu.
Definition: MenuAccess.hpp:206
void insert(std::size_t index, std::shared_ptr< MenuItem > &&mi)
Inserts a new item into the menu.
Definition: Menu.cpp:141
MenuAccess(const std::shared_ptr< Menu > &m)
Creates a new MenuAccess object to modify the given Menu.
Definition: MenuAccess.hpp:42
~MenuAccess()
Gives up access to the menu.
Definition: MenuAccess.hpp:57
void exclusiveLock()
Performs a recursive exclusive lock on block.
Definition: Menu.cpp:28
ItemVec::const_iterator iterator(std::size_t index) const
Returns an iterator to the MenuItem object at the given position.
Definition: Menu.cpp:53
std::size_t size() const
Returns the number of MenuItem objects in the menu.
Definition: Menu.hpp:150
bool haveToggles() const
True if the menu has at least one MenuItem that is a toggle.
Definition: MenuAccess.hpp:90
std::shared_ptr< MenuItem > MenuItemSptr
A shared pointer to a MenuItem.
Definition: MenuItem.hpp:411
const MenuItemSptr & item(std::size_t index) const
Returns the MenuItem object at the given position.
Definition: MenuAccess.hpp:104
void insert(std::size_t index, const MenuItemSptr &mi) const
Inserts a new item into the menu.
Definition: MenuAccess.hpp:143
void clear() const
Removes all items from the menu.
Definition: MenuAccess.hpp:96
const std::string & title() const
Returns the title of the menu.
Definition: MenuAccess.hpp:71
void append(const MenuItemSptr &mi) const
Appends a new item to the end of the menu.
Definition: MenuAccess.hpp:120
Provides an exclusive lock on a Menu to allow the menu to be changed.
Definition: MenuAccess.hpp:32
void clear() noexcept
Removes all items from the menu.
Definition: Menu.cpp:64
const std::string & title() const
Returns the title of the menu.
Definition: Menu.hpp:143
void hide(std::size_t index) const
Hides an item on the menu from view.
Definition: MenuAccess.hpp:179
void changeVisibility(std::size_t index, bool vis) const
Changes the visibility of an item on the menu.
Definition: MenuAccess.hpp:171
void changeEnabledState(std::size_t index, bool en) const
Enables or disables an item on the menu.
Definition: MenuAccess.hpp:198
void changeToggle(std::size_t index, bool state) const
Changes the toggle state of an item on the menu to the indicated state.
Definition: MenuAccess.hpp:239
void clearToggle(std::size_t index) const
Clears the toggle state of an item on the menu.
Definition: MenuAccess.hpp:250
void remove(const std::shared_ptr< MenuItem > &mi)
Removes an item from the menu.
Definition: Menu.cpp:179
void enable(std::size_t index) const
Enables an item on the menu.
Definition: MenuAccess.hpp:214
void append(MenuItemSptr &&mi) const
Appends a new item to the end of the menu.
Definition: MenuAccess.hpp:112
void label(std::size_t index, const std::string &label)
Changes a menu item&#39;s label text.
Definition: MenuAccess.hpp:278