-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusage_example.cpp
161 lines (141 loc) · 5.31 KB
/
usage_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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <array>
#include <iostream>
#include <string>
#include <string_view>
#include "StringMap.hpp"
void StringSwitchExample() {
static constexpr auto sw = StringMatch<"abc", "def", "ghij", "foo", "bar", "baz", "qux",
"abacaba", "ring", "ideal", "GLn(F)">();
static_assert(sw("abc") == 0);
static_assert(sw("def") == 1);
static_assert(sw("ghij") == 2);
static_assert(sw("foo") == 3);
static_assert(sw("bar") == 4);
static_assert(sw("baz") == 5);
static_assert(sw("qux") == 6);
static_assert(sw("abacaba") == 7);
static_assert(sw("ring") == 8);
static_assert(sw("ideal") == 9);
static_assert(sw("GLn(F)") == 10);
static_assert(sw.kDefaultValue == sw("GLn(F)") + 1);
static_assert(sw.kDefaultValue == 11);
static_assert(sw("not_in") == sw.kDefaultValue);
static_assert(sw("") == sw.kDefaultValue);
static_assert(sw("a") == sw.kDefaultValue);
static_assert(sw("A") == sw.kDefaultValue);
static_assert(sw("bc") == sw.kDefaultValue);
static_assert(sw("de") == sw.kDefaultValue);
constexpr const unsigned char kUString[] = "abc";
static_assert(sw(kUString, std::size(kUString) - 1) == sw("abc"));
std::cout << "Input string to search:\n> ";
std::string input;
std::cin >> input;
std::string_view ans;
switch (sw(input)) {
case sw("abc"):
ans = "found string \"abc\"";
break;
case sw("def"):
ans = "found string \"def\"";
break;
case sw("ghij"):
ans = "found string \"ghij\"";
break;
case sw("foo"):
ans = "found string \"foo\"";
break;
case sw("bar"):
ans = "found string \"bar\"";
break;
case sw("baz"):
ans = "found string \"baz\"";
break;
case sw("qux"):
ans = "found string \"qux\"";
break;
case sw("abacaba"):
ans = "found string \"abacaba\"";
break;
case sw("ring"):
ans = "found string \"ring\"";
break;
case sw("ideal"):
ans = "found string \"ideal\"";
break;
case sw("GLn(F)"):
ans = "found string \"GLn(F)\"";
break;
case sw.kDefaultValue:
default:
ans = "not in the switch!";
break;
}
std::cout << ans << '\n';
}
void ComileTimeStringMapExample1() {
// clang-format off
// Map from string to integers, just pass N string literals,
// and they will be mapped to the intergers 0...N-1 respectively
// Default integer value in the matcher will be N
static constexpr auto match = StringMatch<"text1", "text2", "text3", "text4">();
static_assert(match("text1") == 0);
static_assert(match("text2") == 1);
static_assert(match("text3") == 2);
static_assert(match("text4") == 3);
static_assert(match("not in") == match.kDefaultValue);
static_assert(match.kDefaultValue == 4);
std::cout << "Max char amongst strings added to the data structure: '" << match.kMaxChar << "'\n"
<< "Min char amongst strings added to the data structure: '" << match.kMinChar << "'\n"
<< "Default mapped value in the data structure: " << match.kDefaultValue << '\n';
// Map from string to enum
enum class SomeEnum {
kText1,
kText2,
kText3,
kText4,
kNone,
};
using enum SomeEnum;
// First, pass N values in the std::array
// Then, pass default value
// Then, pass N string literals
static constexpr auto map =
StringMap<std::array{kText1, kText2, kText3, kText4, kText1, kText3},
/* DefaultMapValue = */ kNone,
"text1", "text2", "text3", "text4", "Text1", "Text3">();
static_assert(map("text1") == kText1);
static_assert(map("text2") == kText2);
static_assert(map("text3") == kText3);
static_assert(map("text4") == kText4);
static_assert(map("Text1") == kText1);
static_assert(map("Text3") == kText3);
static_assert(map("something else") == kNone);
static_assert(map.kDefaultValue == kNone);
// clang-format on
}
void ComileTimeStringMapExample2() {
// clang-format off
constexpr std::string_view kMyConstants[] = {"abc", "def", "ghi", "sneaky input"};
struct MyTrivialType {
std::array<int, 2> field1{};
int field2{};
constexpr MyTrivialType(int arg1, int arg2, int arg3) noexcept
: field1{arg1, arg2}, field2(arg3) {}
constexpr bool operator==(const MyTrivialType&) const noexcept = default;
};
static constexpr auto map = StringMap<
std::array{MyTrivialType(1, 2, 3), MyTrivialType(4, 5, 6), MyTrivialType(7, 8, 9)},
/* DefaultMapValue = */ MyTrivialType(0, 0, 0),
kMyConstants[0], kMyConstants[1], kMyConstants[2]>();
static_assert(map(kMyConstants[0]) == MyTrivialType(1, 2, 3));
static_assert(map(kMyConstants[1]) == MyTrivialType(4, 5, 6));
static_assert(map(kMyConstants[2]) == MyTrivialType(7, 8, 9));
static_assert(map(kMyConstants[3]) == MyTrivialType(0, 0, 0));
static_assert(map.kDefaultValue == MyTrivialType(0, 0, 0));
// clang-format on
}
int main() {
StringSwitchExample();
ComileTimeStringMapExample1();
ComileTimeStringMapExample2();
}