-
Notifications
You must be signed in to change notification settings - Fork 0
/
options_check_example.cpp
60 lines (50 loc) · 1.75 KB
/
options_check_example.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
56
57
58
59
60
#include <iostream>
#include <variant>
#include <vector>
int main() {
const std::string givenKey = "Thread";
const std::string givenValue = "1";
std::cout << std::endl;
std::vector<std::vector<std::variant<std::string, double, int, bool>>> vecVecVariant = {
{std::string("Thread"), 2, 1, 128},
{std::string("OpenTimeoutSecs"), 0.0, 0.0, 10},
{std::string("Profile"), true}};
// display each value
for (auto &vv : vecVecVariant) {
for (auto &v : vv) {
std::visit([](auto &&arg) { std::cout << arg << " "; }, v);// 2
}
std::cout << std::endl;
}
std::cout << std::endl;
// display each type
for (auto &vv : vecVecVariant) {
for (auto &v : vv) {
std::visit([](auto &&arg) { std::cout << typeid(arg).name() << " "; }, v);// 3
}
std::cout << std::endl;
}
std::cout << std::endl;
// check the key
for (auto &vv : vecVecVariant) {
std::string keyWord = std::get<std::string>(vv[0]);
if (givenKey == keyWord) {
// dispatch by type
std::string stype = std::visit([](auto &&arg) { return typeid(arg).name(); }, vv[1]);
if (stype == typeid(std::string("")).name()) {
std::cout << "Handle string"
<< " ";
} else if (stype == typeid(int).name()) {
std::cout << "Handle int"
<< " ";
} else if (stype == typeid(double).name()) {
std::cout << "Handle double"
<< " ";
}
if (stype == typeid(bool).name()) {
std::cout << "Handle bool"
<< " ";
}
}
}
}