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 ...
随机推荐
- h5页面唤起app(iOS和Android),没有安装则跳转下载页面
浏览器和app没有通信协议,所以h5不知道用户的手机释放安装了app.因此只能是h5去尝试唤起app,若不能唤起,引导用户去下载我们的app. 微信里屏蔽了 schema 协议,如果在微信中打开h5, ...
- 在ubuntu 16.04系统环境中搭建NAS(samba/iscsi/nfs)
在ubuntu 16.04系统中搭建NAS环境 一.基本配置1:设置静态IPvi /etc/network/interfaces#iface ens32 inet dhcpiface ens32 in ...
- C#中的问号
本人转载自: ①http://msdn.microsoft.com/zh-cn/practices/dd391752.aspx ②http://jhxk.iteye.com/blog/439888 在 ...
- Eclipse 在ubuntu桌面显示快捷启动以及解决Eclipse 在ubuntu中点击菜单栏不起作用的原因.
要在Eclipse中设置好之后,可以通过如下方式在周末显示快捷启动以及解决Eclipse在ubuntu高版本中点击菜单栏项不显示列表的问题 在usr/share/app-install/desktop ...
- Getting Started With Hazelcast 读书笔记(第一章)
第一章:数据集群的演化与 早期的服务器架构 显然,应用是可扩展的,但是由于是集中式服务器,随着数据库性能达到极限,再想扩展就变得极端困难,于是出现了缓存. 缓存显然再次提升了可扩展性,减轻了数据 ...
- 云计算P2V的迁移过程
总结一下客户在传统物理机向虚拟化环境迁移中的典型场景和常用工具及步骤.
- fuse进级
任何操作首先都会传递给getattr函数.
- CSV的导入导出
using System; using System.Data; using System.IO; namespace COMMON { public class CSVhelperClass { / ...
- MVC文件上传和下载
1.单个文件上传 HTML写法:form表单中加enctype="multipart/form-data" <form aciont="" method= ...
- SVN的服务器端用户权限配置
第一:用户的配置 SVN和apache整合的话,用户可以直接使用htpasswd dav_svn.passwd_file_address USERNAME来配置. 而账户的管理可以用dav_svn.a ...