Lua (9), idlcpplua

Source: Internet
Author: User

Lua (9), idlcpplua

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

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

 

Compared with the previous project, except four LuaTutorial7.cpp, Tutorial7.cpp, Tutorial7.cpp, Tutorial7. I, and tutorial7.lua files, Tutorial6.cpp is also added to the project. The content of LuaTutorial7.cpp is basically the same as that of LuaTutorial6.cpp.

First, let's take a look at 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);    }    *$}

First line

# Import "Tutorial6. I"

The template class Vector3 is used in the definition of Ray3, so this file must be introduced first.

Struct Ray3 <T>

The template class Ray3 is defined here. There are two member variables m_origin and m_direction of the Vector3 type <T>. In this class, a ray is represented by the m_origin + m_direction * t (t> = 0) parameter equation. There are two overload functions named getPoint to obtain a coordinate point on the ray.

Export Ray3 <float>;

Export Ray3 <double>;

Template instantiation. These two lines of code instruct idlcpp to generate corresponding types of metadata information.

Typedef Ray3 <float> Ray3f;

Typedef Ray3 <double> Ray3d;

Defines the type alias for ease of use.

The content of the compiled Tutorial7.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 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);    }    }

 

Then Tutorial7.cpp

 

 

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

 

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

In addition, template class Ray3 uses template class Vector3, so the instantiation types of Ray3 <float> and Ray3 <double> also use the instantiation types of Vector3 <float> and Vector3 <double>, the metadata of Vector3 <float> is also used in the corresponding Ray3 <float> metadata. Therefore, Tutorial6.cpp needs to be added in this project.

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

The first line:

P = paf. float. NewArray (3 );

Create a float array with three elements in total. float is a built-in type. The native types of C ++ are supported in idlcpp, as follows:

boolcharsigned charunsigned charwchar_tshortunsigned shortlong    unsigned longlong longunsigned long longintunsigned int    floatdoublelong double

Some types have spaces, which makes it easy to use scripts and defines aliases for these types. For details, see Typedef. I in pafcore.

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.