123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- #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;
- }
|