Advertisement
overloop

gettersetter.pl

Oct 15th, 2014
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.89 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use locale;
  4. use strict;
  5.  
  6. if (scalar(@ARGV)<2) {
  7.     print "usage: " . $0 . " 'className' 'type1 var1;type2 var2;typeN varN'\n";
  8.     print "example: " . $0 . " PhoneRecord 'char* name;char* phoneNumber;int age'\n";
  9.     exit(0);
  10. }
  11.  
  12. my ($class,$params) = @ARGV;
  13. my %args = map { $_ =~ m/(.+)\s(.+)/; $2 => $1 } split(";",$params);
  14.  
  15. #print $_ . "|" for split(";",$params);
  16. #print keys %args; exit(0);
  17.  
  18. sub loc {
  19.     my ($g) = @_;
  20.     return "m" . ucfirst($g);
  21. }
  22.  
  23. sub decl {
  24.     my ($c,$t,$n) = @_;
  25.     return $t . ' ' . loc($n) . ';';
  26. }
  27.  
  28. sub getter {
  29.     my ($c,$t,$n) = @_;
  30.     my $declaration = $t . ' ' . $n . "() const;";
  31.     my $implementation = $t . ' '  . $c . '::' . $n . "() const {\n\treturn " . loc($n) . ";\n}";
  32.     return ($declaration,$implementation);
  33. }
  34.  
  35. sub setter {
  36.     my ($c,$t,$n) = @_;
  37.     my $ref = 1;
  38.     my @valuetypes = ('int','float','double','bool','char');
  39.     for (@valuetypes) {
  40.         if ($t eq $_) {
  41.             $ref = 0;
  42.         }
  43.     }
  44.     if (substr($t,-1) eq '*') {
  45.         $ref = 0;
  46.     }
  47.     my $declaration = '';
  48.     my $implementation = '';
  49.     if ($ref) {
  50.         $declaration = 'void set' . ucfirst($n) . '(const ' . $t . "& value);";
  51.         $implementation = 'void ' . $c . '::set' . ucfirst($n) . '(const ' . $t . "& value) {\n\t" . loc($n) . "=value;\n}";
  52.     } else {
  53.         $declaration = 'void set' . ucfirst($n) . '(' . $t . " value);";
  54.         $implementation = 'void ' . $c . '::set' . ucfirst($n) . '(' . $t . " value) {\n\t" . loc($n) . "=value;\n}";
  55.     }
  56.     return ($declaration,$implementation);
  57. }
  58.  
  59. my @p0 = ();
  60. my @p1 = ();
  61. my @p2 = ();
  62. my @p3 = ();
  63. my @p4 = ();
  64.  
  65. for my $n (keys %args) {
  66.     push(@p0,decl($class,$args{$n},$n));
  67.     my @di = getter($class,$args{$n},$n);
  68.     push(@p1,$di[0]);
  69.     push(@p3,$di[1]);
  70.     @di = setter($class,$args{$n},$n);
  71.     push(@p2,$di[0]);
  72.     push(@p4,$di[1]);
  73. }
  74. my $spl = "\n/************* *************/\n";
  75.  
  76. my @p = (@p0,$spl,@p1,$spl,@p2,$spl,@p3,$spl,@p4);
  77. print $_ . "\n" foreach @p;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement