加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

用Perl来帮助生成C++版“自动属性”

发布时间:2020-12-16 00:28:26 所属栏目:大数据 来源:网络整理
导读:用VS来使用C#的应该知道有个自动属性,类似于 public int MyProperty { get; set; } 他帮你自动生成get/set方法,还帮你把这些方法绑定到该属性上,很方便的说,但是C++的IDE里没有这个功能的,而且我们C++需要写 _declspec(property(put=setJobID,get=getJo

用VS来使用C#的应该知道有个自动属性,类似于

public int MyProperty { get; set; }
他帮你自动生成get/set方法,还帮你把这些方法绑定到该属性上,很方便的说,但是C++的IDE里没有这个功能的,而且我们C++需要写
     _declspec(property(put=setJobID,get=getJobID))
        unsigned long jobID;
    void            setJobID(unsigned long inJobID);
    unsigned long   getJobID() const;
这么多代码来做这些事情,故我用perl写了个简单的脚本,根据变量类型、变量名字和变量读写属性帮我自动生成这些。

前提:
1.电脑上有安装perl,且能正常使用。
2.提供一个field.txt,里面的每行都包含?变量类型,变量名字,变量读写属性
例如string,version,rw
? ? ? ?unsigned long,length,r
? ? ? ?YourStructName,structName,w

简单知识点:
1.文件读写
?perl文件读需要用 到IO::File 或者输入重定向,这里用的模块是IO::File.
my $f = new IO::File("< field.txt");# the file contains type,name,rw/r/w
if(!$f)
{
   print("$!n");
   exit(1);
}
?输出这块用的是输出重定向,可以 参考
# 创建STDOUT句柄的一个副本,之后可以关闭STDOUT
open my $oldout,'>&',*STDOUT or die "Cannot dup STDOUT: $!";
# 重新将STDOUT打开为文件output.txt的句柄
# 在打开文件之前,Perl会将原来保存在STDOUT中的句柄关闭
open STDOUT,'>','output.txt' or die "Cannot reopen STDOUT: $!";
# 接下来的默认输出将会写入到output.txt
print "Hello,World!n";
# 从原有的STDOUT副本中恢复默认的STDOUT
open STDOUT,$oldout or die "Cannot dup $oldout: $!";

2.字符串包含
? 用正则表达式就可以$a=~/w/ 表示$a包含“w”。

废话不多说,上全部代码:
#!/usr/bin/perl

use strict;
use warnings;
use IO::File;
my $f = new IO::File("< field.txt");# the file contains type,rw/r/w
if(!$f)
{
   print("$!n");
   exit(1);
}
#stdout -> file
open 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 -> stdout
open 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;

输出的文件是output.txt,里面上半部分是绑定,下半部分是 get/set函数声明。
全部代码见于: https://github.com/lcl-data/GeneratedAutoPropertyInCplusplus LCL_data原创于CSDN blog,转载请注明。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读