From 50b49b5bf24f448fbcbac99099ef9bad72f89beb Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Tue, 3 Apr 2018 20:04:48 +0200 Subject: [PATCH] Simplify passing of arguments --- cmds.cc | 16 ++++++++-------- jam.h | 13 ++++++++++++- parse.cc | 14 ++++++-------- 3 files changed, 26 insertions(+), 17 deletions(-) diff --git a/cmds.cc b/cmds.cc index 7dd8ccd..1c9dcd5 100644 --- a/cmds.cc +++ b/cmds.cc @@ -72,8 +72,8 @@ class move_cmd : public cmd_exec_simple { public: move_cmd(cmd *c) { - x = c->args.at(0)->as_double(0); - y = c->args.at(1)->as_double(0); + x = c->arg("x")->as_double(0); + y = c->arg("y")->as_double(0); } page *process_page(page *p) override { @@ -96,8 +96,8 @@ class scale_cmd : public cmd_exec_simple { public: scale_cmd(cmd *c) { - x_factor = c->args.at(0)->as_double(1); - y_factor = c->args.at(1)->as_double(x_factor); + x_factor = c->arg("x")->as_double(1); + y_factor = c->arg("y")->as_double(x_factor); } page *process_page(page *p) override { @@ -120,7 +120,7 @@ class rotate_cmd : public cmd_exec_simple { public: rotate_cmd(cmd *c) { - deg = c->args.at(0)->as_int(0) % 360; + deg = c->arg("deg")->as_int(0) % 360; if (deg < 0) deg += 360; if (deg % 90) @@ -165,8 +165,8 @@ class flip_cmd : public cmd_exec_simple { public: flip_cmd(cmd *c) { - horizontal = c->args.at(0)->as_int(0); - vertical = c->args.at(1)->as_int(0); + horizontal = c->arg("h")->as_int(0); + vertical = c->arg("v")->as_int(0); if (!horizontal && !vertical) die("Flip has no direction specified"); } @@ -292,7 +292,7 @@ class modulo_cmd : public cmd_exec { public: modulo_cmd(cmd *c) { - n = c->args.at(0)->as_int(0); + n = c->arg("n")->as_int(0); if (n <= 0) die("Modulo must have n > 0"); pipe = c->pipe; diff --git a/jam.h b/jam.h index ca5dd9c..47d80be 100644 --- a/jam.h +++ b/jam.h @@ -6,6 +6,7 @@ #include #include +#include using namespace std; @@ -47,6 +48,8 @@ public: virtual string dump() { return ""; } }; +extern arg_val null_arg; + struct out_context { QPDFObjectHandle resources; QPDFObjectHandle xobjects; @@ -89,9 +92,17 @@ struct cmd_def { struct cmd { const cmd_def *def; - vector args; + unordered_map args; pipeline *pipe; cmd_exec *exec; + arg_val *arg(const string name) + { + auto it = args.find(name); + if (it != args.end()) + return it->second; + else + return &null_arg; + } }; struct pipeline_selector { diff --git a/parse.cc b/parse.cc index 48a32c7..cc0a582 100644 --- a/parse.cc +++ b/parse.cc @@ -188,7 +188,7 @@ public: string dump() { return " \"" + val + '"'; } }; -static arg_val null_arg; +arg_val null_arg; /*** Parser ***/ @@ -287,8 +287,6 @@ static void parse_args(cmd *c) while (adefs[num_args].name) num_args++; - c->args.resize(num_args, &null_arg); - token_type t = next_token(); if (t != TOK_OPEN_PAREN) { @@ -311,7 +309,7 @@ static void parse_args(cmd *c) argi++; if (!adefs[argi].name) parse_error("Command %s has no parameter %s", cdef->name, token.c_str()); - if (c->args.at(argi)->given()) + if (c->args.count(token)) parse_error("Parameter %s given multiple times", token.c_str()); t = next_token(); if (t == TOK_EQUAL) @@ -379,7 +377,7 @@ static void parse_args(cmd *c) parse_error("Parameter %s must have a value", adef->name); } - c->args.at(argi) = val; + c->args[adef->name] = val; t = next_token(); if (t == TOK_CLOSE_PAREN) @@ -389,17 +387,17 @@ static void parse_args(cmd *c) } for (uint i=0; iargs.at(i)->given()) + if ((adefs[i].type & AT_MANDATORY) && !c->args.count(adefs[i].name)) parse_error("Command %s is missing a parameter %s", cdef->name, adefs[i].name); } static void debug_cmd(cmd *c, uint indent=0) { printf("%*sCommand %s\n", indent, "", c->def->name); - for (size_t i=0; i < c->args.size(); i++) + for (uint i=0; c->def->arg_defs[i].name; i++) { const arg_def *adef = &c->def->arg_defs[i]; - string dump = c->args.at(i)->dump(); + string dump = c->arg(adef->name)->dump(); printf("%*sArg #%d: %s = %s\n", indent+4, "", (int) i, adef->name, dump.c_str()); } if (c->pipe) -- 2.39.2