C++混合编程之idlcpp教程Python篇(4)
上一篇在这 C++混合编程之idlcpp教程Python篇(3)
第一篇在这 C++混合编程之idlcpp教程(一)
与前面的工程相似,工程PythonTutorial2中,同样加入了三个文件 PythonTutorial2.cpp, Tutorial2.i, tutorial2.py。其中PythonTutorial2.cpp的内容基本和PythonTutorial1.cpp雷同,不再赘述。首先看一下Tutorial2.i的内容:
namespace tutorial
{
struct Point
{
float x;
float y;
nocode Point();
nocode Point(float a, float b);
#{
Point()
{}
Point(float a, float b)
{
x = a;
y = b;
}
#}
}; struct Rectangle
{
Point m_min;
Point m_max; float left set get;
float right set get; nocode float bottom set get;
nocode float top set get;
nocode float area get;
nocode float getArea(); Rectangle(const Point& min, const Point& max);
Rectangle();
nocode Rectangle(const Rectangle& pt);
#{
void set_bottom(float bottom)
{
m_min.y = bottom;
}
float get_bottom()
{
return m_min.y;
}
void set_top(float top)
{
m_max.y = top;
}
float get_top()
{
return m_max.y;
}
float get_area()
{
return (m_max.x - m_min.x)*(m_max.y - m_min.y);
}
float getArea()
{
return (m_max.x - m_min.x)*(m_max.y - m_min.y);
}
#}
};
#{
inline Rectangle::Rectangle(const Point& min, const Point& max) : m_min(min), m_max(max)
{
}
inline Rectangle::Rectangle()
{}
inline float Rectangle::get_left()
{
return m_min.x;
}
inline void Rectangle::set_left(float left)
{
m_min.x = left;
}
inline float Rectangle::get_right()
{
return m_max.x;
}
inline void Rectangle::set_right(float right)
{
m_max.x = right;
}
#}
}
在这里仍然有struct Point。与PythonTutorial1中的struct Point相比,除了原来的默认构造函数外,多了一个带两个参数的构造函数
Point(float a, float b);
两个构造函数都在meta: 段中,所以idlcpp不会在Tutorial2.h中生成对应的函数声明,所在直接在后面的#{#}写上构造函数的实现代码,这些代码会插入到Tutorial2.h中的对应位置。当然也可以不使用meta:,这样的话这两个构造函数的声明部分就会出现在Tutorial2.h的struct Point中,那么实现代码就要写在外面了。在struct Point后添加了一个新的类型struct Rectangle。前两行
Point m_min;
Point m_max;
声明了两个数据成员。然后是
float left set get;
float right set get;
这里又出现了新的语法:属性。属性语法来自于C#。形式为: 类型 + 名称 + 可选的set和get。在C++中实际上是生成了两个对应的成员函数,函数名分别为set_ + 属性名称,get_ + 属性名称,比如为属性left生成的两个成员函数为:void set_left(float)和float get_left()。然后还有三个属性的声明
float bottom set get;
float top set get;
float area get;
其中属性area是只读属性,即只生成float get_area()成员函数。然后是
float getArea();
这是一个成员函数,在C++生成中的函数形式和这里是一样的。然后是
Rectangle(const Point& min, const Point& max);
Rectangle();
nocode Rectangle(const Rectangle& pt);
此处一共三个构造函数,其中最后一个是拷贝构造函数,对于此类来说,拷贝构造函数可以不写。所以加了nocode前缀。于是在头文件只会有两个构造函数声明,但是在元数据中静态函数New共有三个重载函数。分别对于上面三个构造函数。
后面就是具体函数的实现代码。都放在#{#}中以便复制到头文件中。
编译后生成的Tutorial2.h的内容如下:
//DO NOT EDIT THIS FILE, it is generated by idlcpp
//http://www.idlcpp.org #pragma once #include "./Tutorial2.h"
namespace tutorial{ struct Rectangle; } namespace tutorial
{
struct Point
{
public: float x;
float y;
public:
static Point* New();
static Point* New(float a,float b);
static Point* NewArray(unsigned int count); Point()
{}
Point(float a, float b)
{
x = a;
y = b;
} }; struct Rectangle
{
public: Point m_min;
Point m_max; void set_left( float);
float get_left();
void set_right( float);
float get_right(); Rectangle(const Point& min,const Point& max);
Rectangle();
public:
static Rectangle* New(const Point& min,const Point& max);
static Rectangle* New();
static Rectangle* NewArray(unsigned int count);
static Rectangle* Clone(const Rectangle& pt); void set_bottom(float bottom)
{
m_min.y = bottom;
}
float get_bottom()
{
return m_min.y;
}
void set_top(float top)
{
m_max.y = top;
}
float get_top()
{
return m_max.y;
}
float get_area()
{
return (m_max.x - m_min.x)*(m_max.y - m_min.y);
}
float getArea()
{
return (m_max.x - m_min.x)*(m_max.y - m_min.y);
} }; inline Rectangle::Rectangle(const Point& min, const Point& max) : m_min(min), m_max(max)
{
}
inline Rectangle::Rectangle()
{}
inline float Rectangle::get_left()
{
return m_min.x;
}
inline void Rectangle::set_left(float left)
{
m_min.x = left;
}
inline float Rectangle::get_right()
{
return m_max.x;
}
inline void Rectangle::set_right(float right)
{
m_max.x = right;
} }
内容基本上都是和Tutorial2.i中一一对应的
然后看一下脚本tutorial2.py的内容:
import pafpython;
paf = pafpython.paf; rect1 = paf.tutorial.Rectangle();
rect1.m_min.x = 1;
rect1.m_min.y = 2; print(rect1.left._);
print(rect1.bottom._); rect1.right = 3;
rect1.top = 4; print(rect1.m_max.x._);
print(rect1.m_max.y._); print(rect1.area._); rect2 = paf.tutorial.Rectangle(rect1.m_min, paf.tutorial.Point(5,5));
print(rect2.getArea()._); rect3 = paf.tutorial.Rectangle.New(rect2);
print(rect3.getArea()._);
rect1 = paf.tutorial.Rectangle();
这是rect1 = paf.tutorial.Rectangle.New(); 的简化写法。
后面分别用数据成员和属性来操作rect1。
rect2 = paf.tutorial.Rectangle(rect1.m_min, paf.tutorial.Point(5,5));
调用了Rectangle带参数的构造函数(实际上是静态函数New)。
rect3 = paf.tutorial.Rectangle.New(rect2);
相当于C++中的 Rectangle* rect3 = new Rectangle(*rect2);
编译运行结果如下图:
C++混合编程之idlcpp教程Python篇(4)的更多相关文章
- C++混合编程之idlcpp教程Python篇(9)
上一篇在这 C++混合编程之idlcpp教程Python篇(8) 第一篇在这 C++混合编程之idlcpp教程(一) 与前面的工程相比,工程PythonTutorial7中除了四个文件PythonTu ...
- C++混合编程之idlcpp教程Python篇(8)
上一篇在这 C++混合编程之idlcpp教程Python篇(7) 第一篇在这 C++混合编程之idlcpp教程(一) 与前面的工程相似,工程PythonTutorial6中,同样加入了四个文件:Pyt ...
- C++混合编程之idlcpp教程Python篇(7)
上一篇在这 C++混合编程之idlcpp教程Python篇(6) 第一篇在这 C++混合编程之idlcpp教程(一) 与PythonTutorial4工程相似,工程PythonTutorial5中,同 ...
- C++混合编程之idlcpp教程Python篇(6)
上一篇在这 C++混合编程之idlcpp教程Python篇(5) 第一篇在这 C++混合编程之idlcpp教程(一) 工程PythonTutorial4中加入了四个文件:PythonTutorial4 ...
- C++混合编程之idlcpp教程Python篇(5)
上一篇在这 C++混合编程之idlcpp教程Python篇(4) 第一篇在这 C++混合编程之idlcpp教程(一) 与前面的工程相似,工程PythonTutorial3中,同样加入了三个文件:Py ...
- C++混合编程之idlcpp教程Python篇(3)
上一篇 C++混合编程之idlcpp教程Python篇(2) 是一个 hello world 的例子,仅仅涉及了静态函数的调用.这一篇会有新的内容. 与PythonTutorial0相似,工程Pyth ...
- C++混合编程之idlcpp教程Python篇(2)
在上一篇 C++混合编程之idlcpp教程(一) 中介绍了 idlcpp 工具的使用.现在对 idlcpp 所带的示例教程进行讲解,这里针对的 Python 语言的例子.首先看第一个示例程序 Pyth ...
- C++混合编程之idlcpp教程Lua篇(6)
上一篇在这 C++混合编程之idlcpp教程Lua篇(5) 第一篇在这 C++混合编程之idlcpp教程(一) 工程LuaTutorial4中加入了四个文件:LuaTutorial4.cpp, Tut ...
- C++混合编程之idlcpp教程Lua篇(9)
上一篇在这 C++混合编程之idlcpp教程Lua篇(8) 第一篇在这 C++混合编程之idlcpp教程(一) 与前面的工程相比,工程LuaTutorial7中除了四个文件LuaTutorial7.c ...
随机推荐
- 教你用Ossim平台检测网络的Shellcode攻击
教你用Ossim平台检测网络的Shellcode攻击行为 教程: http://www.tudou.com/programs/view/-hxTm0q1tDY/ 以下是视频截图: 更多视频内容: 本文 ...
- C++学习基础五之函数参数——形参
一.理论部分 C++中函数形参主要分为两类,如图1所示, 图1 总结: 一.当函数参数为非引用形参时,传进函数体内的是实参的拷贝,(注意,对于基本类型而言,拷贝的是实参的值,对于指针而言拷贝的是实参的 ...
- 在php中写接口时 对json格式的转换 简单的方法
方法 一 方法二 可以通过urlencode();遍历出来
- mvc多个按钮的提交方法
转载地址:http://www.cnblogs.com/wuchang/archive/2010/01/29/1658916.html 有时候会遇到这种情况:在一个表单上需要多个按钮来完成不同的功能, ...
- 调用Ria Service中方法的各种方式
前端界面后台: using System; using System.Collections.Generic; using System.Linq; using System.Net; using S ...
- 锋利的js之妈妈再也不用担心我找错钱了
用js实现收银功能. <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <hea ...
- bash脚本编程之一 变量、变量类型等
变量的内容 1.变量命名: 1.只能包含字母.数字和下划线,并且不能以数字开头, 2.不应该跟系统中已有的环境变量重名 3.最好能见名知意 2.变量赋值: 设置变量: ...
- python模块使用案例
python模块使用案例 一.使用MySQLdb模块代码示例: # 导入 MySQLdb模块 import MySQLdb # 和服务器建立链接,host是服务器ip,我的MySQL数据库搭建在本机, ...
- linux获取系统启动时间
1.前言 时间对操作系统来说非常重要,从内核级到应用层,时间的表达方式及精度各部相同.linux内核里面用一个名为jiffes的常量来计算时间戳.应用层有time.getdaytime等函数.今天需要 ...
- [wechall] Time to Reset (Exploit, Coding, PHP)
Time to Reset (Exploit, Coding, PHP) <?php $start=1453000000; $cstf="nipNlJ6ZQPuGQ3i90QUTP8J ...