parse/enum: add support fro enum classes

This commit is contained in:
Danny Robson 2019-08-26 12:18:37 +10:00
parent 6367596537
commit ad92254554
2 changed files with 39 additions and 19 deletions

View File

@ -53,7 +53,9 @@ namespace cruft::parse::enumeration {
for (auto const [key,val]: m_cache) { for (auto const [key,val]: m_cache) {
if (equal (key, str)) { if (equal (key, str)) {
str = str.consume (key.size ()); str = str.consume (key.size ());
return val; // We must cast the value here so that enum
// classes are supported.
return static_cast<ValueT> (val);
} }
} }

View File

@ -5,27 +5,45 @@
enum enumeration_t : u16 { FOO, BAR = 2, QUX = 257 }; enum enumeration_t : u16 { FOO, BAR = 2, QUX = 257 };
enum class enum_class { VALUE };
int main () int main ()
{ {
auto const cookie = cruft::parse::enumeration::setup<enumeration_t> ({
{ "FOO", FOO },
{ "BAR", BAR },
{ "QUX", QUX },
});
(void)cookie;
cruft::TAP::logger tap; cruft::TAP::logger tap;
tap.expect_eq (FOO, cruft::parse::from_string<enumeration_t> ("FOO"), "enumeration, FOO");
tap.expect_eq (BAR, cruft::parse::from_string<enumeration_t> ("BAR"), "enumeration, BAR");
tap.expect_eq (QUX, cruft::parse::from_string<enumeration_t> ("QUX"), "enumeration, QUX");
tap.expect_eq ( {
u16 {QUX}, auto const cookie = cruft::parse::enumeration::setup<enumeration_t> ({
cruft::parse::enumeration::from_string<u16> ( { "FOO", FOO },
cruft::typeidx<enumeration_t> (), { "BAR", BAR },
"QUX" { "QUX", QUX },
), });
"u16, QUX" (void)cookie;
);
tap.expect_eq (FOO, cruft::parse::from_string<enumeration_t> ("FOO"), "enumeration, FOO");
tap.expect_eq (BAR, cruft::parse::from_string<enumeration_t> ("BAR"), "enumeration, BAR");
tap.expect_eq (QUX, cruft::parse::from_string<enumeration_t> ("QUX"), "enumeration, QUX");
tap.expect_eq (
u16 {QUX},
cruft::parse::enumeration::from_string<u16> (
cruft::typeidx<enumeration_t> (),
"QUX"
),
"u16, QUX"
);
}
{
auto const cookie = cruft::parse::enumeration::setup<enum_class> ({
{ "VALUE", enum_class::VALUE },
});
(void)cookie;
tap.expect_eq (
enum_class::VALUE,
cruft::parse::from_string<enum_class> ("VALUE"), "enum class, VALUE"
);
}
return tap.status (); return tap.status ();
} }