From 115e8dc4c5042d805ce929530bb10b05d6fbf250 Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Mon, 20 Mar 2023 13:27:25 +0300 Subject: Add support for std::multimap --- cli-examples/features/driver.cxx | 25 +++++++++++--- cli-examples/features/options.cli | 7 ++-- cli/cli/pregenerated/cli/options.cxx | 50 ++++++++++++++++++++++++++++ cli/cli/runtime-source.cxx | 64 ++++++++++++++++++++++++++++++++++++ 4 files changed, 138 insertions(+), 8 deletions(-) diff --git a/cli-examples/features/driver.cxx b/cli-examples/features/driver.cxx index a6299b9..fc364a0 100644 --- a/cli-examples/features/driver.cxx +++ b/cli-examples/features/driver.cxx @@ -2,6 +2,7 @@ // author : Boris Kolpackov // license : MIT; see accompanying LICENSE file +#include // pair #include #include #include @@ -45,13 +46,27 @@ main (int argc, char* argv[]) // --map | -m // - typedef map str_map; - const str_map& m = o.map (); - str_map::const_iterator i (m.find ("a")); + { + typedef map str_map; + const str_map& m = o.map (); + str_map::const_iterator i (m.find ("a")); - if (i != m.end ()) - cerr << "value for the 'a' key: " << i->second << endl; + if (i != m.end ()) + cerr << "value for the 'a' map key: " << i->second << endl; + } + // --multimap + // + { + typedef multimap str_multimap; + const str_multimap& m = o.multimap (); + + pair r ( + m.equal_range ("a")); + + for (str_multimap::const_iterator i (r.first); i != r.second; ++i) + cerr << "value for the 'a' multimap key: " << i->second << endl; + } } catch (const cli::exception& e) { diff --git a/cli-examples/features/options.cli b/cli-examples/features/options.cli index 4065830..d1e4b0c 100644 --- a/cli-examples/features/options.cli +++ b/cli-examples/features/options.cli @@ -30,10 +30,11 @@ namespace features std::vector --vector | -v; std::set --set | -s; - // We can also use maps. In this case the option value is expected to have - // two parts: the key and the value, separated by '='. For example: -m a=1 - // -m =true -m c= -m d (same as -m d=). + // We can also use maps and multimaps. In this case the option value is + // expected to have two parts: the key and the value, separated by '='. + // For example: -m a=1 -m =true -m c= -m d (same as -m d=). // std::map --map | -m; + std::multimap --multimap; }; } diff --git a/cli/cli/pregenerated/cli/options.cxx b/cli/cli/pregenerated/cli/options.cxx index 99aa50d..9f0be5f 100644 --- a/cli/cli/pregenerated/cli/options.cxx +++ b/cli/cli/pregenerated/cli/options.cxx @@ -676,6 +676,56 @@ namespace cli } }; + template + struct parser > + { + static void + parse (std::multimap& m, bool& xs, scanner& s) + { + const char* o (s.next ()); + + if (s.more ()) + { + std::size_t pos (s.position ()); + std::string ov (s.next ()); + std::string::size_type p = ov.find ('='); + + K k = K (); + V v = V (); + std::string kstr (ov, 0, p); + std::string vstr (ov, (p != std::string::npos ? p + 1 : ov.size ())); + + int ac (2); + char* av[] = + { + const_cast (o), + 0 + }; + + bool dummy; + if (!kstr.empty ()) + { + av[1] = const_cast (kstr.c_str ()); + argv_scanner s (0, ac, av, false, pos); + parser::parse (k, dummy, s); + } + + if (!vstr.empty ()) + { + av[1] = const_cast (vstr.c_str ()); + argv_scanner s (0, ac, av, false, pos); + parser::parse (v, dummy, s); + } + + m.insert (typename std::multimap::value_type (k, v)); + } + else + throw missing_value (o); + + xs = true; + } + }; + template void thunk (X& x, scanner& s) diff --git a/cli/cli/runtime-source.cxx b/cli/cli/runtime-source.cxx index 9620648..912027a 100644 --- a/cli/cli/runtime-source.cxx +++ b/cli/cli/runtime-source.cxx @@ -1216,6 +1216,70 @@ generate_runtime_source (context& ctx, bool complete) os << "};"; + // parser> + // + os << "template " << endl + << "struct parser >" + << "{"; + + os << "static void" << endl + << "parse (std::multimap& m, " << (sp ? "bool& xs, " : "") << "scanner& s)" + << "{" + << "const char* o (s.next ());" + << endl + << "if (s.more ())" + << "{" + << "std::size_t pos (s.position ());" + << "std::string ov (s.next ());" + << "std::string::size_type p = ov.find ('=');" + << endl + << "K k = K ();" + << "V v = V ();" + << "std::string kstr (ov, 0, p);" + << "std::string vstr (ov, (p != std::string::npos ? p + 1 : ov.size ()));" + << endl + << "int ac (2);" + << "char* av[] =" + << "{" + << "const_cast (o)," << endl + << "0" + << "};"; + if (sp) + os << "bool dummy;"; + os << "if (!kstr.empty ())" + << "{" + << "av[1] = const_cast (kstr.c_str ());" + << "argv_scanner s (0, ac, av, false, pos);" + << "parser::parse (k, " << (sp ? "dummy, " : "") << "s);" + << "}" + << "if (!vstr.empty ())" + << "{" + << "av[1] = const_cast (vstr.c_str ());" + << "argv_scanner s (0, ac, av, false, pos);" + << "parser::parse (v, " << (sp ? "dummy, " : "") << "s);" + << "}" + << "m.insert (typename std::multimap::value_type (k, v));" + << "}" + << "else" << endl + << "throw missing_value (o);"; + if (sp) + os << endl + << "xs = true;"; + os << "}"; + + if (gen_merge) + os << "static void" << endl + << "merge (std::multimap& b, const std::multimap& a)" + << "{" + << "for (typename std::multimap::const_iterator i (a.begin ()); " << endl + << "i != a.end (); " << endl + << "++i)" << endl + << "b.insert (typename std::multimap::value_type (i->first," << endl + << "i->second));" << endl + << "}"; + + os << "};"; + // Parser thunk. // os << "template " << endl -- cgit v1.1