]> mj.ucw.cz Git - paperjam.git/commitdiff
Simplify passing of arguments
authorMartin Mares <mj@ucw.cz>
Tue, 3 Apr 2018 18:04:48 +0000 (20:04 +0200)
committerMartin Mares <mj@ucw.cz>
Tue, 3 Apr 2018 18:04:48 +0000 (20:04 +0200)
cmds.cc
jam.h
parse.cc

diff --git a/cmds.cc b/cmds.cc
index 7dd8ccdb1504dfbb69533e1d0358b48d8527e9df..1c9dcd5449e35b496045610b509405a19b54f864 100644 (file)
--- 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 ca5dd9c0472f9a7e29dc91e0431eaeb826531682..47d80be2e534b5d811958d27c8225fd3d2888e00 100644 (file)
--- a/jam.h
+++ b/jam.h
@@ -6,6 +6,7 @@
 
 #include <vector>
 #include <list>
+#include <unordered_map>
 
 using namespace std;
 
@@ -47,6 +48,8 @@ public:
   virtual string dump() { return "<undef>"; }
 };
 
+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<arg_val *> args;
+  unordered_map<string, arg_val *> 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 {
index 48a32c717c0318a806ce6edfb5eb077a0a4dc06d..cc0a58235e1c35e119c5190eb9a1ebdd689ef549 100644 (file)
--- a/parse.cc
+++ b/parse.cc
@@ -188,7 +188,7 @@ public:
   string dump() { return "<string> \"" + 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; i<num_args; i++)
-    if ((adefs[i].type & AT_MANDATORY) && !c->args.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)