C++混合编程之idlcpp教程Lua篇(9)
上一篇在这 C++混合编程之idlcpp教程Lua篇(8)
第一篇在这 C++混合编程之idlcpp教程(一)
与前面的工程相比,工程LuaTutorial7中除了四个文件LuaTutorial7.cpp, Tutorial7.cpp, Tutorial7.i, tutorial7.lua 外,Tutorial6.cpp也被加入了此工程中。其中LuaTutorial7.cpp的内容基本和LuaTutorial6.cpp雷同,不再赘述。
首先看一下Tutorial7.i的内容:
#import "Tutorial6.i" namespace tutorial
{
template<T>
struct Ray3
{
Ray3();
Ray3(const Vector3<T>& origin, const Vector3<T>& direction);
void getPoint(Vector3<T>& point, T t) const;
Vector3<T> getPoint(T t) const;
Vector3<T> m_origin;
Vector3<T> m_direction;
}; export Ray3<float>;
export Ray3<double>;
typedef Ray3<float> Ray3f;
typedef Ray3<double> Ray3d; #{ template<typename T>
inline Ray3<T>::Ray3()
{} template<typename T>
inline Ray3<T>::Ray3(const Vector3<T>& origin, const Vector3<T>& direction) :
m_origin(origin), m_direction(direction)
{} template<typename T>
inline void Ray3<T>::getPoint(Vector3<T>& point, T t) const
{
point.x = m_origin.x + m_direction.x * t;
point.y = m_origin.y + m_direction.y * t;
point.z = m_origin.z + m_direction.z * t;
}
template<typename T>
inline Vector3<T> Ray3<T>::getPoint(T t) const
{
return Vector3<T>(m_origin.x + m_direction.x * t,
m_origin.y + m_direction.y * t,
m_origin.z + m_direction.z * t);
} #}
}
第一行
#import "Tutorial6.i"
在后面Ray3的定义中使用到了模板类Vector3,所以在此处要先引入此文件。
template<T>
struct Ray3
此处定义了模板类Ray3。其中有类型为Vector3<T>的两个成员变量m_origin和m_direction。在这个类中以 m_origin + m_direction * t (t >= 0) 参数方程的形式表示一个射线。有两个名为getPoint的重载函数用来获取射线上的一点坐标。
export Ray3<float>;
export Ray3<double>;
模板实例化,这两行代码指示idlcpp生成相应类型的元数据信息。
typedef Ray3<float> Ray3f;
typedef Ray3<double> Ray3d;
定义类型别名,方便使用。
编译后生成的Tutorial7.h的内容如下:
//DO NOT EDIT THIS FILE, it is generated by idlcpp
//http://www.idlcpp.org #pragma once #include "./Tutorial6.h" namespace tutorial
{
template<typename T>
struct Ray3
{
public: Ray3();
Ray3(const Vector3<T>& origin,const Vector3<T>& direction);
void getPoint(Vector3<T>& point,T t)const ;
Vector3<T> getPoint(T t)const ;
Vector3<T> m_origin;
Vector3<T> m_direction;
}; typedef Ray3<float> Ray3f;
typedef Ray3<double> Ray3d; template<typename T>
inline Ray3<T>::Ray3()
{} template<typename T>
inline Ray3<T>::Ray3(const Vector3<T>& origin, const Vector3<T>& direction) :
m_origin(origin), m_direction(direction)
{} template<typename T>
inline void Ray3<T>::getPoint(Vector3<T>& point, T t) const
{
point.x = m_origin.x + m_direction.x * t;
point.y = m_origin.y + m_direction.y * t;
point.z = m_origin.z + m_direction.z * t;
}
template<typename T>
inline Vector3<T> Ray3<T>::getPoint(T t) const
{
return Vector3<T>(m_origin.x + m_direction.x * t,
m_origin.y + m_direction.y * t,
m_origin.z + m_direction.z * t);
} }
然后是Tutorial7.cpp
#include "Tutorial7.h"
#include "Tutorial7.mh"
#include "Tutorial7.ic"
#include "Tutorial7.mc"
因为模板类的代码都写在头文件中了,所以Tutorial7.cpp只需要包含对应的四个文件即可。
另外模板类Ray3用到了模板类Vector3,所以其实例化类型Ray3<float>和Ray3<double>也分别用到Vector3的实例化类型Vector3<float>和Vector3<double>,相应的Ray3<float>元数据中也会用到Vector3<float>的元数据信息。所以在这个工程中需要将Tutorial6.cpp加入进来。
最后看一下Tutorial7.lua的内容
p = paf.float.NewArray();
p[] = ;
p[] = ;
p[] = ;
ray = paf.tutorial.Ray3f(paf.tutorial.Vector3f.s_zero, paf.tutorial.Vector3f(p));
pt = paf.tutorial.Vector3f(,,);
ray:getPoint(pt, );
print(pt.x._);
print(pt.y._);
print(pt.z._);
pt = ray:getPoint();
print(pt.x._);
print(pt.y._);
print(pt.z._);
第一行:
p = paf.float.NewArray(3);
创建一个float类型的数组,共三个元素,其中float是内置的类型。C++的原生类型在idlcpp中都是支持的,如下:
bool
char
signed char
unsigned char
wchar_t
short
unsigned short
long
unsigned long
long long
unsigned long long
int
unsigned int
float
double
long double
考虑到有些类型中间有空格,为脚本使用方便,还为这些类型定义了别名,具体参见pafcore中的Typedef.i
编译执行,结果如下图:

C++混合编程之idlcpp教程Lua篇(9)的更多相关文章
- 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篇(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 ...
随机推荐
- Javascript.ReactNative-2-javascript-syntax-in-react-native
JavaScript Syntax in React Native Contents: Arrow Function Let+Const Default + Rest + Spread Destruc ...
- iOS.DistributionApp.1-mobile-provision-file[draft]
.mobileprovision file 0. .mobileprovision file的作用 .mobileprovision file作用以及扮演的角色 1. 如何删除旧的.mobilepro ...
- 解决mac升级后,出现的 xcrun: error: invalid active developer path, missing xcrun 错误
最近升级了mac系统,然后接着写代码就出问题了. 报错信息如下: xcrun: error: invalid active developer path (/Library/Developer/Com ...
- 弄个知乎的粒子动态背景_实践particles.js
好久没登录知乎,发现他们的登录页面粒子动态效果蛮炫的,查一下代码用了Particles.js基于Canvas画布创建粒子颗粒效果. 上图 上图: 感觉有比格,就照着弄了一个,玩玩. githu ...
- 异步I/O操作
今天在看boost库的时候注意到异步I/O操作时,缓冲区有效性问题. 如何实现异步操作:以异步读操作为例async_read(buffer, handler): void handler() {} v ...
- ReportViewer内存泄漏问题解决方案[上]
做这个项目有点倒霉,快要验收的时候,发现微软ReportViewer控件的一个bug,导致我们的项目无法正常验收. 问题描叙:用ReportViewer本地模式做的报表,在ASP.NET页面中呈现.在 ...
- FreeMarker标签与使用
模板技术在现代的软件开发中有着重要的地位,而目前最流行的两种模板技术恐怕要算freemarker和velocity了,webwork2.2对两者都有不错的支持,也就是说在webwork2中你可以随意选 ...
- visul studio 文件分包
1.搜索算法. 2.软件控制逻辑. 3.自定义控件. 4.GUI模块. 5.线程化操作
- 总结JS 常用函数
希望本文总结的内容能给各位看官带来焕然一新的感觉.另外,如果你们有什么值得推荐的js技巧,欢迎在评论中补充,我可以收纳在本文中. PS:此文档会持续新增内容. Ajax请求 jquery ajax函数 ...
- [16]APUE:套接字
[a] socket / socketpair #include <sys/socket.h> int socket(int domain, int type, int protocol) ...