C++混合编程之idlcpp教程Lua篇(7)
上一篇在这 C++混合编程之idlcpp教程Lua篇(6)
第一篇在这 C++混合编程之idlcpp教程(一)
与LuaTutorial4工程相似,工程LuaTutorial5中,同样加入了四个文件:LuaTutorial5.cpp, Tutorial5.cpp, Tutorial5.i, tutorial5.lua。其中LuaTutorial5.cpp的内容基本和LuaTutorial4.cpp雷同,不再赘述。
首先看一下Tutorial5.i的内容:
#import "../../paf/src/pafcore/Reference.i"
###include <vector> namespace tutorial
{
struct Point
{
float x;
float y;
Point();
Point(float a, float b);
nocode Point(const Point& pt);
}; override class Shape : Reference
{
override abstract float getArea();
## virtual ~Shape() {}
}; class ShapeManager(value_object)
{
void addShape(Shape* shape);
float getTotalArea();
static ShapeManager* GetInstance();
#{
~ShapeManager();
private:
std::vector<Shape*> m_shapes;
#}
}; class Triangle : Shape
{
Point m_vertices[#];
nocode Triangle();
##virtual float getArea();
}; }
与Tutorial4.i相比,大部分内容是一样的,不同之处在于类型Shape的声明以及其下的纯虚函数getArea;
override class Shape : Reference
override abstract float getArea();
在这两处声明的最前面都多了一个关键字override。法意味着可以在脚本代码中写一个类型,让它“派生”自Shape,并且能够“重写”虚函数getArea。当然实际上是通过idlcpp生成的一个派生类配合脚本插件代码来完成类似的任务。通过在类型的声明class 前加上关键字override表示此类型可以被脚本“派生”,在虚函数声明的关键字virtual 或 abstract前加上关键字override表示此虚函数可以被脚本“重写”。
在宿主语言和脚本的混合使用中,一个常见的用法是在宿主语言中根据一定的条件向外发出事件,而用脚本语言来编写事件处理代码,例如在WOW中用一个XML文件描述GUI界面,同时注明事件处理函数对应的Lua函数名。idlcpp提供的脚本继承C++类然后重写虚函数的功能可以很好的实现类似的需求。
编译后生成的Tutorial5.h的内容如下:
//DO NOT EDIT THIS FILE, it is generated by idlcpp
//http://www.idlcpp.org #pragma once #include "../../paf/src/pafcore/Typedef.h"
#include "../../paf/src/pafcore/Reference.h"
#include <vector> namespace tutorial
{
struct Point
{
public: float x;
float y;
Point();
Point(float a,float b);
}; class Shape : public pafcore::Reference
{
public:
static ::pafcore::ClassType* GetType();
virtual ::pafcore::ClassType* getType();
virtual size_t getAddress(); virtual float getArea() = ;
virtual ~Shape() {}
}; class ShapeManager
{
public: void addShape(Shape* shape);
float getTotalArea();
static ShapeManager* GetInstance(); ~ShapeManager();
private:
std::vector<Shape*> m_shapes; }; class Triangle : public Shape
{
public:
static ::pafcore::ClassType* GetType();
virtual ::pafcore::ClassType* getType();
virtual size_t getAddress(); Point m_vertices[];
virtual float getArea();
}; }
这里生成的代码和Tutorial4.h基本一致。
最后看一下Tutorial5.lua的内容
Circle = {}
Circle.__index = Circle;
function Circle.New()
circle= {radius = 1.0}
setmetatable(circle, Circle);
circle.shape = paf.tutorial.Shape._Derive_(circle);
return circle;
end
function Circle:getArea()
return self.radius * self.radius * 3.1415926;
end
circle = Circle.New();
circle.radius = 2.0;
shapeManager = paf.tutorial.ShapeManager.GetInstance();
shapeManager:addShape(circle.shape);
print(shapeManager:getTotalArea()._);
triangle = paf.tutorial.Triangle();
triangle.m_vertices[] = paf.tutorial.Point(,);
triangle.m_vertices[] = paf.tutorial.Point(,);
triangle.m_vertices[] = paf.tutorial.Point(,);
shapeManager:addShape(triangle);
print(shapeManager:getTotalArea()._);
在上面的代码中,写了一个类型Circle。在函数Circle.New 通过下面这一行
circle.shape = paf.tutorial.Shape._Derive_(circle);
来模拟继承,语法:C++类型._Derive_(脚本对象) 用于完成模拟继承的行为。实际上circle.shape才是C++类型Shape的派生类实例的引用,在C++中需要用到Shape类型的地方,将circle.shape传递过去即可,如下面的使用方式。
shapeManager:addShape(circle.shape);
然后在类型Circle中提供一个与C++基类同名的函数getArea用来计数圆的面积即可,最终使用时脚本插件会找到对应函数进行调用。
编译执行,结果如下图:

C++混合编程之idlcpp教程Lua篇(7)的更多相关文章
- C++混合编程之idlcpp教程Lua篇(9)
上一篇在这 C++混合编程之idlcpp教程Lua篇(8) 第一篇在这 C++混合编程之idlcpp教程(一) 与前面的工程相比,工程LuaTutorial7中除了四个文件LuaTutorial7.c ...
- C++混合编程之idlcpp教程Lua篇(8)
上一篇在这 C++混合编程之idlcpp教程Lua篇(7) 第一篇在这 C++混合编程之idlcpp教程(一) 与前面的工程相似,工程LuaTutorial6中,同样加入了四个文件:LuaTutori ...
- C++混合编程之idlcpp教程Lua篇(6)
上一篇在这 C++混合编程之idlcpp教程Lua篇(5) 第一篇在这 C++混合编程之idlcpp教程(一) 工程LuaTutorial4中加入了四个文件:LuaTutorial4.cpp, Tut ...
- C++混合编程之idlcpp教程Lua篇(5)
上一篇在这 C++混合编程之idlcpp教程Lua篇(4) 第一篇在这 C++混合编程之idlcpp教程(一) 与前面的工程相似,工程LuaTutorial3中,同样加入了三个文件:LuaTutori ...
- C++混合编程之idlcpp教程Lua篇(4)
上一篇在这 C++混合编程之idlcpp教程Lua篇(3) 与前面的工程相似,工程LuaTutorial2中,同样加入了三个文件 LuaTutorial2.cpp, Tutorial2.i, tut ...
- C++混合编程之idlcpp教程Lua篇(3)
上一篇 C++混合编程之idlcpp教程Lua篇(2) 是一个 hello world 的例子,仅仅涉及了静态函数的调用.这一篇会有新的内容. 与LuaTutorial0相似,工程LuaTutoria ...
- C++混合编程之idlcpp教程Lua篇(2)
在上一篇 C++混合编程之idlcpp教程(一) 中介绍了 idlcpp 工具的使用.现在对 idlcpp 所带的示例教程进行讲解,这里针对的 Lua 语言的例子.首先看第一个示例程序 LuaTuto ...
- C++混合编程之idlcpp教程Python篇(7)
上一篇在这 C++混合编程之idlcpp教程Python篇(6) 第一篇在这 C++混合编程之idlcpp教程(一) 与PythonTutorial4工程相似,工程PythonTutorial5中,同 ...
- C++混合编程之idlcpp教程Python篇(6)
上一篇在这 C++混合编程之idlcpp教程Python篇(5) 第一篇在这 C++混合编程之idlcpp教程(一) 工程PythonTutorial4中加入了四个文件:PythonTutorial4 ...
随机推荐
- Access批量操作
鉴于C#要插5万条记录到Access很慢,在网上找了好久的资料,终于找到了比较有用的信息(转载自Bach)谢谢! 总结如下: 1.导出TXT: select * into [data.txt] in ...
- AutoCad2008 部分快捷键
编组开关: Ctrl+Shift+A 查看扩展属性命令: xdlist 加载新的菜单栏命令:menu
- 微信公众平台创建自定义菜单中文编码导致system error
创建包含了中文的自定义菜单时总是返回{"errcode":-1,"errmsg":"system error"},要将编码方式设置为UTF- ...
- Ceph剖析:定时器safetimer的实现
定时器的作用是在指定的时间执行指定的动作.SafeTimer通过multimap数据结构维护定时项,定时项是时间和事件的Pair,定时项在map中按照定时时间从小到大排列.此外,SafeTimer使用 ...
- aa4
// 初始化地图 var BMapExt = new BMapExtension(domMain, BMap, require('echarts'), require('zrender')); var ...
- SGA(System Global Area)
系统激活时在内存内规划的一个固定的区域,用于存储每位使用者所需存取的数据和必备的系统信息.这个区域成为系统全局区. 数据块缓存区:存放读取数据文件的数据块副本,或者曾经处理过的数据.有效减少读取数据时 ...
- 在rails中 Rendering Partials through Ajax
之前做.net的时候,自己做了一个showcontent的插件,用来加载页面的局部partial 之前采用的是ashx的方式 但rails里面不太方面,今天找到一个比较好的方法,试验成功 起初网上找到 ...
- ActionScript 3.0 自写类整理笔记(十三)——Random类
一个简单的随机函数工具类,总共提供了9种静态方法来获取不同的随机值随便写的,如果你还有什么更好的建议,请提出来,谢谢~ index.Random类:代码:public final class Rand ...
- JavaScript中对象的含义与this的指向
JavaScript中的对象:无序属性的集合 -其属性可以包含基本值.对象或函数.对象就是一组没有顺序的值.我们可以吧JavaScript中的对象想象成键值对,其中值可以是数据和函数.对象的行为和特征 ...
- ElasticSearch学习-centos下安装
1.安装java运行环境(jre) //这里我安装了jdk 其实只需要安装jre就可以了 0)cd /usr;mkdir /usr/java; cd java 1)wget http://downlo ...