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

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

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

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

    上一篇在这 C++混合编程之idlcpp教程Lua篇(6) 第一篇在这 C++混合编程之idlcpp教程(一) 与LuaTutorial4工程相似,工程LuaTutorial5中,同样加入了四个文件: ...

  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. python autopep8

    安装 使用pip install autopep8或easy_install 都可以. 使用 autopep8 -i -a 要检查的py文件路径 更多参数使用可以参考:https://github.c ...

  2. 发送短信MFMessageComposeViewController

    if([MFMessageComposeViewController canSendText]) { MFMessageComposeViewController * controller = [[M ...

  3. 应用程序缓存--manifest

    应用程序缓存(Application Cache)为应用带来三个优势: 离线浏览 - 用户可在应用离线时使用它们 速度 - 已缓存资源加载得更快 减少服务器负载 - 浏览器将只从服务器下载更新过或更改 ...

  4. c++父类和子类转化致命的代码错误

    最近在工作中,出现了严重的代码错误,对象的基类和子类的继承,代码大致如下: class A { }; class B : public A { } void main() { A* a;(用于子类对象 ...

  5. 金蝶 K/3 Cloud 服务端控件编程模型

    如下图是服务端已有的控件编程模型

  6. 论人品 | | noip1015模拟考

    第一题:火车进站... 由于有了老师给的助攻,第一题的时间为半小时,主要在读题了.... jzoj1146 第二题:car 难在正方形的计算? 第二题时间:1.5hour 第三题:sort排序?

  7. 关于delphi点击webbrowser中任意一点的问题

    关于delphi点击webbrowser中任意一点的问题 有时候我们需要delphi载入webbrowser1打开网页的时候 需要点击某一个点的位置 可能是坐标 可能是按钮 可能是其他的控件应该如何来 ...

  8. WinForm 使用 HttpUtility

    在 Visual C# 中使用 HttpUtility 是无效的,即使添加了命名空间 System.Web,是因为需要在引用中添加 System.Web.dll. 可是没有 System.Web.dl ...

  9. spring mvc(1):基础入门

      依赖 pom.xml ( maven ) <properties>  <spring.version>3.0.5.RELEASE</spring.version> ...

  10. MG--滚动的视觉差效果

    #几句代码完成tableView滚动的视觉差 - 效果图 (失帧严重)![](http://upload-images.jianshu.io/upload_images/1429890-f2c8577 ...