Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use locale;
- use strict;
- if (scalar(@ARGV)<2) {
- print "usage: " . $0 . " 'className' 'type1 var1;type2 var2;typeN varN'\n";
- print "example: " . $0 . " PhoneRecord 'char* name;char* phoneNumber;int age'\n";
- exit(0);
- }
- my ($class,$params) = @ARGV;
- my %args = map { $_ =~ m/(.+)\s(.+)/; $2 => $1 } split(";",$params);
- #print $_ . "|" for split(";",$params);
- #print keys %args; exit(0);
- sub loc {
- my ($g) = @_;
- return "m" . ucfirst($g);
- }
- sub decl {
- my ($c,$t,$n) = @_;
- return $t . ' ' . loc($n) . ';';
- }
- sub getter {
- my ($c,$t,$n) = @_;
- my $declaration = $t . ' ' . $n . "() const;";
- my $implementation = $t . ' ' . $c . '::' . $n . "() const {\n\treturn " . loc($n) . ";\n}";
- return ($declaration,$implementation);
- }
- sub setter {
- my ($c,$t,$n) = @_;
- my $ref = 1;
- my @valuetypes = ('int','float','double','bool','char');
- for (@valuetypes) {
- if ($t eq $_) {
- $ref = 0;
- }
- }
- if (substr($t,-1) eq '*') {
- $ref = 0;
- }
- my $declaration = '';
- my $implementation = '';
- if ($ref) {
- $declaration = 'void set' . ucfirst($n) . '(const ' . $t . "& value);";
- $implementation = 'void ' . $c . '::set' . ucfirst($n) . '(const ' . $t . "& value) {\n\t" . loc($n) . "=value;\n}";
- } else {
- $declaration = 'void set' . ucfirst($n) . '(' . $t . " value);";
- $implementation = 'void ' . $c . '::set' . ucfirst($n) . '(' . $t . " value) {\n\t" . loc($n) . "=value;\n}";
- }
- return ($declaration,$implementation);
- }
- my @p0 = ();
- my @p1 = ();
- my @p2 = ();
- my @p3 = ();
- my @p4 = ();
- for my $n (keys %args) {
- push(@p0,decl($class,$args{$n},$n));
- my @di = getter($class,$args{$n},$n);
- push(@p1,$di[0]);
- push(@p3,$di[1]);
- @di = setter($class,$args{$n},$n);
- push(@p2,$di[0]);
- push(@p4,$di[1]);
- }
- my $spl = "\n/************* *************/\n";
- my @p = (@p0,$spl,@p1,$spl,@p2,$spl,@p3,$spl,@p4);
- print $_ . "\n" foreach @p;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement