C++混合编程之idlcpp教程Lua篇(4)
上一篇在这 C++混合编程之idlcpp教程Lua篇(3)
与前面的工程相似,工程LuaTutorial2中,同样加入了三个文件 LuaTutorial2.cpp, Tutorial2.i, tutorial2.lua。其中LuaTutorial2.cpp的内容基本和LuaTutorial1.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
与LuaTutorial1中的struct Point相比,除了原来的默认构造函数外,多了一个带两个参数的构造函数
Point(float a, float b);
两个构造函数都有nocode修饰中,所以idlcpp不会在Tutorial2.h中生成对应的函数声明,所在直接在后面的#{#}写上构造函数的实现代码,这些代码会插入到Tutorial2.h中的对应位置。当然也可以不使用nocode,这样的话这两个构造函数的声明部分就会出现在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; 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();
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.lua的内容:
rect1 = paf.tutorial.Rectangle();
rect1.m_min.x = ;
rect1.m_min.y = ; print(rect1.left._);
print(rect1.bottom._); rect1.right = ;
rect1.top = ; print(rect1.m_max.x._);
print(rect1.m_max.y._); print(rect1.area._); rect2 = paf.tutorial.Rectangle(rect1.m_min, paf.tutorial.Point(,));
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);
或者这一句可以利用lua的语法特点简写为 rect3 = rect2:New();
编译运行结果如下图:

C++混合编程之idlcpp教程Lua篇(4)的更多相关文章
- 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篇(7)
上一篇在这 C++混合编程之idlcpp教程Lua篇(6) 第一篇在这 C++混合编程之idlcpp教程(一) 与LuaTutorial4工程相似,工程LuaTutorial5中,同样加入了四个文件: ...
- 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篇(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 ...
随机推荐
- 为什么C++中空类和空结构体大小为1?(转载)
原文链接:http://www.spongeliu.com/260.html 对于结构体和空类大小是1这个问题,首先这是一个C++问题,在C语言下空结构体大小为0(当然这是编译器相关的).这里的空类和 ...
- WampServer服务中MySQL无法正常启动解决方案
打开wampserver->mysql->my.ini,添加或修改innodb_force_recovery = 1 然后重启所有服务就大功告成了!
- Failed to load the JNI shared library jvm.dll
jdk和使用的ide版本不符合,换一个版本的jdk或者换版本的ide
- 几篇关于VisualStudio的调试工具文章
现代的软件变得日益复杂,强大的调试功能也变得日益重要起来.在VisualStudio的最近几个版本中,在调试工具方面也是增强了不少的,本文转录了几个微软官方介绍的一些新增的调试功能的文章,如果能很好的 ...
- 开发常用小demo 整理
pc懒加载 https://github.com/ningmengxs/Lazy_loading 元素滑动 js 效果 https://github.com/ningmengxs/elem ...
- ASP.NET Web API系列教程目录
ASP.NET Web API系列教程目录 Introduction:What's This New Web API?引子:新的Web API是什么? Chapter 1: Getting Start ...
- Binder的设计和框架
转自:http://wangkuiwu.github.io/2014/09/01/Binder-Introduce/ 1. Binder架构解析 1.1 Binder模型 上图中涉及到Binder模型 ...
- android框架整理
http://blog.csdn.net/ma969070578/article/details/27808043 闲来无事.想搭个框架试试 分析一般应用 将资料整理整理 粗略统计 需要以下资料 1. ...
- Find the equipment indices
Here is a simple program test task, it doesn't have very diffcult logic: A zero-indexed array A cons ...
- 使用JNDI或JDBC连接数据库
一. JNDI 1. tomcat环境 找到X:\xxx\......\apache-tomcat-6.0.39\conf\server.xml,在<Host>节点中配置如下: <H ...