From: g0dil Date: Thu, 9 Sep 2010 10:45:49 +0000 (+0000) Subject: Utils/Console: Add SENF_CONSOLE_REGISTER_ENUM 'key()' support X-Git-Url: http://g0dil.de/git?a=commitdiff_plain;h=78a6e233083efa63a9cd0684a92abc64202a9ee7;p=senf.git Utils/Console: Add SENF_CONSOLE_REGISTER_ENUM 'key()' support git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@1711 270642c3-0616-0410-b53a-bc976706d245 --- diff --git a/senf/Packets/ParseHelpers.ih b/senf/Packets/ParseHelpers.ih index 4053ba3..6dc6bf5 100644 --- a/senf/Packets/ParseHelpers.ih +++ b/senf/Packets/ParseHelpers.ih @@ -30,6 +30,7 @@ # include # include # include +# include "../Utils/preprocessor.hh" # include "../Utils/mpl.hh" # # ////////////////////////////////ih.p/////////////////////////////////////// @@ -506,14 +507,6 @@ # define SENF_PARSER_COLLECTION_TAG_GETAUX__packetSize() _ # define SENF_PARSER_COLLECTION_TAG_AUXTYPE__packetSize() packetSize # -# // No recursive call so we need some more of theese ... ARGH !!! -# define SENF_CAT_RECURS1(a, b) SENF_CAT_RECURS1_I(a,b) -# define SENF_CAT_RECURS1_I(a, b) a ## b -# define SENF_CAT_RECURS2(a, b) SENF_CAT_RECURS2_I(a,b) -# define SENF_CAT_RECURS2_I(a, b) a ## b -# define SENF_CAT_RECURS3(a, b) SENF_CAT_RECURS3_I(a,b) -# define SENF_CAT_RECURS3_I(a, b) a ## b -# # define SENF_PARSER_COLLECTION_TAG_EXPAND__none() senf::detail::auxtag::none # # define SENF_PARSER_COLLECTION_TAG_RECURS1(aux) \ diff --git a/senf/Utils/Console/Traits.ih b/senf/Utils/Console/Traits.ih index f979e9e..a596ec2 100644 --- a/senf/Utils/Console/Traits.ih +++ b/senf/Utils/Console/Traits.ih @@ -82,8 +82,29 @@ namespace detail { long parseEnum(EnumTable const & table, ParseCommandInfo::TokensRange const & tokens); std::string formatEnum(EnumTable const & table, long value); -# define SENF_CONSOLE_REGISTER_ENUM_ELT(r,d,e) \ - ( BOOST_PP_STRINGIZE(e), static_cast(d e) ) +# define SENF_CONSOLE_REGISTER_ENUM_ELT(r,d,e) \ + BOOST_PP_IF( SENF_CONSOLE_REGISTER_ENUM_HASKEY(e), \ + SENF_CONSOLE_REGISTER_ENUM_WITHKEY, \ + SENF_CONSOLE_REGISTER_ENUM_NOKEY )(d, e) + +# define SENF_CONSOLE_REGISTER_ENUM_GOBBLE__key(k,e) +# define SENF_CONSOLE_REGISTER_ENUM_GET_KEY__key(k,e) k +# define SENF_CONSOLE_REGISTER_ENUM_GET_ENUM__key(k,e) e +# define SENF_CONSOLE_REGISTER_ENUM_HASKEY(e) \ + BOOST_PP_IS_EMPTY( SENF_CAT_RECURS1(SENF_CONSOLE_REGISTER_ENUM_GOBBLE__, e) ) + +# define SENF_CONSOLE_REGISTER_ENUM_KEY_GETENUM(e) \ + SENF_CAT_RECURS2(SENF_CONSOLE_REGISTER_ENUM_GET_ENUM__, e) +# define SENF_CONSOLE_REGISTER_ENUM_KEY_GETKEY(e) \ + SENF_CAT_RECURS3(SENF_CONSOLE_REGISTER_ENUM_GET_KEY__, e) + +# define SENF_CONSOLE_REGISTER_ENUM_NOKEY(prefix, e) \ + ( BOOST_PP_STRINGIZE(e), static_cast(prefix e) ) + +# define SENF_CONSOLE_REGISTER_ENUM_WITHKEY(prefix, e) \ + ( SENF_CONSOLE_REGISTER_ENUM_KEY_GETKEY(e), \ + static_cast(prefix SENF_CONSOLE_REGISTER_ENUM_KEY_GETENUM(e)) ) + # define SENF_CONSOLE_REGISTER_ENUM_(Prefix, Type, Values) \ inline senf::console::detail::EnumTable & senf_console_enum_table(Prefix Type) \ diff --git a/senf/Utils/Console/Traits.test.cc b/senf/Utils/Console/Traits.test.cc index 871037e..00e3d11 100644 --- a/senf/Utils/Console/Traits.test.cc +++ b/senf/Utils/Console/Traits.test.cc @@ -52,6 +52,11 @@ namespace { static MemberEnum test (MemberEnum value) { return value; } }; SENF_CONSOLE_REGISTER_ENUM_MEMBER( TestClass, MemberEnum, (MemberFoo)(MemberBar) ); + + enum TestEnumKey { ONE, TWO, THREE }; + SENF_CONSOLE_REGISTER_ENUM( TestEnumKey, (key("1s",ONE))(key("2",TWO))(key("three",THREE)) ); + + TestEnumKey testKey (TestEnumKey value) { return value; } } SENF_AUTO_UNIT_TEST(charTraits) @@ -147,6 +152,7 @@ SENF_AUTO_UNIT_TEST(enumSupport) senf::console::root().add("test", dir); dir.add("test",fty::Command(&test)); + dir.add("testKey",fty::Command(&testKey)); std::stringstream ss; SENF_CHECK_NO_THROW( @@ -201,6 +207,24 @@ SENF_AUTO_UNIT_TEST(enumSupport) parser.parse("test/test foo", boost::bind( boost::ref(executor), boost::ref(ss), _1 )) ); BOOST_CHECK_EQUAL( ss.str(), "Foo\n" ); + + ss.str(""); + SENF_CHECK_NO_THROW( + parser.parse("test/testKey 1S", + boost::bind( boost::ref(executor), boost::ref(ss), _1 )) ); + BOOST_CHECK_EQUAL( ss.str(), "1s\n" ); + + ss.str(""); + SENF_CHECK_NO_THROW( + parser.parse("test/testKey 2", + boost::bind( boost::ref(executor), boost::ref(ss), _1 )) ); + BOOST_CHECK_EQUAL( ss.str(), "2\n" ); + + ss.str(""); + SENF_CHECK_NO_THROW( + parser.parse("test/testKey ThrEE", + boost::bind( boost::ref(executor), boost::ref(ss), _1 )) ); + BOOST_CHECK_EQUAL( ss.str(), "three\n" ); } SENF_AUTO_UNIT_TEST(singleToken) diff --git a/senf/Utils/preprocessor.hh b/senf/Utils/preprocessor.hh index b358f04..0e4d733 100644 --- a/senf/Utils/preprocessor.hh +++ b/senf/Utils/preprocessor.hh @@ -47,6 +47,15 @@ */ #define SENF_PP_SEQ_BACK(seq) BOOST_PP_SEQ_ELEM(BOOST_PP_DEC(BOOST_PP_SEQ_SIZE(seq)),seq) +# // No recursive call so we need some more of theese ... ARGH !!! +# define SENF_CAT_RECURS1(a, b) SENF_CAT_RECURS1_I(a,b) +# define SENF_CAT_RECURS1_I(a, b) a ## b +# define SENF_CAT_RECURS2(a, b) SENF_CAT_RECURS2_I(a,b) +# define SENF_CAT_RECURS2_I(a, b) a ## b +# define SENF_CAT_RECURS3(a, b) SENF_CAT_RECURS3_I(a,b) +# define SENF_CAT_RECURS3_I(a, b) a ## b +# + ///\} ///////////////////////////////hh.e////////////////////////////////////////