-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConsoleMenu.cpp
55 lines (47 loc) · 1.19 KB
/
ConsoleMenu.cpp
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
#include "ConsoleMenu.h"
#include <iostream>
template <typename T>
void ConsoleMenu<T>::addOption(const T name, const std::function<void()> f)
{
m_options.emplace_back(name, f);
}
template <typename T>
void ConsoleMenu<T>::exec() const
{
int32_t option = -1;
do
{
system("cls");
printMenu();
std::cout << "Enter an option: ";
std::cin >> option;
if(option > 0 && option <= m_options.size())
{
system("cls");
m_options[option - 1].second();
system("pause");
}
}while(option);
}
template <typename T>
template <typename M>
void ConsoleMenu<T>::printCommand(int32_t i, const M& name) const
{
if(std::is_same<M, std::string>::value)
std::cout << "\t" << i << ") " << name.c_str() << std::endl;
else if(std::is_same<M, std::wstring>::value)
std::wcout << L"\t" << i << L") " << name.c_str() << std::endl;
}
template <typename T>
void ConsoleMenu<T>::printMenu() const
{
std::cout << "Commands: " << std::endl;
for(uint64_t i = 0; i < m_options.size(); ++i)
{
printCommand<T>(static_cast<int32_t>(i) + 1, m_options[i].first);
}
printCommand<std::string>(0, "Exit");
std::cout << std::endl;
}
template class ConsoleMenu<std::string>;
template class ConsoleMenu<std::wstring>;