Lua (3), idlcpplua

Source: Internet
Author: User

Lua (3), idlcpplua

In the previous article, the idlcpp tutorial of C ++ hybrid programming Lua (2) is a hello world example, which only involves the call of static functions. This article will have new content.

Similar to LuaTutorial0, The LuaTutorial1 project also includes three files: LuaTutorial1.cpp, Tutorial1. I, and tutorial1.lua. The content of LuaTutorial1.cpp is basically the same as that of LuaTutorial0.cpp. First, let's take a look at Tutorial1. I:

namespace tutorial{    struct Point    {        float x;        float y;    meta:        Point();    };}

The content of the compiled Tutorial1.h is as follows:

 

//DO NOT EDIT THIS FILE, it is generated by idlcpp//http://www.idlcpp.org#pragma oncenamespace tutorial{ struct Point; }namespace tutorial{    struct Point    {    public:        float x;        float y;    public:        static Point* New();        static Point* NewArray(unsigned int count);    };}

 

The content of the compiled Tutorial1.ic is as follows:

 

//DO NOT EDIT THIS FILE, it is generated by idlcpp//http://www.idlcpp.org#pragma once#include "Tutorial1.h"#include "Tutorial1.mh"#include "../../paf/src/pafcore/RefCount.h"namespace tutorial{    inline Point* Point::New()    {        return new Point();    }    inline Point* Point::NewArray(unsigned int count)    {        return new_array<Point>(count);    }}

 

Some of the code generated above is redundant and will be removed after the compiler is improved.

Struct Point defines a struct.

The following two lines

Float x;

Float y;

It indicates that there are two float data members x and y.

Next line

Meta:

This is a unique keyword of idlcpp and does not exist in C ++. As mentioned above, when the idlcpp compiles the. I file to generate the corresponding header file code, it also generates metadata code. For example, the above line of code

Float x;

Idlcpp generates the same member declaration in tutorial1.h, and also generates the corresponding code in the metadata code. However, sometimes we only want to generate the corresponding code in the metadata, but the header file does not need the corresponding code. Or, in the opposite case, you only want to generate the corresponding code in the header file, and the metadata does not need the corresponding code. To cope with these situations, idlcpp provides three keywords: native, meta, and all. Similar to public, protected, and private in C ++ class declaration. In struct or class

Native:

Meta:

All:

Three forms appear, which affect the subsequent member declaration until the next keyword is encountered. Native indicates that only the corresponding code is generated in the header file, not in the metadata. meta indicates that only the corresponding code is generated in the metadata, not in the header file. All indicates that the corresponding code is generated in the header file and metadata. The default value is all. The meta keyword affects the following line of code.

Point ();

That is, you do not need to declare the default constructor in the header file, so you do not need to write a default constructor implementation. The code to be generated in the metadata is based on the following rules:

Then let's take a look at the content of the script tutorial1.lua:

pt = paf.tutorial.Point.New();pt.x = 1;pt.y = 2;print(pt.x);print(pt.y);print(pt.x._);print(pt.y._);

The compilation and running result is as follows:

 

First line

Pt = paf. tutorial. Point. New ();

Is a new Point object, and the variable pt stores its reference.

Equivalent to: tutorial: Point * pt = new: tutorial: Point ();

The following two lines

Pt. x = 1;

Pt. y = 2;

Equivalent

Pt-> x = 1;

Pt-> y = 2;

The following two rows print the output results of the first two rows. When idlcpp is used, all types in C ++ (including native types such as int and float) are userdata in lua. To convert the C ++ native type to the lua type, use the. _ syntax. See the last two print statements.

 

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.