Use Perl to generate "automatic attributes" for C ++"

Source: Internet
Author: User

If you use vs to use C #, you should know that there is an automatic attribute, which is similar

public int MyProperty { get; set; }

It automatically generates get/set methods for you and binds these methods to this property. It is easy to say that this function is not available in C ++ IDE, and we need to write c ++

     _declspec(property(put=setJobID, get=getJobID))        unsigned long jobID;    void            setJobID(unsigned long inJobID);    unsigned long   getJobID() const;

With so much code to do these things, I wrote a simple script using Perl, which is automatically generated for me based on the variable type, variable name, and variable read/write attributes.

Prerequisites: 1. Perl is installed on your computer and can be used properly. 2.20.a field.txt, each line in it contains the variable type, variable name, variable read/write attributes such as string, version, RW unsigned long, length, r yourstructname, structname, W simple knowledge points: 1. IO: file or input redirection is required for reading and writing Perl files. The module here is Io: file.
my $f = new IO::File("< field.txt");# the file contains type,name,rw/r/wif(!$f){   print("$!\n");   exit(1);}

The output part uses the output redirection. For more information, see

# Create a copy of The stdout handle, and disable stdoutopen my $ oldout, '> &', \ * stdout or die "cannot DUP stdout: $! "; # Re-open stdoutto the output.txt file handle # Perl closes open stdout, '>', 'output.txt 'or die" cannot reopen stdout: $! "; # The next vertex output will be written into output.txt print" Hello, world! \ N "; # restore the default stdoutopen stdout from the original stdout copy, '> &', $ oldout or die" cannot DUP \ $ oldout: $! ";

2. Use a regular expression to include strings. $ A = ~ /W/indicates that $ A contains "W ". Not much nonsense, all the code above:

#!/usr/bin/perluse strict;use warnings;use IO::File;my $f = new IO::File("< field.txt");# the file contains type,name,rw/r/wif(!$f){   print("$!\n");   exit(1);}#stdout -> fileopen my $oldout, '>&', \*STDOUT or die "Cannot dup STDOUT: $!";open STDOUT, '>', 'output.txt' or die "Cannot reopen STDOUT: $!";my $line;my @array;while($line=<$f>){   chomp($line);   $array[@array]=[split(/,/,$line,3)];}my $i;#for $i ( 0 .. $#array ) #{            #print $array[$i][0]." ".$array[$i][1]." ".$array[$i][2]."\n";#}#    _declspec(property(put=setName,get=getname))               #       type name;for $i (0..$#array){ my $result = "_declspec(property(";my $type = $array[$i][0];my $name = $array[$i][1];my $rw   = $array[$i][2];if ($rw =~ /w/){$result = $result."put=set".$name.",";}if($rw =~ /r/){$result = $result."get=get".$name."))";}$result = $result."\n    ".$type."  ".$name.";\n";print $result;}#  void            setProgress(unsigned long inPercentDone);#unsigned char   getProgress() const;for $i (0..$#array){ my $type = $array[$i][0];my $name = $array[$i][1];my $rw   = $array[$i][2];my $result ="";if ($rw =~ /w/){#w$result = $result."void    set".$name."(".$type." in".$name.");\n";}if($rw =~ /r/){#r$result = $result.$type."    get".$name."() const;\n";}print $result;}#file -> stdoutopen STDOUT, '>&', $oldout or die "Cannot dup \$oldout: $!";my $status = system( "notepad.exe output.txt" );if ($status != 0){print "Failed to open output.txt";exit -1;} else {print "Success to open output.txt,please check it !";}exit 0;
The output file is output.txt, with the upper part bound and the lower part as the get/set function declaration. All code see: https://github.com/lcl-data/GeneratedAutoPropertyInCplusplus
Lcl_data was originally created on the csdn blog. For more information, see.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.