DUDS
Distributed Update of Data from Something
Menu.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 MENU_HPP
11 #define MENU_HPP
12 
13 //#include <duds/ui/menu/MenuErrors.hpp>
14 #include <boost/noncopyable.hpp>
15 #include <thread>
16 #include <shared_mutex>
17 #include <string>
18 #include <vector>
19 #include <map>
20 #include <memory>
21 
22 namespace duds { namespace ui {
23 
27 namespace menu {
28 
29 class MenuAccess;
30 class MenuItem;
31 class MenuOutput;
32 class MenuOutputAccess;
33 class MenuView;
34 
45 class Menu : public std::enable_shared_from_this<Menu>, boost::noncopyable {
46 public:
50  typedef std::vector< std::shared_ptr<MenuItem> > ItemVec;
54  typedef std::map< MenuView*, std::weak_ptr<MenuView> > ViewMap;
55 private:
59  ItemVec items;
64  ViewMap views;
68  std::shared_timed_mutex block;
72  std::thread::id lockOwner;
76  std::string lbl;
81  std::size_t invis;
85  std::size_t toggles;
90  int lockCnt;
97  int updateIdx;
98  friend MenuAccess;
99  friend MenuItem;
100  friend MenuOutput;
102  friend MenuView;
103 public:
110  Menu(std::size_t reserve = 0);
118  Menu(const std::string &title, std::size_t reserve = 0);
124  static std::shared_ptr<Menu> make(std::size_t reserve = 0) {
125  return std::make_shared<Menu>(reserve);
126  }
133  static std::shared_ptr<Menu> make(
134  const std::string &title,
135  std::size_t reserve = 0
136  ) {
137  return std::make_shared<Menu>(title, reserve);
138  }
143  const std::string &title() const {
144  return lbl;
145  }
150  std::size_t size() const {
151  return items.size();
152  }
156  std::size_t invisible() const {
157  return invis;
158  }
162  std::size_t visible() const {
163  return items.size() - invis;
164  }
168  bool empty() const {
169  return items.empty();
170  }
174  int updateIndex() const {
175  return updateIdx;
176  }
180  bool haveToggles() const {
181  return toggles > 0;
182  }
183 private:
187  void exclusiveLock();
191  void exclusiveUnlock();
197  void addView(const std::shared_ptr<MenuView> &view);
207  void informViews(void (MenuView::* eventFunc)(std::size_t), std::size_t idx);
216  ItemVec::const_iterator iterator(std::size_t index) const;
220  ItemVec::const_iterator cbegin() const {
221  return items.cbegin();
222  }
226  ItemVec::const_iterator cend() const {
227  return items.cend();
228  }
229 
230  // the following functions must only be invoked by MenuAccess
231 
235  void clear() noexcept;
240  void title(const std::string &newTitle) noexcept;
246  void append(std::shared_ptr<MenuItem> &&mi);
252  void append(const std::shared_ptr<MenuItem> &mi);
262  void insert(std::size_t index, std::shared_ptr<MenuItem> &&mi);
271  void insert(std::size_t index, const std::shared_ptr<MenuItem> &mi);
277  void remove(const std::shared_ptr<MenuItem> &mi);
283  void remove(std::size_t index);
284 };
285 
289 typedef std::shared_ptr<Menu> MenuSptr;
290 
294 typedef std::weak_ptr<Menu> MenuWptr;
295 
296 } } }
297 
298 #endif // #ifndef MENU_HPP
friend MenuAccess
Definition: Menu.hpp:98
void exclusiveUnlock()
Performs a recursive exclusive unlock on block.
Definition: Menu.cpp:41
std::shared_ptr< Menu > MenuSptr
A shared pointer to a Menu.
Definition: Menu.hpp:289
bool haveToggles() const
True if the menu has at least one MenuItem that is a toggle.
Definition: Menu.hpp:180
std::thread::id lockOwner
Used with block to implement recursive locking.
Definition: Menu.hpp:72
std::size_t visible() const
Returns the number of visible MenuItem objects in the menu.
Definition: Menu.hpp:162
std::size_t toggles
The number of items that are toggles.
Definition: Menu.hpp:85
void informViews(void(MenuView::*eventFunc)(std::size_t), std::size_t idx)
Calls a function on each view using this menu, and provides the index of a menu item.
Definition: Menu.cpp:93
std::shared_timed_mutex block
Used to enable thread-safe operations.
Definition: Menu.hpp:68
int lockCnt
The number of exclusive locks held by the thread indicated by lockOwner.
Definition: Menu.hpp:90
ItemVec items
The store of menu items for the menu.
Definition: Menu.hpp:59
void append(std::shared_ptr< MenuItem > &&mi)
Appends a new item to the end of the menu.
Definition: Menu.cpp:114
void addView(const std::shared_ptr< MenuView > &view)
Stores a weak reference to the given view for the purpose of informing the view of any item insertion...
Definition: Menu.cpp:87
std::size_t invisible() const
Returns the number of MenuItem objects flagged as invisible in the menu.
Definition: Menu.hpp:156
Stores the data that defines a menu and provides thread-safe access to that data. ...
Definition: Menu.hpp:45
ViewMap views
The views; used to inform the views that menu items have been added or removed.
Definition: Menu.hpp:64
static std::shared_ptr< Menu > make(std::size_t reserve=0)
Makes a new menu that is managed by a std::shared_ptr.
Definition: Menu.hpp:124
std::vector< std::shared_ptr< MenuItem > > ItemVec
The type that holds the menu&#39;s items.
Definition: Menu.hpp:50
int updateIdx
This value is incremented every time the menu is changed.
Definition: Menu.hpp:97
std::size_t invis
The number of items that are currently flagged as invisible.
Definition: Menu.hpp:81
void insert(std::size_t index, std::shared_ptr< MenuItem > &&mi)
Inserts a new item into the menu.
Definition: Menu.cpp:141
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::map< MenuView *, std::weak_ptr< MenuView > > ViewMap
Container type used to track views of the menu.
Definition: Menu.hpp:54
std::size_t size() const
Returns the number of MenuItem objects in the menu.
Definition: Menu.hpp:150
std::weak_ptr< Menu > MenuWptr
A weak pointer to a Menu.
Definition: Menu.hpp:294
ItemVec::const_iterator cbegin() const
Returns the begin iterator for the contained MenuItem objects.
Definition: Menu.hpp:220
Keeps track of the selected menu item, and updates it based on user input.
Definition: MenuView.hpp:46
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
static std::shared_ptr< Menu > make(const std::string &title, std::size_t reserve=0)
Makes a new menu that is managed by a std::shared_ptr.
Definition: Menu.hpp:133
ItemVec::const_iterator cend() const
Returns the end iterator for the contained MenuItem objects.
Definition: Menu.hpp:226
std::string lbl
The menu&#39;s name; optional.
Definition: Menu.hpp:76
int updateIndex() const
Returns a number that is incremented every time the menu is changed.
Definition: Menu.hpp:174
Menu(std::size_t reserve=0)
Construct a new menu.
Definition: Menu.cpp:18
bool empty() const
True if the menu has no items.
Definition: Menu.hpp:168
friend MenuOutputAccess
Definition: Menu.hpp:101