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

Source: Internet
Author: User
Tags lua

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

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

Similar to the previous project, three documents were added to the project LuaTutorial3: LuaTutorial3.cpp, tutorial3.i, Tutorial3.lua. The content of LuaTutorial3.cpp basically and LuaTutorial2.cpp identical, no longer repeat.

First look at the contents of tutorial3.i:

namespacetutorial{structPoint {floatx; floaty;        Meta:point (); Point (floatAfloatb); Point (ConstPointrefPT); $*Point () {}, point (floatAfloatb) {x=A; Y=b; }        *$    }; structShape {Abstract floatGetarea (); $$        Virtual~Shape () {}}; structTriangle:shape {point m_vertices[$3]; Meta:StaticTriangleNewNew (); $*Virtual floatGetarea () {returnFabs (m_vertices[0].x * m_vertices[1].y+ m_vertices[1].x * m_vertices[2].y+ m_vertices[2].x * m_vertices[0].y-m_vertices[0].x * m_vertices[2].y-m_vertices[1].x * m_vertices[0].y-m_vertices[2].x * m_vertices[1].Y) *0.5; }        Statictriangle*New () {return NewTriangle; }        *$    };}


There is still a struct point here. The base class struct Shape is introduced. Where this line

Abstract float Getarea ();

Represents a pure virtual function, which is equivalent to the C + +

Virtual float Getarea () = 0;

If it is not a pure virtual function, use the keyword virtual instead of abstract.

New added Type Triangle

struct Triangle:shape

As with C + +, use: to represent inheritance. Because Idlcpp represents interface information, only public inheritance is available. Unlike C + +, Idlcpp does not have a public, protected, private three keywords.

Then is the data member m_vertices;

Point M_vertices[$3];

Idlcpp only cares about the interface information, and in its parsing section, only point m_vertices[] is visible. The number 3 requires a way to insert the code. Where $ represents a directly attached identifier or integer (actually a string consisting of letters, underscores, and numbers) will be inserted into the location corresponding to the generated C + + header file. Next two lines

Meta

Static Triangle new New ();

Declares a static function called new, whose implementation code is within a subsequent type declaration, so here is a meta block to generate the function declaration in the header file, as mentioned earlier, idlcpp if the declaration of the constructor is seen, a static function new is generated, so the declaration of the constructor cannot occur at this time. Avoid name collisions. Compare the C + + declarations behind the implementation section

Static triangle* New ()

This is inconsistent with C + + where there is one less * and one more new. New is a keyword in idlcpp, placed between the function return value type and the functions name, indicating that the function creates an object in new form, returns its pointer, and the outside needs to delete it in the form of delete (there is another case of reference counting, which is not discussed). In scripting languages, there is a general garbage collection mechanism, and the scripting language automatically manages the allocation release of memory. Programmers generally don't care about when to delete objects, while the lifetime of allocating objects on a heap in C + + is generally maintained by programmers. To handle the differences, Idlcpp has the following forms in the return value type portion of the function declaration:

Idlcpp statement

C + + declaration

Realize

TypeName

TypeName

return value

TypeName ref

typename&

return reference

TypeName PTR

typename*

return pointer

TypeName New

typename*

return pointer, external need delete, or add reference count, outside need release

TypeName New []

typename*

Return pointer, outside need delete[]

For example, the following C + + code:

 int   g_a;  int  * Getglobal () { return  &G_A;}  int  * Getnew () { return  new  int  ;} int  * Getnewarray ( count) { return  new  int  [count];}  

The return value types of the three functions are int*, but for the next two, the memory is freed with delete and delete[], and on the syntactic level, the declaration from the function cannot distinguish between these cases. It can only be handled differently by the programmer according to the actual situation. In the scripting language, you do not want to see a call to the deleted object that is displayed. So idlcpp is differentiated in the generated metadata code by syntax-level declarations, which are then processed by the runtime Library (Pafcore.dll).

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

//Do not EDIT the This FILE, it's generated by Idlcpp//http://www.idlcpp.org#pragmaOnce#include"./tutorial3.h"namespacetutorial{structTriangle;}namespacetutorial{structPoint { Public:        floatx; floaty;  Public:        Staticpoint*New (); Staticpoint* New (floatAfloatb); Staticpoint* NewArray (unsignedintcount); Staticpoint* Clone (Constpoint&PT); Point () {}, point (floatAfloatb) {x=A; Y=b;    }            }; structShape { Public:        Virtual floatGetarea () =0 ; Virtual~Shape () {}}; structTriangle: PublicShape { Public: Point m_vertices[3]; Virtual floatGetarea () {returnFabs (m_vertices[0].x * m_vertices[1].y+ m_vertices[1].x * m_vertices[2].y+ m_vertices[2].x * m_vertices[0].y-m_vertices[0].x * m_vertices[2].y-m_vertices[1].x * m_vertices[0].y-m_vertices[2].x * m_vertices[1].Y) *0.5; }        Statictriangle*New () {return NewTriangle; }            };}

The content is basically corresponding to the tutorial3.i in one by one. Then take a look at the contents of the script Tutorial3.lua:

Triangle = paf.tutorial.Triangle (); triangle.m_vertices[0] = paf.tutorial.Point (0,0  ); triangle.m_vertices[1] = Paf.tutorial.Point (0,1); triangle.m_ vertices[2] = Paf.tutorial.Point (1,1); Print (Triangle:getarea (). _);

A Tirangle object is created and the data member is set, where the runtime can detect that the subscript range is 0 to 2, if the out of range will be an error, and finally call the function declared in its base class shape Getarea (), because this is a virtual function, so it will eventually be called to Traingle:: Getarea ().

Compile run results such as:

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

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.