Skip to content

Commit 643a660

Browse files
author
Ross Lombardi
committed
version 2 (now produces a usable enum_values.cpp);
1 parent aaafd2d commit 643a660

File tree

5 files changed

+72
-63
lines changed

5 files changed

+72
-63
lines changed

enum_reader/enum_reader.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ struct enum_data
146146
}
147147
else
148148
{
149-
return nspace + ":" + name;
149+
return nspace + "::" + name;
150150
}
151151
}
152152
};
@@ -304,14 +304,16 @@ string create_enum_map_data(enum_data const& data)
304304
// and the reverse
305305
string map_text;
306306

307-
map_text.append("std::unordered_map<" + data.type + ", std::string> enum_flags<" + data.name + ">::value_to_name = {\r");
307+
map_text.append("enum class " + data.full_name() + " : " + data.type + ";\r");
308+
309+
map_text.append("const std::unordered_map<" + data.type + ", std::string> enum_static<" + data.name + ">::value_to_name = {\r");
308310

309311
for (auto& enum_value : data.values)
310312
{
311313
map_text.append("\t{ " + to_string(enum_value.value) + ", \"" + enum_value.name + "\" }, \r");
312314
}
313315
map_text.append(" };\r");
314-
map_text.append("std::unordered_map<std::string, " + data.type + "> enum_flags<" + data.name + ">::name_to_value = {\r");
316+
map_text.append("const std::unordered_map<std::string, " + data.type + "> enum_static<" + data.name + ">::name_to_value = {\r");
315317

316318
for (auto& enum_value : data.values)
317319
{
@@ -370,7 +372,7 @@ string create_cpp_file_text(vector<string> const& all_maps)
370372
{
371373
string text;
372374

373-
text.append("#include \"enum_flags.h\"\r\r");
375+
text.append("#include <unordered_map>\r#include \"enum_values.h\"\r\r");
374376

375377
for (auto& m : all_maps)
376378
{
@@ -404,7 +406,7 @@ int main(int argc, char** argv)
404406

405407
for (auto& entry : std::filesystem::directory_iterator(code_path))
406408
{
407-
if (entry.is_regular_file() &&
409+
if (entry.is_regular_file() && (!entry.path().string().ends_with("enum_values.cpp")) &&
408410
(entry.path().extension().string().ends_with(".cpp")) || entry.path().extension().string().ends_with(".h"))
409411
{
410412
cout << "[Source] Opening " << entry.path().string() << "." << endl;

enum_reader/enum_reader.vcxproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
<SDLCheck>true</SDLCheck>
105105
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
106106
<ConformanceMode>true</ConformanceMode>
107+
<LanguageStandard>stdcpp20</LanguageStandard>
107108
</ClCompile>
108109
<Link>
109110
<SubSystem>Console</SubSystem>
@@ -118,6 +119,7 @@
118119
<SDLCheck>true</SDLCheck>
119120
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
120121
<ConformanceMode>true</ConformanceMode>
122+
<LanguageStandard>stdcpp20</LanguageStandard>
121123
</ClCompile>
122124
<Link>
123125
<SubSystem>Console</SubSystem>
@@ -132,6 +134,7 @@
132134
<SDLCheck>true</SDLCheck>
133135
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
134136
<ConformanceMode>true</ConformanceMode>
137+
<LanguageStandard>stdcpp20</LanguageStandard>
135138
</ClCompile>
136139
<Link>
137140
<SubSystem>Console</SubSystem>

enum_values/enum_values.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,62 @@
11
#include <unordered_map>
22
#include "enum_values.h"
33

4+
enum class myvalues : int;
5+
const std::unordered_map<int, std::string> enum_static<myvalues>::value_to_name = {
6+
{ 0, "zero" },
7+
{ 1, "one" },
8+
{ 2, "two" },
9+
{ 3, "three" },
10+
{ 3, "also_three" },
11+
{ 4, "four" },
12+
{ 5, "five" },
13+
};
14+
const std::unordered_map<std::string, int> enum_static<myvalues>::name_to_value = {
15+
{ "zero", 0 },
16+
{ "one", 1 },
17+
{ "two", 2 },
18+
{ "three", 3 },
19+
{ "also_three", 3 },
20+
{ "four", 4 },
21+
{ "five", 5 },
22+
};
23+
24+
enum class yourvalues : int;
25+
const std::unordered_map<int, std::string> enum_static<yourvalues>::value_to_name = {
26+
{ 0, "zero" },
27+
{ 1, "one" },
28+
{ 2, "two" },
29+
{ 2, "also_two" },
30+
{ 3, "three" },
31+
{ 4, "four" },
32+
{ 5, "five" },
33+
};
34+
const std::unordered_map<std::string, int> enum_static<yourvalues>::name_to_value = {
35+
{ "zero", 0 },
36+
{ "one", 1 },
37+
{ "two", 2 },
38+
{ "also_two", 2 },
39+
{ "three", 3 },
40+
{ "four", 4 },
41+
{ "five", 5 },
42+
};
43+
44+
enum class theirvalues : int;
45+
const std::unordered_map<int, std::string> enum_static<theirvalues>::value_to_name = {
46+
{ 0, "zero" },
47+
{ 1, "one" },
48+
{ 2, "two" },
49+
{ 2, "also_two" },
50+
{ 3, "three" },
51+
{ 4, "four" },
52+
{ 5, "five" },
53+
};
54+
const std::unordered_map<std::string, int> enum_static<theirvalues>::name_to_value = {
55+
{ "zero", 0 },
56+
{ "one", 1 },
57+
{ "two", 2 },
58+
{ "also_two", 2 },
59+
{ "three", 3 },
60+
{ "four", 4 },
61+
{ "five", 5 },
62+
};

enum_values/enum_values.vcxproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
<SDLCheck>true</SDLCheck>
104104
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
105105
<ConformanceMode>true</ConformanceMode>
106+
<LanguageStandard>stdcpp20</LanguageStandard>
106107
</ClCompile>
107108
<Link>
108109
<SubSystem>Console</SubSystem>
@@ -117,6 +118,7 @@
117118
<SDLCheck>true</SDLCheck>
118119
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
119120
<ConformanceMode>true</ConformanceMode>
121+
<LanguageStandard>stdcpp20</LanguageStandard>
120122
</ClCompile>
121123
<Link>
122124
<SubSystem>Console</SubSystem>
@@ -131,6 +133,7 @@
131133
<SDLCheck>true</SDLCheck>
132134
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
133135
<ConformanceMode>true</ConformanceMode>
136+
<LanguageStandard>stdcpp20</LanguageStandard>
134137
</ClCompile>
135138
<Link>
136139
<SubSystem>Console</SubSystem>

enum_values/tester.cpp

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -41,64 +41,6 @@ enum class theirvalues : int //ns@ourvalues
4141
five,
4242
};
4343

44-
const std::unordered_map<int, std::string> enum_static<myvalues>::value_to_name = {
45-
{ 0, "zero" },
46-
{ 1, "one" },
47-
{ 2, "two" },
48-
{ 3, "three" },
49-
{ 3, "also_three" },
50-
{ 4, "four" },
51-
{ 5, "five" },
52-
};
53-
const std::unordered_map<std::string, int> enum_static<myvalues>::name_to_value = {
54-
{ "zero", 0 },
55-
{ "one", 1 },
56-
{ "two", 2 },
57-
{ "three", 3 },
58-
{ "also_three", 3 },
59-
{ "four", 4 },
60-
{ "five", 5 },
61-
};
62-
63-
const std::unordered_map<int, std::string> enum_static<yourvalues>::value_to_name = {
64-
{ 0, "zero" },
65-
{ 1, "one" },
66-
{ 2, "two" },
67-
{ 2, "also_two" },
68-
{ 3, "three" },
69-
{ 4, "four" },
70-
{ 5, "five" },
71-
};
72-
const std::unordered_map<std::string, int> enum_static<yourvalues>::name_to_value = {
73-
{ "zero", 0 },
74-
{ "one", 1 },
75-
{ "two", 2 },
76-
{ "also_two", 2 },
77-
{ "three", 3 },
78-
{ "four", 4 },
79-
{ "five", 5 },
80-
};
81-
82-
const std::unordered_map<int, std::string> enum_static<theirvalues>::value_to_name = {
83-
{ 0, "zero" },
84-
{ 1, "one" },
85-
{ 2, "two" },
86-
{ 2, "also_two" },
87-
{ 3, "three" },
88-
{ 4, "four" },
89-
{ 5, "five" },
90-
};
91-
const std::unordered_map<std::string, int> enum_static<theirvalues>::name_to_value = {
92-
{ "zero", 0 },
93-
{ "one", 1 },
94-
{ "two", 2 },
95-
{ "also_two", 2 },
96-
{ "three", 3 },
97-
{ "four", 4 },
98-
{ "five", 5 },
99-
};
100-
101-
10244
void that(myvalues v)
10345
{
10446
v = myvalues::also_three;

0 commit comments

Comments
 (0)