]> mj.ucw.cz Git - paperjam.git/commitdiff
Simplify construction of command objects
authorMartin Mares <mj@ucw.cz>
Tue, 3 Apr 2018 17:48:48 +0000 (19:48 +0200)
committerMartin Mares <mj@ucw.cz>
Tue, 3 Apr 2018 17:48:48 +0000 (19:48 +0200)
cmds.cc

diff --git a/cmds.cc b/cmds.cc
index 9ca7637d684dd1966615086ebf068f14e35643f5..7dd8ccdb1504dfbb69533e1d0358b48d8527e9df 100644 (file)
--- a/cmds.cc
+++ b/cmds.cc
@@ -13,6 +13,8 @@
 /*** null ***/
 
 class null_cmd : public cmd_exec {
+public:
+  null_cmd(cmd *c UNUSED) { }
   vector<page *> process(vector<page *> &pages) override { return pages; }
 };
 
@@ -20,11 +22,6 @@ static const arg_def no_args[] = {
   { NULL,      0 }
 };
 
-static cmd_exec *null_ctor(cmd *c UNUSED)
-{
-  return new null_cmd;
-}
-
 /*** Generic transformed page ***/
 
 class xform_page : public page {
@@ -71,8 +68,13 @@ vector<page *> cmd_exec_simple::process(vector<page *> &pages)
 /*** move ***/
 
 class move_cmd : public cmd_exec_simple {
-public:
   double x, y;
+public:
+  move_cmd(cmd *c)
+    {
+      x = c->args.at(0)->as_double(0);
+      y = c->args.at(1)->as_double(0);
+    }
   page *process_page(page *p) override
     {
       pdf_matrix m;
@@ -87,19 +89,16 @@ static const arg_def move_args[] = {
   { NULL,      0 }
 };
 
-static cmd_exec *move_ctor(cmd *c)
-{
-  move_cmd *m = new move_cmd;
-  m->x = c->args.at(0)->as_double(0);
-  m->y = c->args.at(1)->as_double(0);
-  return m;
-}
-
 /*** scale ***/
 
 class scale_cmd : public cmd_exec_simple {
-public:
   double x_factor, y_factor;
+public:
+  scale_cmd(cmd *c)
+    {
+      x_factor = c->args.at(0)->as_double(1);
+      y_factor = c->args.at(1)->as_double(x_factor);
+    }
   page *process_page(page *p) override
     {
       pdf_matrix m;
@@ -114,19 +113,19 @@ static const arg_def scale_args[] = {
   { NULL,      0 }
 };
 
-static cmd_exec *scale_ctor(cmd *c)
-{
-  scale_cmd *s = new scale_cmd;
-  s->x_factor = c->args.at(0)->as_double(1);
-  s->y_factor = c->args.at(1)->as_double(s->x_factor);
-  return s;
-}
-
 /*** rotate ***/
 
 class rotate_cmd : public cmd_exec_simple {
-public:
   int deg;
+public:
+  rotate_cmd(cmd *c)
+    {
+      deg = c->args.at(0)->as_int(0) % 360;
+      if (deg < 0)
+       deg += 360;
+      if (deg % 90)
+       die("Rotate requires a multiple of 90 degrees");
+    }
   page *process_page(page *p) override
     {
       pdf_matrix m;
@@ -158,23 +157,19 @@ static const arg_def rotate_args[] = {
   { NULL,      0 }
 };
 
-static cmd_exec *rotate_ctor(cmd *c)
-{
-  rotate_cmd *r = new rotate_cmd;
-  r->deg = c->args.at(0)->as_int(0) % 360;
-  if (r->deg < 0)
-    r->deg += 360;
-  if (r->deg % 90)
-    die("Rotate requires a multiple of 90 degrees");
-  return r;
-}
-
 /*** flip ***/
 
 class flip_cmd : public cmd_exec_simple {
-public:
   bool horizontal;
   bool vertical;
+public:
+  flip_cmd(cmd *c)
+    {
+      horizontal = c->args.at(0)->as_int(0);
+      vertical = c->args.at(1)->as_int(0);
+      if (!horizontal && !vertical)
+       die("Flip has no direction specified");
+    }
   page *process_page(page *p) override
     {
       pdf_matrix m;
@@ -198,21 +193,15 @@ static const arg_def flip_args[] = {
   { NULL,      0 }
 };
 
-static cmd_exec *flip_ctor(cmd *c)
-{
-  flip_cmd *f = new flip_cmd;
-  f->horizontal = c->args.at(0)->as_int(0);
-  f->vertical = c->args.at(1)->as_int(0);
-  if (!f->horizontal && !f->vertical)
-    die("Flip has no direction specified");
-  return f;
-}
-
 /*** select ***/
 
 class select_cmd : public cmd_exec {
-public:
   pipeline *pipe;
+public:
+  select_cmd(cmd *c)
+    {
+      pipe = c->pipe;
+    }
   vector<page *> process(vector<page *> &pages);
 };
 
@@ -246,18 +235,15 @@ vector<page *> select_cmd::process(vector<page *> &pages)
   return out;
 }
 
-static cmd_exec *select_ctor(cmd *c)
-{
-  select_cmd *r = new select_cmd;
-  r->pipe = c->pipe;
-  return r;
-}
-
 /*** apply ***/
 
 class apply_cmd : public cmd_exec {
-public:
   pipeline *pipe;
+public:
+  apply_cmd(cmd *c)
+    {
+      pipe = c->pipe;
+    }
   vector<page *> process(vector<page *> &pages);
 };
 
@@ -298,19 +284,19 @@ vector<page *> apply_cmd::process(vector<page *> &pages)
   return out;
 }
 
-static cmd_exec *apply_ctor(cmd *c)
-{
-  apply_cmd *r = new apply_cmd;
-  r->pipe = c->pipe;
-  return r;
-}
-
 /*** modulo ***/
 
 class modulo_cmd : public cmd_exec {
-public:
   pipeline *pipe;
   int n;
+public:
+  modulo_cmd(cmd *c)
+    {
+      n = c->args.at(0)->as_int(0);
+      if (n <= 0)
+       die("Modulo must have n > 0");
+      pipe = c->pipe;
+    }
   vector<page *> process(vector<page *> &pages);
 };
 
@@ -364,20 +350,11 @@ static const arg_def modulo_args[] = {
   { NULL,      0 }
 };
 
-static cmd_exec *modulo_ctor(cmd *c)
-{
-  modulo_cmd *m = new modulo_cmd;
-  m->n = c->args.at(0)->as_int(0);
-  if (m->n <= 0)
-    die("Modulo must have n > 0");
-  m->pipe = c->pipe;
-  return m;
-}
-
 /*** draw_bbox ***/
 
 class draw_bbox_cmd : public cmd_exec {
 public:
+  draw_bbox_cmd(cmd *c UNUSED) { }
   vector<page *> process(vector<page *> &pages);
 };
 
@@ -407,23 +384,19 @@ vector<page *> draw_bbox_cmd::process(vector<page *> &pages)
   return out;
 }
 
-static cmd_exec *draw_bbox_ctor(cmd *c UNUSED)
-{
-  draw_bbox_cmd *m = new draw_bbox_cmd;
-  return m;
-}
-
 /*** Command table ***/
 
+template<typename T> cmd_exec *ctor(cmd *c) { return new T(c); }
+
 const cmd_def cmd_table[] = {
-  { "null",    no_args,        0,      null_ctor       },
-  { "move",    move_args,      0,      move_ctor       },
-  { "scale",   scale_args,     0,      scale_ctor      },
-  { "rotate",  rotate_args,    0,      rotate_ctor     },
-  { "flip",    flip_args,      0,      flip_ctor       },
-  { "select",  no_args,        1,      select_ctor     },
-  { "apply",   no_args,        1,      apply_ctor      },
-  { "modulo",  modulo_args,    1,      modulo_ctor     },
-  { "draw_bbox",no_args,       0,      draw_bbox_ctor  },
+  { "null",    no_args,        0,      &ctor<null_cmd>         },
+  { "move",    move_args,      0,      &ctor<move_cmd>         },
+  { "scale",   scale_args,     0,      &ctor<scale_cmd>        },
+  { "rotate",  rotate_args,    0,      &ctor<rotate_cmd>       },
+  { "flip",    flip_args,      0,      &ctor<flip_cmd>         },
+  { "select",  no_args,        1,      &ctor<select_cmd>       },
+  { "apply",   no_args,        1,      &ctor<apply_cmd>        },
+  { "modulo",  modulo_args,    1,      &ctor<modulo_cmd>       },
+  { "draw_bbox",no_args,       0,      &ctor<draw_bbox_cmd>    },
   { NULL,      NULL,           0,      NULL    }
 };