C++混合編程之idlcpp教程Lua篇(8)

上一篇在這 C++混合編程之idlcpp教程Lua篇(7)html

第一篇在這 C++混合編程之idlcpp教程(一)編程

與前面的工程類似,工程LuaTutorial6中,一樣加入了四個文件:LuaTutorial6.cpp, Tutorial6.cpp, Tutorial6.i, tutorial6.lua。其中LuaTutorial6.cpp的內容基本和LuaTutorial5.cpp雷同,再也不贅述。c#

首先看一下Tutorial6.i的內容:函數

 

#import "../../paf/src/pafcore/typedef.i"

namespace tutorial
{
    struct Vector3<N>
    {
        Vector3();
        Vector3(const Vector3& v);
        Vector3(N a, N b, N c);
        Vector3(const N* p);
        N getLength();
        N length get;
        N lengthSquare get;

        static Vector3<N> s_zero;
        
        nocode N x;
        nocode N y;
        nocode N z;
        nocode N v[#3];

        #{
        union
        {
            struct
            {
                N x,y,z;
            };
            N v[3];
        };
        #}
    };

    export Vector3<float>;
    export Vector3<double>;
    typedef Vector3<float> Vector3f;
    typedef Vector3<double> Vector3d;

    #{
    template<typename N>
    Vector3<N> Vector3<N>::s_zero(0, 0, 0);

    template<typename N>
    inline Vector3<N>::Vector3()
    {
    }

    template<typename N>
    inline Vector3<N>::Vector3(const Vector3<N>& v) : x(v.x), y(v.y), z(v.z)
    {}

    template<typename N>
    inline Vector3<N>::Vector3(N a, N b, N c) : x(a), y(b), z(c)
    {}

    template<typename N>
    inline Vector3<N>::Vector3(const N* p) : x(p[0]), y(p[1]), z(p[2])
    {}

    template<typename N>
    inline N Vector3<N>::getLength()
    {
        return N(sqrt(x * x + y * y + z * z));
    }

    template<typename N>
    inline N Vector3<N>::get_length()
    {
        return N(sqrt(x * x + y * y + z * z));
    }

    template<typename N>
    inline N Vector3<N>::get_lengthSquare()
    {
        return (x * x + y * y + z * z);
    }
    #}
}

 

 

struct Vector3<N>lua

這是一個模板類,C++的模板功能複雜強大,編譯器實在難寫。因此大多數C++模板的高級特性在idlcpp中都沒有作支持,畢竟idlcpp只負責對腳本語言提供接口,有一些簡單的模板功能就夠用了。另外因爲不許備支持數值作爲模板參數,只支持類型作爲模板參數,因此語法採用了c#的形式。與C++的模板語法不太同樣。 spa

static Vector3 s_zero;3d

這一行聲明瞭一個靜態成員變量。idlcpp支持靜態成員變量,靜態成員函數,靜態屬性(實際上也是靜態成員函數)。code

nocode N x;htm

nocode N y;blog

nocode N z;

nocode N v[#3];

#{

union

{

  struct

  {

    N x,y,z;

  };

  N v[3];

};

#}

idlcpp 沒有提供 union。好在能夠經過nocode 和 #{#} 分別在生成的元數據描述代碼和C++頭文件提供各自的內容。

下面兩行代碼

export Vector3<float>;

export Vector3<double>;

用於生成元數據代碼。idlcpp中經過這樣的聲明語句纔會生成相應類型的元數據信息。

再下面兩行代碼

typedef Vector3<float> Vector3f;

typedef Vector3<double> Vector3d;

爲模板類實例類型聲明瞭類型別名。由於這兩個類型名分別是::tutorial::Vector3<float> 和 ::tutorial::Vector3<double>,在腳本中使用不方便,有了類型別名以後就能夠經過::tutorial::Vector3f和::tutorial::Vector3d來使用。

後面就是成員函數的實現代碼,不在贅述。

編譯後生成的Tutorial6.h的內容以下:

 

//DO NOT EDIT THIS FILE, it is generated by idlcpp
//http://www.idlcpp.org

#pragma once


namespace tutorial
{
    template<typename N>
    struct Vector3
    {
    public:

        Vector3();
        Vector3(const Vector3& v);
        Vector3(N a,N b,N c);
        Vector3(const N* p);
        N getLength();
        N get_length();
        N get_lengthSquare();

        static Vector3<N> s_zero;


        union
        {
            struct
            {
                N x,y,z;
            };
            N v[3];
        };
        
    };



    typedef Vector3<float> Vector3f;
    typedef Vector3<double> Vector3d;


    template<typename N>
    Vector3<N> Vector3<N>::s_zero(0, 0, 0);

    template<typename N>
    inline Vector3<N>::Vector3()
    {
    }

    template<typename N>
    inline Vector3<N>::Vector3(const Vector3<N>& v) : x(v.x), y(v.y), z(v.z)
    {}

    template<typename N>
    inline Vector3<N>::Vector3(N a, N b, N c) : x(a), y(b), z(c)
    {}

    template<typename N>
    inline Vector3<N>::Vector3(const N* p) : x(p[0]), y(p[1]), z(p[2])
    {}

    template<typename N>
    inline N Vector3<N>::getLength()
    {
        return N(sqrt(x * x + y * y + z * z));
    }

    template<typename N>
    inline N Vector3<N>::get_length()
    {
        return N(sqrt(x * x + y * y + z * z));
    }

    template<typename N>
    inline N Vector3<N>::get_lengthSquare()
    {
        return (x * x + y * y + z * z);
    }
    
}

 

idlcpp會爲只讀屬性length和lengthSquare 生成對應的函數聲明get_length和get_lengthSquare。

其餘的內容,C++和idl基本上都是同樣的。

而後是Tutorial6.cpp

 

#include "Tutorial6.h" #include "Tutorial6.mh" #include "Tutorial6.ic" #include "Tutorial6.mc"


由於模板類的代碼都寫在頭文件中了,因此Tutorial6.cpp只須要包含對應的四個文件便可。 

最後看一下Tutorial6.lua的內容

v1 = paf.tutorial.Vector3f(1,1,2); v1.z = 1; print(v1.length._); v2 = paf.tutorial.Vector3d(2,2,1); v2.v[2] = 2; print(v2:getLength()._);


 

編譯執行,結果以下圖:

相關文章
相關標籤/搜索