Lua (7), idlcpplua
Previous Article Lua in this idlcpp tutorial on C ++ hybrid programming (6)
Article 1 idlcpp tutorial in C ++ hybrid programming (I)
Similar to the LuaTutorial4 project, LuaTutorial5 also includes four files: LuaTutorial5.cpp, Tutorial5.cpp, Tutorial5. I, and tutorial5.lua. The content of LuaTutorial5.cpp is basically the same as that of LuaTutorial4.cpp.
First, let's take a look at Tutorial5. I:
#import "../../paf/src/pafcore/Reference.i"$$#include <vector>namespace tutorial{ struct Point { float x; float y; Point(); Point(float a, float b); meta: Point(const Point ref pt); }; export class Shape : Reference { export abstract float getArea(); $$ virtual ~Shape() {} }; class ShapeManager(value_object) { void addShape(Shape ptr shape); float getTotalArea(); static ShapeManager ptr GetInstance(); $* ~ShapeManager(); private: std::vector<Shape*> m_shapes; *$ }; class Triangle : Shape { Point m_vertices[$3]; meta: Triangle(); $$virtual float getArea(); };}
Compared with Tutorial4. I, most of the content is the same. The difference lies in the declaration of the type Shape and the pure virtual function getArea under it;
Export class Shape: Reference
Export abstract float getArea ();
An export keyword is added at the beginning of the two statements. This keyword has different meanings from the export in C ++. It is only intended to find a ready-made keyword in C ++ for direct use. In fact, this is not suitable for use temporarily. The writing method here means that you can write a type in the script code so that it "derives" from the Shape and can "Overwrite" the virtual function getArea. Of course, a derived class generated by idlcpp works with the script plug-in code to complete similar tasks.
By adding the keyword export before the declared class of the type, this type can be "derived" by the script ", add the keyword export before the virtual or abstract keywords of the virtual function declaration to indicate that the virtual function can be "overwritten" by the script ".
In the mixed use of the host language and script, a common usage is to issue events based on certain conditions in the host language, and write the event processing code in the script language, for example, you can use an XML file in WOW to describe the GUI and specify the Lua function name corresponding to the event processing function. The script provided by idlcpp inherits the C ++ class and then overrides the functions of virtual functions, which can achieve similar requirements.
The content of the compiled Tutorial5.h is as follows:
//DO NOT EDIT THIS FILE, it is generated by idlcpp//http://www.idlcpp.org#pragma once#include "./Tutorial5.h"#include "../../paf/src/pafcore/Reference.h"namespace tutorial{ class ShapeManager; }namespace tutorial{ class Triangle; }#include <vector>namespace tutorial{ struct Point { public: float x; float y; Point(); Point(float a,float b); public: static Point* New(); static Point* New(float a,float b); static Point* NewArray(unsigned int count); static Point* Clone(const Point& pt); }; class Shape : public ::pafcore::Reference { public: virtual ::pafcore::Type* getType(); virtual float getArea() = 0 ; virtual ~Shape() {} }; class ShapeManager { public: void addShape(Shape* shape); float getTotalArea(); static ShapeManager* GetInstance(); ~ShapeManager(); private: std::vector<Shape*> m_shapes; }; class Triangle : public Shape { public: virtual ::pafcore::Type* getType(); Point m_vertices[3]; public: static Triangle* New(); static Triangle* NewARC(); static Triangle* NewArray(unsigned int count); static Triangle* NewArrayARC(unsigned int count);virtual float getArea(); };}
The code generated here is basically the same as Tutorial4.h.
Finally, let's take a look at Tutorial5.lua.
Circle = {}Circle.__index = Circle;function Circle.New() circle= {radius = 1.0} setmetatable(circle, Circle); circle.shape = paf.tutorial.Shape._Derive_(circle); return circle;endfunction Circle:getArea() return self.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 of Circle is written. Use the following line in function Circle. New:
Circle. shape = paf. tutorial. Shape. _ Derive _ (circle );
Syntax: C ++ type. _ Derive _ (script object) is used to simulate inheritance. Actually, circle. shape is the reference of the C ++ type Shape derived class instance. In C ++, where the Shape type is needed, the circle. you can pass the shape to the image as follows.
ShapeManager: addShape (circle. shape );
Then, in the Circle type, provide a function getArea with the same name as the C ++ base class to count the area of the Circle. In the end, the script plug-in will find the corresponding function for calling.
Compile and execute the command. The result is as follows: