Skip to content

Commit 2f04065

Browse files
author
Ross Lombardi
committed
fixed namespaces;
formatted tester.cpp for other people; created additional static functions; added enum description support;
1 parent 643a660 commit 2f04065

File tree

4 files changed

+94
-29
lines changed

4 files changed

+94
-29
lines changed

enum_reader/enum_reader.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const string enum_header_name_text = R"(enum class ([A-z]+))";
1111
const string enum_header_type_text = R"((:|: )([A-z]+))"; // 2nd group is what we take
1212
const string enum_header_nspace_text = R"(\/\/ns@([A-z]+))";
1313

14-
const string enum_value_name_text = R"(^\s([A-z_]+))";
14+
const string enum_value_name_text = R"(^[\s]*([A-z_]+))";
1515
const string enum_value_expr_text = R"(=([0-9A-z\-| ]+))";
1616
const string enum_value_comment_text = R"(\/\/([ A-z0-9!?]+))";
1717

@@ -304,16 +304,25 @@ string create_enum_map_data(enum_data const& data)
304304
// and the reverse
305305
string map_text;
306306

307-
map_text.append("enum class " + data.full_name() + " : " + data.type + ";\r");
307+
if (data.nspace.empty())
308+
{
309+
map_text.append("enum class " + data.name + " : " + data.type + ";\r");
310+
}
311+
else
312+
{
313+
map_text.append("namespace " + data.nspace + "\r{\r\tenum class " + data.name + " : " + data.type + "; \r}\r");
314+
}
315+
316+
map_text.append("const std::string enum_static<" + data.full_name() + ">::enum_desc = \"" + data.comment + "\";\r");
308317

309-
map_text.append("const std::unordered_map<" + data.type + ", std::string> enum_static<" + data.name + ">::value_to_name = {\r");
318+
map_text.append("const std::unordered_map<" + data.type + ", std::string> enum_static<" + data.full_name() + ">::value_to_name = {\r");
310319

311320
for (auto& enum_value : data.values)
312321
{
313322
map_text.append("\t{ " + to_string(enum_value.value) + ", \"" + enum_value.name + "\" }, \r");
314323
}
315324
map_text.append(" };\r");
316-
map_text.append("const std::unordered_map<std::string, " + data.type + "> enum_static<" + data.name + ">::name_to_value = {\r");
325+
map_text.append("const std::unordered_map<std::string, " + data.type + "> enum_static<" + data.full_name() + ">::name_to_value = {\r");
317326

318327
for (auto& enum_value : data.values)
319328
{

enum_values/enum_values.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "enum_values.h"
33

44
enum class myvalues : int;
5+
const std::string enum_static<myvalues>::enum_desc = "these values are used by those values, top kek!";
56
const std::unordered_map<int, std::string> enum_static<myvalues>::value_to_name = {
67
{ 0, "zero" },
78
{ 1, "one" },
@@ -22,6 +23,7 @@ const std::unordered_map<std::string, int> enum_static<myvalues>::name_to_value
2223
};
2324

2425
enum class yourvalues : int;
26+
const std::string enum_static<yourvalues>::enum_desc = "";
2527
const std::unordered_map<int, std::string> enum_static<yourvalues>::value_to_name = {
2628
{ 0, "zero" },
2729
{ 1, "one" },
@@ -41,8 +43,12 @@ const std::unordered_map<std::string, int> enum_static<yourvalues>::name_to_valu
4143
{ "five", 5 },
4244
};
4345

44-
enum class theirvalues : int;
45-
const std::unordered_map<int, std::string> enum_static<theirvalues>::value_to_name = {
46+
namespace espace
47+
{
48+
enum class theirvalues : int;
49+
}
50+
const std::string enum_static<espace::theirvalues>::enum_desc = "";
51+
const std::unordered_map<int, std::string> enum_static<espace::theirvalues>::value_to_name = {
4652
{ 0, "zero" },
4753
{ 1, "one" },
4854
{ 2, "two" },
@@ -51,7 +57,7 @@ const std::unordered_map<int, std::string> enum_static<theirvalues>::value_to_na
5157
{ 4, "four" },
5258
{ 5, "five" },
5359
};
54-
const std::unordered_map<std::string, int> enum_static<theirvalues>::name_to_value = {
60+
const std::unordered_map<std::string, int> enum_static<espace::theirvalues>::name_to_value = {
5561
{ "zero", 0 },
5662
{ "one", 1 },
5763
{ "two", 2 },
@@ -60,3 +66,4 @@ const std::unordered_map<std::string, int> enum_static<theirvalues>::name_to_val
6066
{ "four", 4 },
6167
{ "five", 5 },
6268
};
69+

enum_values/enum_values.h

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class enum_static
1111
private:
1212
static const std::unordered_map<_UTy, std::string> value_to_name;
1313
static const std::unordered_map<std::string, _UTy> name_to_value;
14+
static const std::string enum_desc;
1415

1516
public:
1617

@@ -19,11 +20,31 @@ class enum_static
1920
return value_to_name;
2021
}
2122

22-
static const auto iterable_by_name()
23+
static const auto iterable_by_names()
2324
{
2425
return value_to_name;
2526
}
2627

28+
static const auto description()
29+
{
30+
return enum_desc;
31+
}
32+
33+
static _ETy flag_set(_ETy value, _ETy flag)
34+
{
35+
return static_cast<_UTy>(value) | static_cast<_UTy>(flag);
36+
}
37+
38+
static _ETy flag_remove(_ETy value, _ETy flag)
39+
{
40+
return static_cast<_UTy>(value) & ~static_cast<_UTy>(flag);
41+
}
42+
43+
static _ETy flag_toggle(_ETy value, _ETy flag)
44+
{
45+
return static_cast<_UTy>(value) ^ static_cast<_UTy>(flag);
46+
}
47+
2748
template<typename Ty>
2849
requires std::is_same_v<_ETy, Ty> || std::is_same_v<_UTy, Ty>
2950
static bool has_flag(Ty value, _UTy other)
@@ -154,12 +175,12 @@ class enum_value
154175

155176
bool has_flag(_UTy other) const
156177
{
157-
return has_flag(_value, other);
178+
return enum_static<_ETy>::has_flag(_value, other);
158179
}
159180

160181
bool has_flag(_ETy other) const
161182
{
162-
return has_flag(other, static_cast<_UTy>(other));
183+
return enum_static<_ETy>::has_flag(other, static_cast<_UTy>(other));
163184
}
164185

165186
enum_value& operator=(const enum_value& other)
@@ -180,17 +201,17 @@ class enum_value
180201

181202
void flag_set(_ETy flag)
182203
{
183-
_value |= static_cast<_UTy>(flag);
204+
_value = enum_static<_ETy>::flag_set(_value, flag);
184205
}
185206

186207
void flag_remove(_ETy flag)
187208
{
188-
_value &= ~static_cast<_UTy>(flag);
209+
_value = enum_static<_ETy>::flag_remove(_value, flag);
189210
}
190211

191212
void flag_toggle(_ETy flag)
192213
{
193-
_value ^= static_cast<_UTy>(flag);
214+
_value = enum_static<_ETy>::flag_toggle(_value, flag);
194215
}
195216

196217
enum_value& operator++()

enum_values/tester.cpp

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,60 +30,88 @@ enum class yourvalues : int
3030
five,
3131
};
3232

33-
enum class theirvalues : int //ns@ourvalues
33+
namespace espace
3434
{
35-
zero, // nope
36-
one,
37-
two,
38-
also_two = two, // does it work?
39-
three, // ah!!
40-
four,
41-
five,
42-
};
35+
enum class theirvalues : int //ns@espace
36+
{
37+
zero, // nope
38+
one,
39+
two,
40+
also_two = two, // does it work?
41+
three, // ah!!
42+
four,
43+
five,
44+
};
45+
}
4346

44-
void that(myvalues v)
47+
void foo(myvalues v)
4548
{
46-
v = myvalues::also_three;
49+
v = myvalues::also_three; // this line brought to you by compiler warnings
4750
}
4851

4952
int main()
5053
{
54+
// get description
55+
std::cout << enum_static<myvalues>::description() << std::endl;
56+
std::cout << enum_static<espace::theirvalues>::description() << std::endl;
57+
58+
// using the enum_value class to hold values (useful for flags
5159
enum_value<myvalues> val1;
5260
enum_value<myvalues> val2;
5361

62+
// reset to 0
63+
val1.clear();
64+
65+
// assign to enum
5466
val1 = myvalues::two;
67+
68+
// assignment by data()
5569
val1.data() = 10;
56-
val1.clear();
70+
val2.data() = 5;
71+
72+
// assignment by enum directly
73+
*val1 = myvalues::three;
74+
5775
auto ok = (val1.data() == 5);
76+
77+
// comparison of two enum_values
5878
ok = (val1 == val2);
79+
80+
// comparison by enum
5981
ok = (val1 == myvalues::three);
82+
83+
// enum_value asignment
6084
val1 = val2;
61-
val2.data() = 5;
62-
*val1 = myvalues::three;
6385

6486
auto c = val1.data();
6587
std::cout << c << std::endl;
6688

67-
for (auto v : enum_static<myvalues>::iterable_by_name())
89+
// iterate over full list of values
90+
for (auto v : enum_static<myvalues>::iterable_by_names())
6891
{
6992
std::cout << v.second << std::endl;
7093
}
7194

95+
// assign by string
7296
val1.from_string("zero");
7397
std::cout << val1.to_string() << std::endl;
7498

99+
// assign by string flag
75100
auto unknowns = val1.from_flag_string("one!+!two!+!four", "!+!");
76101
assert(unknowns.empty());
77102

103+
// detect unknown flag text
78104
unknowns = val1.from_flag_string("one+two+four", "!+!");
79105
assert(unknowns == "one+two+four");
80106

81107
unknowns = val1.from_flag_string("eight+sixteen+two+four+thirty-two", "+");
82108
assert(unknowns == "eight+sixteen+thirty-two");
83109

110+
// to flag string
84111
std::cout << val1.to_flag_string("+") << std::endl;
85112

86-
that(*val1);
113+
// pass enum direct
114+
foo(*val1);
87115

88116
return 0;
89117
}

0 commit comments

Comments
 (0)