From ad9225455474799c73e00eff9ba71c47573591bd Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Mon, 26 Aug 2019 12:18:37 +1000 Subject: [PATCH] parse/enum: add support fro enum classes --- parse/enum.hpp | 4 +++- test/parse/enum.cpp | 54 ++++++++++++++++++++++++++++++--------------- 2 files changed, 39 insertions(+), 19 deletions(-) diff --git a/parse/enum.hpp b/parse/enum.hpp index 257f0190..0b39caba 100644 --- a/parse/enum.hpp +++ b/parse/enum.hpp @@ -53,7 +53,9 @@ namespace cruft::parse::enumeration { for (auto const [key,val]: m_cache) { if (equal (key, str)) { str = str.consume (key.size ()); - return val; + // We must cast the value here so that enum + // classes are supported. + return static_cast (val); } } diff --git a/test/parse/enum.cpp b/test/parse/enum.cpp index 9be1fbd1..4be58f44 100644 --- a/test/parse/enum.cpp +++ b/test/parse/enum.cpp @@ -5,27 +5,45 @@ enum enumeration_t : u16 { FOO, BAR = 2, QUX = 257 }; +enum class enum_class { VALUE }; + int main () { - auto const cookie = cruft::parse::enumeration::setup ({ - { "FOO", FOO }, - { "BAR", BAR }, - { "QUX", QUX }, - }); - (void)cookie; - cruft::TAP::logger tap; - tap.expect_eq (FOO, cruft::parse::from_string ("FOO"), "enumeration, FOO"); - tap.expect_eq (BAR, cruft::parse::from_string ("BAR"), "enumeration, BAR"); - tap.expect_eq (QUX, cruft::parse::from_string ("QUX"), "enumeration, QUX"); - tap.expect_eq ( - u16 {QUX}, - cruft::parse::enumeration::from_string ( - cruft::typeidx (), - "QUX" - ), - "u16, QUX" - ); + { + auto const cookie = cruft::parse::enumeration::setup ({ + { "FOO", FOO }, + { "BAR", BAR }, + { "QUX", QUX }, + }); + (void)cookie; + + tap.expect_eq (FOO, cruft::parse::from_string ("FOO"), "enumeration, FOO"); + tap.expect_eq (BAR, cruft::parse::from_string ("BAR"), "enumeration, BAR"); + tap.expect_eq (QUX, cruft::parse::from_string ("QUX"), "enumeration, QUX"); + + tap.expect_eq ( + u16 {QUX}, + cruft::parse::enumeration::from_string ( + cruft::typeidx (), + "QUX" + ), + "u16, QUX" + ); + } + + { + auto const cookie = cruft::parse::enumeration::setup ({ + { "VALUE", enum_class::VALUE }, + }); + (void)cookie; + + tap.expect_eq ( + enum_class::VALUE, + cruft::parse::from_string ("VALUE"), "enum class, VALUE" + ); + } + return tap.status (); } \ No newline at end of file