Lua (5), idlcpplua
Previous Article Lua in this idlcpp tutorial on C ++ hybrid programming (4)
Article 1 idlcpp tutorial in C ++ hybrid programming (I)
Similar to the previous project, the luatutori_3 project also includes three files: LuaTutorial3.cpp, Tutorial3. I, and tutorial3.lua. The content of LuaTutorial3.cpp is basically the same as that of LuaTutorial2.cpp.
First, let's take a look at Tutorial3. I:
namespace tutorial{ struct Point { float x; float y; meta: Point(); Point(float a, float b); Point(const Point ref pt); $* Point() {} Point(float a, float b) { x = a; y = b; } *$ }; struct Shape { abstract float getArea(); $$ virtual ~Shape() {} }; struct Triangle : Shape { Point m_vertices[$3]; meta: static Triangle new New(); $* virtual float getArea() { return fabs(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; } static Triangle* New() { return new Triangle; } *$ };}
There is still a struct Point here. Introduced the base class struct Shape. This line
Abstract float getArea ();
Declares a pure virtual function, which is equivalent
Virtual float getArea () = 0;
If it is not a pure virtual function, use the keyword virtual instead of abstract.
The Triangle type is added.
Struct Triangle: Shape
Like C ++, it uses: To represent inheritance. Because idlcpp indicates interface information, only public inheritance is allowed. Unlike C ++, idlcpp does not have the public, protected, and private keywords.
Then the data member m_vertices;
Point m_vertices [$3];
Idlcpp only cares about interface information. In its syntax analysis, only Point m_vertices [] can be seen. Number 3 requires code insertion. $ Indicates that an identifier or INTEGER (a string consisting of letters, underscores, and numbers) directly connected to the end is inserted to the corresponding position of the generated C ++ header file. Next two lines
Meta:
Static Triangle new New ();
A static function named New is declared, and its implementation code is included in the subsequent type declaration. Therefore, meta is used to prevent function declaration from being generated in the header file, as described above, if idlcpp sees the constructor declaration, it will generate the static function New, so the constructor declaration cannot appear at this time to avoid name conflict. Compare the C ++ declaration in the implementation section below
Static Triangle * New ()
Here, the difference between C ++ and C ++ is that one is missing * and a new is added. New is a keyword in idlcpp. It is placed between the function return value type and function name, indicating that an object is created in the form of new in the Function and Its pointer is returned, the outside world needs to delete it in the form of delete (there is another case about reference count, which will not be discussed for the moment ). Generally, the script language comes with a garbage collection mechanism, which automatically manages the distribution and release of memory. Programmers generally do not have to worry about when to delete objects. In C ++, the lifetime of allocating objects on Stacks is generally maintained by programmers. To handle the differences, idlcpp has the following forms in the return value type section of function declaration:
Idlcpp Declaration |
C ++ statement |
Implementation |
TypeName |
TypeName |
Return Value |
TypeName ref |
TypeName & |
Return reference |
TypeName ptr |
TypeName * |
Return pointer |
TypeName new |
TypeName * |
Returns the pointer. delete is required for the outside world, or reference count is added. release is required for the outside world. |
TypeName new [] |
TypeName * |
Returns a pointer. delete [] is required from outside. |
For example, the following C ++ code:
int g_a;int* getGlobal(){ return &g_a;}int* getNew(){ return new int;}int* getNewArray(int count){ return new int[count];}
The return values of the three functions are int *, but for the next two functions, delete and delete [] are used to release the memory. In terms of syntax, the declaration of the function cannot distinguish between these conditions. Programmers can only perform different processing based on the actual situation. In the script language, you do not want to see the displayed call to delete objects. Therefore, idlcpp is distinguished in the generated metadata code through a syntax declaration, and then processed by the Runtime Library (pafcore. dll.
The content of the compiled Tutorial3.h is as follows:
//DO NOT EDIT THIS FILE, it is generated by idlcpp//http://www.idlcpp.org#pragma once#include "./Tutorial3.h"namespace tutorial{ struct Triangle; }namespace tutorial{ struct Point { public: float x; float y; public: static Point* New(); static Point* New(float a,float b); static Point* NewArray(unsigned int count); static Point* Clone(const Point& pt); Point() {} Point(float a, float b) { x = a; y = b; } }; struct Shape { public: virtual float getArea() = 0 ; virtual ~Shape() {} }; struct Triangle : public Shape { public: Point m_vertices[3]; virtual float getArea() { return fabs(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; } static Triangle* New() { return new Triangle; } };}
The content basically corresponds to Tutorial3. I. Then let's take a look at the content 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 data members are set. During the running process, the range of objects to be detected is 0 to 2. If the range is exceeded, an error is returned, finally, call the getArea () function declared in the Shape of the base class. Because this is a virtual function, Traingle: getArea () is called ().
The compilation and running result is as follows: