Browse Source

First commit

Anton A. Truttse 2 years ago
commit
434cbd2c76
6 changed files with 269 additions and 0 deletions
  1. 99 0
      fillMenu.cpp
  2. 24 0
      fillMenu.h
  3. 18 0
      main.cpp
  4. BIN
      menu
  5. 65 0
      menu.cpp
  6. 63 0
      menu.h

+ 99 - 0
fillMenu.cpp

@@ -0,0 +1,99 @@
+#include <cstdlib>
+#include <iostream>
+
+#include "fillMenu.h"
+
+CMenu g_MainMenu("Main menu:");
+CMenu g_SubMenu1("Submenu1:");
+CMenu g_SubMenu2("Submenu2:");
+
+void initMenus()
+{
+    g_MainMenu.addItem('1', "First item", menuItem1);
+    g_MainMenu.addItem('2', "Second item", menuItem2);
+    g_MainMenu.addItem('3', "Third item", menuItem3);
+    g_MainMenu.addItem('4', "Quit", menuItem4);
+
+    g_SubMenu1.addItem('1', "11 item", menuItem11);
+    g_SubMenu1.addItem('2', "12 item", menuItem12);
+    g_SubMenu1.addItem('3', "Return to main menu", menuItem13);
+
+    g_SubMenu2.addItem('1', "21 item", menuItem21);
+    g_SubMenu2.addItem('2', "Return to main menu", menuItem22);
+
+}
+
+int menuItem1()
+{
+    std::cout << "Item 1 Selected!" << std::endl;
+    do
+    {
+        g_SubMenu1.draw();
+    }
+    while (g_SubMenu1.selectItem() == 0);
+
+    return 0;
+}
+
+int menuItem2()
+{
+    std::cout << "Item 2 Selected!" << std::endl;
+    do
+    {
+        g_SubMenu2.draw();
+    }
+    while (g_SubMenu2.selectItem() == 0);
+
+    return 0;
+}
+
+int menuItem3()
+{
+    std::cout << "Item 3 Selected!" << std::endl;
+
+    return 0;
+}
+
+int menuItem4()
+{
+    std::cout << "Item 4 Selected!" << std::endl;
+    std::cout << "Quit!" << std::endl;
+    exit(EXIT_SUCCESS);
+
+    return 0;
+}
+
+int menuItem11()
+{
+    std::cout << "Item 11 Selected!" << std::endl;
+
+    return 0;
+}
+
+int menuItem12()
+{
+    std::cout << "Item 12 Selected!" << std::endl;
+
+    return 0;
+}
+
+int menuItem13()
+{
+    std::cout << "Item 13 Selected!" << std::endl;
+
+    return 1;
+}
+
+int menuItem21()
+{
+    std::cout << "Item 21 Selected!" << std::endl;
+
+    return 0;
+}
+
+int menuItem22()
+{
+    std::cout << "Item 22 Selected!" << std::endl;
+
+    return 1;
+}

+ 24 - 0
fillMenu.h

@@ -0,0 +1,24 @@
+#ifndef FILLMENU_H_INCLUDED
+#define FILLMENU_H_INCLUDED
+
+#include "menu.h"
+
+extern CMenu g_MainMenu;
+extern CMenu g_SubMenu1;
+extern CMenu g_SubMenu2;
+
+int menuItem1();
+int menuItem2();
+int menuItem3();
+int menuItem4();
+
+int menuItem11();
+int menuItem12();
+int menuItem13();
+
+int menuItem21();
+int menuItem22();
+
+void initMenus();
+
+#endif // FILLMENU_H_INCLUDED

+ 18 - 0
main.cpp

@@ -0,0 +1,18 @@
+#include <iostream>
+
+#include "fillMenu.h"
+
+using namespace std;
+
+int main()
+{
+    initMenus();
+
+    while (true)
+    {
+        g_MainMenu.draw();
+        g_MainMenu.selectItem();
+    }
+
+    return 0;
+}

BIN
menu


+ 65 - 0
menu.cpp

@@ -0,0 +1,65 @@
+#include <algorithm>
+#include <iostream>
+
+#include "menu.h"
+
+const char CMenu::STR_DEFAULT_TITLE[] = "Please, choose:";
+const char CMenu::STR_DEFAULT_DIVIDER[] = " - ";
+const char CMenu::STR_DEFAULT_PREFIX[] = "";
+
+bool CMenu::addItem(const menuMapValue &_item)
+{
+    if (m_MenuMap.find(_item.first) != m_MenuMap.end())
+    {
+        return false;
+    }
+
+    m_MenuMap.insert(_item);
+    return true;
+}
+
+bool CMenu::addItem(const menuMapKey _key, const menuMapItem &_item)
+{
+    return addItem(std::make_pair(_key, _item));
+}
+
+bool CMenu::addItem(const menuMapKey _key, const std::string _title, const menuFuncPtr _item)
+{
+    return addItem(_key, MenuItem(_title, _item));
+}
+
+void CMenu::draw() const
+{
+    std::cout << std::endl << m_Title << std::endl;
+
+    // for_each with lambda-function
+    // need capture (this) to access class members (m_Prefix, m_Divider)
+    for_each(m_MenuMap.cbegin(), m_MenuMap.cend(), [this](const menuMapValue& _item)
+        {
+            std::cout << m_Prefix <<_item.first << m_Divider << _item.second.title << std::endl;
+        }
+    );
+
+    std::cout << std::endl;
+}
+
+int  CMenu::selectItem() const
+{
+    char cKey;
+    menuMapCIt it;
+
+    do
+    {
+        std::cin >> cKey;
+        it = m_MenuMap.find(cKey);
+    }
+    while (it == m_MenuMap.cend());
+
+    if (it->second.item != nullptr)
+    {
+        return (*it->second.item)();
+    }
+
+    return -1;
+}
+

+ 63 - 0
menu.h

@@ -0,0 +1,63 @@
+#ifndef MENU_H_INCLUDED
+#define MENU_H_INCLUDED
+
+#include <map>
+#include <string>
+
+typedef int (*menuFuncPtr)();
+
+struct MenuItem
+{
+    std::string title;
+    menuFuncPtr item;
+
+    MenuItem(const std::string _title, const menuFuncPtr _item) : title(_title), item(_item) {};
+};
+
+
+typedef std::map<char, MenuItem> menuMap;
+typedef menuMap::iterator menuMapIt;
+typedef menuMap::const_iterator menuMapCIt;
+typedef menuMap::value_type menuMapValue;
+typedef menuMap::key_type menuMapKey;
+typedef menuMap::mapped_type menuMapItem;
+
+class CMenu
+{
+    protected:
+        std::string m_Title;
+        std::string m_Prefix;
+        std::string m_Divider;
+
+        menuMap m_MenuMap;
+
+        static const char STR_DEFAULT_TITLE[];
+        static const char STR_DEFAULT_PREFIX[];
+        static const char STR_DEFAULT_DIVIDER[];
+
+    public:
+        CMenu() : m_Title(STR_DEFAULT_TITLE),
+                  m_Prefix(STR_DEFAULT_PREFIX),
+                  m_Divider(STR_DEFAULT_DIVIDER) {};
+        CMenu(const std::string _title) : m_Title(_title),
+                  m_Prefix(STR_DEFAULT_PREFIX),
+                  m_Divider(STR_DEFAULT_DIVIDER) {};
+
+        virtual ~CMenu() {};
+
+        virtual bool addItem(const menuMapValue &_item);
+        virtual bool addItem(const menuMapKey _key, const menuMapItem &_item);
+        virtual bool addItem(const menuMapKey _key, const std::string _title, const menuFuncPtr _item);
+
+        virtual void draw() const;
+        virtual int  selectItem() const;
+
+        virtual void setTitle(const std::string _title) {m_Title = _title;}
+        virtual void setPrefix(const std::string _prefix) {m_Prefix = _prefix;}
+        virtual void setDivider(const std::string _divider) {m_Divider = _divider;}
+
+    private:
+        CMenu(const CMenu& _copy) {};
+        CMenu& operator=(const CMenu& _copy) {return *this;};
+};
+#endif // MENU_H_INCLUDED