# HG changeset patch # User John W. Eaton # Date 1294903418 18000 # Node ID 7bddd70bc838902b6a710a7b77493c1f4203b2c1 # Parent aee00336a440c6a9ee4f36e6d760ea4073c9eb54 mk-opts.pl: generate initialization lists and in-line code for copy method diff -r aee00336a440 -r 7bddd70bc838 ChangeLog --- a/ChangeLog Thu Jan 13 02:19:50 2011 -0500 +++ b/ChangeLog Thu Jan 13 02:23:38 2011 -0500 @@ -1,3 +1,8 @@ +2011-01-12 John W. Eaton + + * mk-opts.pl: Generate initialization lists and in-line code for + copying. + 2010-01-11 Rik * ROADMAP: Update guide to Octave directory structure. diff -r aee00336a440 -r 7bddd70bc838 mk-opts.pl --- a/mk-opts.pl Thu Jan 13 02:19:50 2011 -0500 +++ b/mk-opts.pl Thu Jan 13 02:23:38 2011 -0500 @@ -360,6 +360,63 @@ } } +sub emit_copy_body +{ + local ($pfx, $var) = @_; + + for ($i = 0; $i < $opt_num; $i++) + { + print "${pfx}$optvar[$i] = ${var}.$optvar[$i];\n"; + } + + print "${pfx}reset = ${var}.reset;\n"; +} + +## To silence GCC warnings, we create an initialization list even +## though the init function actually does the work of initialization. + +sub emit_default_init_list +{ + local ($prefix) = @_; + + for ($i = 0; $i < $opt_num; $i++) + { + if ($i == 0) + { + $pfx = ""; + } + else + { + $pfx = $prefix; + } + + print "${pfx}$optvar[$i] (),\n"; + } + + print "${prefix}reset ()\n"; +} + +sub emit_copy_ctor_init_list +{ + local ($prefix, $var) = @_; + + for ($i = 0; $i < $opt_num; $i++) + { + if ($i == 0) + { + $pfx = ""; + } + else + { + $pfx = $prefix; + } + + print "${pfx}$optvar[$i] ($var.$optvar[$i]),\n"; + } + + print "${prefix}reset ($var.reset)\n"; +} + sub emit_opt_class_header { local ($i, $s); @@ -380,14 +437,30 @@ { public: - ${class_name} (void) { init (); } + ${class_name} (void) + : "; + + &emit_default_init_list (" "); - ${class_name} (const ${class_name}& opt) { copy (opt); } + print " { + init (); + } + + ${class_name} (const ${class_name}& opt) + : "; + + &emit_copy_ctor_init_list (" ", "opt"); + + print " { } ${class_name}& operator = (const ${class_name}& opt) { if (this != &opt) - copy (opt); + {\n"; + + &emit_copy_body (" ", "opt"); + + print " } return *this; } @@ -415,22 +488,15 @@ print " reset = true; }\n"; - print "\n void copy (const ${class_name}& opt)\n {\n"; - - for ($i = 0; $i < $opt_num; $i++) - { - print " $optvar[$i] = opt.$optvar[$i];\n"; - } - - print " reset = opt.reset; - }\n"; - ## For backward compatibility and because set_options is probably ## a better name in some contexts: - print "\n void set_options (const ${class_name}& opt) { copy (opt); }\n"; + print "\n void set_options (const ${class_name}& opt) + {\n"; - print "\n void set_default_options (void) { init (); }\n"; + &emit_copy_body (" ", "opt"); + + print " }\n\n void set_default_options (void) { init (); }\n"; for ($i = 0; $i < $opt_num; $i++) {