Lua (8), idlcpplua

Source: Internet
Author: User

Lua (8), idlcpplua

Previous Article Lua in this idlcpp tutorial on C ++ hybrid programming (7)

Article 1 idlcpp tutorial in C ++ hybrid programming (I)

Similar to the previous project, LuaTutorial6 also includes four files: LuaTutorial6.cpp, Tutorial6.cpp, Tutorial6. I, and tutorial6.lua. The content of LuaTutorial6.cpp is basically the same as that of LuaTutorial5.cpp.

First, let's take a look at Tutorial6. I:

 

namespace tutorial{    template<typename N>    struct Vector3    {        Vector3();        Vector3(const Vector3 ref v);        Vector3(N a, N b, N c);        Vector3(const N ptr p);        N getLength();        N length get;        N lengthSquare get;        static Vector3 s_zero;        meta:        N x;        N y;        N z;        N v[$3];        $*        union        {            struct            {                N x,y,z;            };            N v[3];        };        *$    };    template class Vector3<float>;    template class 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);    }    *$}

 

Template <typename N>

Struct Vector3

This is a template class. The C ++ template is complex and powerful, and the compiler is really hard to write. Therefore, the advanced features of most C ++ templates are not supported in idlcpp. After all, idlcpp is only responsible for providing interfaces to the script language, and some simple template functions are enough, the template-related syntax is consistent with that of C ++.

Static Vector3 s_zero;

This row declares a static member variable. Idlcpp supports static member variables, static member functions, and static attributes (actually static member functions ).

Meta:

N x;

N y;

N z;

N v [$3];

$ *

Union

{

Struct

{

N x, y, z;

};

N v [3];

};

* $

Idlcpp does not provide union. Fortunately, you can use meta and $ ** $ to provide their respective content in the generated metadata description code and the C ++ header file.

The following two lines of code

Template class Vector3 <float>;

Template class Vector3 <double>;

It is consistent with the template class declaration in C ++.

In idlcpp, such declaration statements generate metadata information of the corresponding type. These two statements are optional for C ++, but for idlcpp, to make the script see the two template class instance types, these two lines of code must be written.

The following two lines of code

Typedef Vector3 <float> Vector3f;

Typedef Vector3 <double> Vector3d;

The Type alias is declared for the template class instance type. Because these two types of names are: tutorial: Vector3 <float> and: tutorial: Vector3 <double>, it is inconvenient to use them in scripts, with the type alias, you can use: tutorial: Vector3f and: tutorial: Vector3d.

The implementation code of member functions will be described later.

The content of the compiled Tutorial6.h is as follows:

 

//DO NOT EDIT THIS FILE, it is generated by idlcpp//http://www.idlcpp.org#pragma once#include "./Tutorial6.h"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 s_zero;    public:        static Vector3* New();        static Vector3* New(N a,N b,N c);        static Vector3* New(const N* p);        static Vector3* NewArray(unsigned int count);        static Vector3* Clone(const Vector3& v);        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 generates the corresponding function declaration get_length and get_lengthSquare for the read-only attribute length and lengthSquare. Idlcpp generates the corresponding static functions New, NewArray, and Clone according to the constructor declaration.

For other content, C ++ and idl are basically the same.

Then Tutorial6.cpp

 

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


Because the template class code is written in the header file, Tutorial6.cpp only needs to contain the corresponding four files.

Finally, let's take a look at 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()._);


 

Compile and execute the command. The result is as follows:

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.