-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenu.cpp
More file actions
71 lines (58 loc) · 1.72 KB
/
Copy pathmenu.cpp
File metadata and controls
71 lines (58 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "menu.h"
Menu::Menu(std::string menu_name) : m_menuName(menu_name) {
m_entries = new Entries;
AddEntry("q", "exit " + m_menuName, ([this]() { Quit(); }));
}
Menu::~Menu() {
for (auto entry : *m_entries) {
delete (entry);
entry == nullptr;
}
delete (m_entries);
m_entries = nullptr;
}
void Menu::AddEntry(std::string command, std::string description,
EntryFunction method) {
Entry* newOption = new Entry;
newOption->command = command;
newOption->description = description;
newOption->method = method;
m_entries->push_back(newOption);
}
void Menu::Quit() { m_quit = true; }
void Menu::EnterMenu() {
m_quit = false;
while (!m_quit) {
std::string input;
while (!m_quit) {
PrintMenu();
std::cin >> input;
EvaluateUserInput(input);
}
}
}
// present possible options to user
void Menu::PrintMenu() {
std::cout << "\n" + m_menuName + "\nWhat do you want to do?\n\n";
for (auto item : *m_entries) {
std::cout << item->command << ": " << item->description << "\n";
}
std::cout << std::endl;
}
void Menu::EvaluateUserInput(std::string input) {
for (auto item : *m_entries) {
if (item->command == input) {
std::cout << item->description << std::endl;
item->method();
return;
}
}
std::cout << "No valid choice. Please choose from given options."
<< std::endl;
}
// Sub menu
void Menu::AddSubMenu(std::string command, std::string sub_menu_name) {
m_menu = new Menu(sub_menu_name);
AddEntry(command, sub_menu_name, [this]() { m_menu->EnterMenu(); });
}
Menu* Menu::GetSubMenu() { return m_menu; }