上一篇在这 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)的更多相关文章

  1. C++混合编程之idlcpp教程Lua篇(9)

    上一篇在这 C++混合编程之idlcpp教程Lua篇(8) 第一篇在这 C++混合编程之idlcpp教程(一) 与前面的工程相比,工程LuaTutorial7中除了四个文件LuaTutorial7.c ...

  2. C++混合编程之idlcpp教程Lua篇(8)

    上一篇在这 C++混合编程之idlcpp教程Lua篇(7) 第一篇在这 C++混合编程之idlcpp教程(一) 与前面的工程相似,工程LuaTutorial6中,同样加入了四个文件:LuaTutori ...

  3. C++混合编程之idlcpp教程Lua篇(6)

    上一篇在这 C++混合编程之idlcpp教程Lua篇(5) 第一篇在这 C++混合编程之idlcpp教程(一) 工程LuaTutorial4中加入了四个文件:LuaTutorial4.cpp, Tut ...

  4. C++混合编程之idlcpp教程Lua篇(5)

    上一篇在这 C++混合编程之idlcpp教程Lua篇(4) 第一篇在这 C++混合编程之idlcpp教程(一) 与前面的工程相似,工程LuaTutorial3中,同样加入了三个文件:LuaTutori ...

  5. C++混合编程之idlcpp教程Lua篇(4)

    上一篇在这  C++混合编程之idlcpp教程Lua篇(3) 与前面的工程相似,工程LuaTutorial2中,同样加入了三个文件 LuaTutorial2.cpp, Tutorial2.i, tut ...

  6. C++混合编程之idlcpp教程Lua篇(3)

    上一篇 C++混合编程之idlcpp教程Lua篇(2) 是一个 hello world 的例子,仅仅涉及了静态函数的调用.这一篇会有新的内容. 与LuaTutorial0相似,工程LuaTutoria ...

  7. C++混合编程之idlcpp教程Lua篇(2)

    在上一篇 C++混合编程之idlcpp教程(一) 中介绍了 idlcpp 工具的使用.现在对 idlcpp 所带的示例教程进行讲解,这里针对的 Lua 语言的例子.首先看第一个示例程序 LuaTuto ...

  8. C++混合编程之idlcpp教程Python篇(7)

    上一篇在这 C++混合编程之idlcpp教程Python篇(6) 第一篇在这 C++混合编程之idlcpp教程(一) 与PythonTutorial4工程相似,工程PythonTutorial5中,同 ...

  9. C++混合编程之idlcpp教程Python篇(6)

    上一篇在这 C++混合编程之idlcpp教程Python篇(5) 第一篇在这 C++混合编程之idlcpp教程(一) 工程PythonTutorial4中加入了四个文件:PythonTutorial4 ...

随机推荐

  1. .net学习笔记--使用抽象方法实现多态

    在使用抽象方法实现多态之前,我们必须知道一些知识点: 1.抽象类不能被实例化: 2.抽象类可以包含非抽象成员,它们可以由其子类继承调用. 我们可以先创建一个Person的抽象类,代码如下: abstr ...

  2. C# Acrobat打开pdf出错,提示Acrobat.AcroPDDocClass不能强制转换为Acrobat.CAcroPDDoc,使用com组件{9B4CD3E7-4981-101B-9CA8-9240CE2738AE},HRESULT: 0x80004002

    要批量将PDF文件内容按页转换为图片,在写的过程过程遇到两个问题. 一,下载的SDK中,提示要引用COM组件Acrobat,但在我的电脑上就是看不到,只能看到Adobe Acrobat 7.0 Bro ...

  3. EF6配合MySQL或MSSQL(CodeFirst模式)配置指引

    一.新建一个解决方案,包含两个项目:EF6CodeFirstMySQL.Model(动态库项目),EF6CodeFirstMySQL.Tests(控制台应用) 二.通过NuGet将EntityFram ...

  4. python课程第一周重点记录

  5. Matlab(3) -- 编写M文件(函数)

    转自:http://blog.csdn.net/misskissc/article/details/8178089 matlab的命令编辑窗口(Command Window)界面主要是用来调用系统命令 ...

  6. js如何找到方法在哪个js文件

    在Console窗口中输入var f = methodA.prototype.constructor;console.log(f); 网络搜索到的方法.

  7. sqlite3 根据实体自动生成建表语句

      public class BuildSqlTool { public static string GetCreateTableSql(object t) { //CREATE TABLE &quo ...

  8. 从github上获取资源速度慢的解决办法

    今天在github上clone一个仓库的时候,速度非常慢,只有3kb/s,开代理也没用,网上找到的各种git config的方法也没有用,最后想到设置hosts试试.于是在git的安装目录下找到了/e ...

  9. 安卓使用adb命令安装软件

    准备工作: 确信 \Android-sdk-windows\tools\下有 adb.exe     AdbWinApi.dll     AdbWinUsbApi.dll 三个文件,如果没有从\and ...

  10. Android 文章列表

    Android  --列表-- Android(1)-Handler Looper Message MessageQueuehttp://www.cnblogs.com/TS-qrt/articles ...