Idlcpp Tutorials for C + + mixed programming Lua (8)

Source: Internet
Author: User

Previous Idlcpp Tutorial in this C + + Mixed programming Lua chapter (7)

The first one in this C + + mixed programming Idlcpp tutorial (i)

Similar to the previous project, four documents were added to the project LuaTutorial6: LuaTutorial6.cpp, Tutorial6.cpp, tutorial6.i, Tutorial6.lua. The content of LuaTutorial6.cpp basically and LuaTutorial5.cpp identical, no longer repeat.

First look at the contents of tutorial6.i:

namespacetutorial{Template<typename n>structVector3 {Vector3 (); Vector3 (ConstVector3refv);        Vector3 (n A, n B, n c); Vector3 (ConstN ptr p);        N GetLength (); N lengthGet; N LengthsquareGet; StaticVector3 S_zero;        Meta:n x;        N y;        N Z; N v[$3]; $*Union {struct{N x, y, Z;            }; N v[3];        }; *$    }; Templateclassvector3<float>; Templateclassvector3<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 (Constvector3<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 (Constn p): X (p[0]), Y (p[1]), Z (p[2]) {} template<typename n>inline N Vector3<N>:: GetLength () {returnN (sqrt (x * x + y * y + z *z)); } template<typename n>inline N Vector3<N>:: Get_length () {returnN (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, C + + templates are complex and powerful, the compiler is really difficult to write. So most of the advanced features of the C + + template are not supported in Idlcpp, after all, Idlcpp is only responsible for the scripting language interface, there are some simple template functionality is sufficient, template-related syntax and C + + are consistent.  

Static Vector3 S_zero;

This line declares a static member variable. Idlcpp supports static member variables, static member functions, and static properties (which are actually static member functions).

Meta

N x;

N y;

N Z;

N v[$3];

$*

Union

{

struct

{

N x, y, Z;

};

N V[3];

};

*$

Idlcpp did not provide a union. Fortunately, Meta and $**$ can be used to provide their own content in the generated metadata description code and C + + header files respectively.

The following two lines of code

Template class vector3<float>;

Template class vector3<double>;

Consistent with template class declarations in C + +.

It is not necessary for C + + to generate metadata information of the corresponding type through such declaration statements in Idlcpp, but for Idlcpp, the two lines of code must be written in order for the script to see the two template class instance types.

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 the two type names are::tutorial::vector3<float> and:: Tutorial::vector3<double>, the use of the script is inconvenient, with the type alias can be passed: tutorial::vector3f and:: Tutorial::vector3d to use.

The following is the implementation of the member function code, not repeat.

The contents of the Tutorial6.h generated after compilation are as follows:

//Do not EDIT the This FILE, it's generated by Idlcpp//http://www.idlcpp.org#pragmaOnce#include"./tutorial6.h"namespacetutorial{Template<typename n>structVector3 { Public: Vector3 (); Vector3 (Constvector3&v);        Vector3 (N a,n b,n c); Vector3 (ConstNp);        N GetLength ();        N get_length ();        N Get_lengthsquare (); StaticVector3 S_zero;  Public:        Staticvector3*New (); Staticvector3*New (N a,n b,n c); Staticvector3* New (ConstNp); Staticvector3* NewArray (unsignedintcount); Staticvector3* Clone (Constvector3&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 (Constvector3<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 (Constn p): X (p[0]), Y (p[1]), Z (p[2]) {} template<typename n>inline N Vector3<N>:: GetLength () {returnN (sqrt (x * x + y * y + z *z)); } template<typename n>inline N Vector3<N>:: Get_length () {returnN (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 declarations get_length and get_lengthsquare for read-only property length and Lengthsquare. Idlcpp generates the corresponding static function New,newarray and clone based on the constructor declaration.

Other things, C + + and IDL are basically the same.

And then the Tutorial6.cpp.

" Tutorial6.h "  "tutorial6.mh " "Tutorial6.ic"   "tutorial6.mc"


because the template class code is written in the header file, so Tutorial6.cpp only need to include the corresponding four files.  

Finally, take a look at the contents of Tutorial6.lua

V1 = paf.tutorial.Vector3f (1,1,21; Print  = Paf.tutorial.Vector3d (2,2,1); v2.v[22 ; Print (V2:getlength (). _);


Compile execution with results such as:

Idlcpp Tutorials for C + + mixed programming Lua chapter (8)

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.