-
Notifications
You must be signed in to change notification settings - Fork 0
/
Menu.h
57 lines (49 loc) · 972 Bytes
/
Menu.h
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
#include <iostream>
#include <string>
class Menu
{
private:
int count;
int pick()
{
int selection = 0;
while(selection < 1 || selection > count)
{
std::cout << "Selection (1-" << count << "): ";
if(!(std::cin >> selection))
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
selection = 0;
}
}
count = 0;
return selection;
}
void item(const std::string& item)
{
count += 1;
std::cout << count << ") " << item << '\n';
}
int static_menu()
{
return pick();
}
public:
Menu(): count(0)
{}
template<class... Items>
int static_menu(const std::string& i, Items&&... rest)
{
item(i);
return static_menu(rest...);
}
template<class Container, class Helper>
auto dynamic_menu(const Container& container, Helper&& helper) -> decltype((container.front()))
{
for(const auto& i : container)
item(helper(i));
int selection = pick();
return container[selection-1];
}
};