用Perl来帮助生成C++版“自动属性”
2012-02-16 09:13
393 查看
用VS来使用C#的应该知道有个自动属性,类似于
前提:
1.电脑上有安装perl,且能正常使用。
2.提供一个field.txt,里面的每行都包含 变量类型,变量名字,变量读写属性
例如string,version,rw
unsigned long,length,r
YourStructName,structName,w
简单知识点:
1.文件读写
perl文件读需要用到IO::File 或者输入重定向,这里用的模块是IO::File.
2.字符串包含
用正则表达式就可以$a=~/w/ 表示$a包含“w”。
废话不多说,上全部代码:
输出的文件是output.txt,里面上半部分是绑定,下半部分是 get/set函数声明。
全部代码见于:https://github.com/lcl-data/GeneratedAutoPropertyInCplusplus
LCL_data原创于CSDN blog,转载请注明。
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,name,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,转载请注明。
相关文章推荐
- 将C++代码全部写到头文件:)python脚本帮助自动生成相应的实现文件初始框架
- 帮助你自动生成浏览器特有CSS3属性的Javascript类库 - CSSFx
- ndk 自动生成c++代码
- 如何自动生成Jni的Java层调C++层的头文件
- OC 自动生成分类属性方法
- Vs.NET2005自动生成属性
- 帮助自动生成页面任何元素的Label的CSS类库 - Label.css
- 偷懒秘笈之自动生成属性
- Runtime 解析字典自动生成属性代码 及 字典转模型方式一 KVC/Runtime
- Create一个DataTable时需要创建这样一列,它的结果是根据一定的表达式自动生成的,DataColumn的Expression得属性
- 支持自动TOC超文本目录生成的chm转换器(perl脚本)
- vs2005怎样自动生成属性
- WEBAPI 自动生成帮助文档
- 学习,自动生成属性
- 巧用项目生成事件属性自动打包
- linux c/c++ 代码使用 doxygen 自动生成文档
- 用doxygen为C/C++程序自动生成文档(二)-doxygen风格注释简介
- Entity Framework 不修改EF自动生成类,添加注解校验和额外属性或字段
- webAPI 自动生成帮助文档
- Perl Spreadsheet::WriteExcel 模块自动生成excel 文件