Previous Idlcpp Tutorial in this C + + Mixed programming Lua Chapter (6)
The first one in this C + + mixed programming Idlcpp tutorial (i)
Similar to the LUATUTORIAL4 project, in the engineering LUATUTORIAL5, four documents were added: LuaTutorial5.cpp, Tutorial5.cpp, tutorial5.i, Tutorial5.lua. The content of LuaTutorial5.cpp basically and LuaTutorial4.cpp identical, no longer repeat.
First look at the contents of tutorial5.i:
#import".. /.. /PAF/SRC/PAFCORE/REFERENCE.I"$$ #include<vector>namespacetutorial{structPoint {floatx; floaty; Point (); Point (floatAfloatb); Meta:point (ConstPointrefPT); }; Exportclassshape:reference {ExportAbstract floatGetarea (); $$ Virtual~Shape () {}}; classShapemanager (value_object) {voidaddshape (Shape ptr shape); floatGettotalarea (); Staticshapemanager ptr getinstance (); $* ~Shapemanager (); Private: Std::vector<Shape*>m_shapes; *$ }; classTriangle:shape {point m_vertices[$3]; Meta:triangle (); $$Virtual floatGetarea (); };}
Compared with tutorial4.i, most of the content is the same, the difference is the declaration of the type shape and the pure virtual function getarea under it;
Export Class Shape:reference
Export abstract float getarea ();
At the top of these two statements, there is one more keyword export. This keyword and C + + in the meaning of the export is completely different, just want to find a ready-made keyword in C + + directly use it, in fact, this is not appropriate, for the time being used first. The notation here means that you can write a type in the script code that "derives" from shape and is able to "overwrite" the virtual function Getarea. Of course, it is actually a derived class generated by idlcpp that mates with the script plug-in code to accomplish a similar task.
This type can be "derived" by the script by adding the keyword export to the type's declaration class, and adding the keyword export to the keyword virtual or abstract before the virtual function declaration indicates that this virtual function can be "overwritten" by the script.
In the mixed use of host languages and scripts, a common usage is to emit events in the host language based on certain conditions, and to write event-handling code in a scripting language, such as using an XML file in Wow to describe the GUI interface, as well as specifying the LUA function name corresponding to the event handler function. Idlcpp provides a script that inherits the C + + class and then overrides the virtual function to implement a similar requirement.
The contents of the Tutorial5.h generated after compilation are as follows:
//Do not EDIT the This FILE, it's generated by Idlcpp//http://www.idlcpp.org#pragmaOnce#include"./tutorial5.h"#include".. /.. /paf/src/pafcore/reference.h"namespacetutorial{classShapemanager;}namespacetutorial{classTriangle;} #include<vector>namespacetutorial{structPoint { Public: floatx; floaty; Point (); Point (floatAfloatb); Public: Staticpoint*New (); Staticpoint* New (floatAfloatb); Staticpoint* NewArray (unsignedintcount); Staticpoint* Clone (Constpoint&PT); }; classShape: Public::p afcore::reference { Public: Virtual::p afcore::type*GetType (); Virtual floatGetarea () =0 ; Virtual~Shape () {}}; classShapemanager { Public: voidAddShape (shape*shape); floatGettotalarea (); Staticshapemanager*getinstance (); ~Shapemanager (); Private: Std::vector<Shape*>m_shapes; }; classTriangle: PublicShape { Public: Virtual::p afcore::type*GetType (); Point m_vertices[3]; Public: Statictriangle*New (); Statictriangle*Newarc (); Statictriangle* NewArray (unsignedintcount); Statictriangle* Newarrayarc (unsignedintcount);Virtual floatGetarea (); };}
The code generated here is basically the same as the Tutorial4.h.
Finally, take a look at the contents of Tutorial5.lua
Circle ={}circle.__index=Circle;functioncircle.new () Circle= {radius =1.0} setmetatable(circle, Circle); Circle.shape= Paf.tutorial.Shape._derive_ (circle); returnCircle;EndfunctionCircle:getarea ()returnSelf.radius * Self.radius *3.1415926;EndCircle=circle.new (); Circle.radius=2.0; Shapemanager=paf.tutorial.ShapeManager.GetInstance (); Shapemanager:addshape (circle.shape);Print(Shapemanager:gettotalarea (). _); 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); Shapemanager:addshape (triangle);Print(Shapemanager:gettotalarea (). _);
In the above code, a type circle is written. In the function circle.new through the following line
Circle.shape = Paf.tutorial.shape._derive_ (circle);
To emulate inheritance, Syntax: C + + type. _derive_ (Script object) is used to perform impersonation inheritance behavior. In fact, Circle.shape is a reference to a derived class instance of C + + type shape, where the shape type is needed in C + +, passing the Circle.shape in the past, as used below.
Shapemanager:addshape (Circle.shape);
Then provide a function in type circle with the same name as the C + + base class Getarea to count the area of the circle, and the script plug-in will find the corresponding function to call when it is finally used.
Compile execution with results such as:
Idlcpp Tutorials for C + + mixed programming Lua (7)