上一篇在這 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
{
struct Ray3<T>
{
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,所以在此處要先引入此文件。
struct Ray3<T>
此處定義了模板類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(3); p[0] = 1; p[1] = 2; p[2] = 3; ray = paf.tutorial.Ray3f(paf.tutorial.Vector3f.s_zero, paf.tutorial.Vector3f(p)); pt = paf.tutorial.Vector3f(0,0,0); ray:getPoint(pt, 2); print(pt.x._); print(pt.y._); print(pt.z._); pt = ray:getPoint(3); 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
編譯執行,結果如下圖: