Idlcpp Tutorials for C + + Hybrid Programming Python chapter (3)

Source: Internet
Author: User

A idlcpp tutorial on C + + mixed programming Python (2) is an example of Hello World and involves only the invocation of a static function. This article will have a new content.

Similar to PythonTutorial0, in engineering PythonTutorial1, three files were added

PythonTutorial1.cpp, tutorial1.i, tutorial1.py

The content of PythonTutorial1.cpp basically and PythonTutorial0.cpp identical, no longer repeat.

First look at the contents of tutorial1.i:

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

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

//Do not EDIT the This FILE, it's generated by Idlcpp//http://www.idlcpp.org#pragmaOncenamespacetutorial{structPoint ;}namespacetutorial{structPoint { Public:        floatx; floaty;  Public:        Staticpoint*New (); Staticpoint* NewArray (unsignedintcount); };}

The contents of the Tutorial1.ic generated after compilation are as follows:

//Do not EDIT the This FILE, it's generated by Idlcpp//http://www.idlcpp.org#pragmaOnce#include"Tutorial1.h"#include"TUTORIAL1.MH"#include".. /.. /paf/src/pafcore/refcount.h"namespacetutorial{Inline Point*point::new () {return NewPoint (); } Inline Point* Point::newarray (unsignedintcount) {        returnNew_array<point>(count); }}

Some of the code generated above is redundant, and later the compiler will be improved to eliminate the extra code.

A struct point defines a struct.

The following two lines

float x;

Float y;

Indicates that there are two data members of type float x and Y.

And then the next line

Meta

This is a unique keyword for idlcpp, and there is no corresponding presence in C + +. As mentioned above, Idlcpp compiles the. i file to generate the corresponding header file code at the same time, it will also produce Narimoto data code. Like the above line of code

float x;

Idlcpp generates the same member declarations in the Tutorial1.h, along with the corresponding code generation in the metadata code. But sometimes we just want to generate the code in the metadata, and there's no need for the code in the header file. Or in the opposite case, you only want to generate the code in the header file, and the metadata does not need to have the corresponding code. To address these situations, Idlcpp provides three keyword Native,meta,all. The usage is similar to public in C + + class declarations, protected, private usage. That is, in a struct or class,

Native

Meta

All

Three forms appear, affecting subsequent member declarations until the next corresponding keyword is encountered. Where native indicates that only the corresponding code is generated in the header file and not generated in the metadata, meta indicates that the corresponding code is generated only in the metadata and not in the header file. All means that the corresponding code is generated in both the header file and the metadata. The default value is all. Here the meta-keyword affects the following line of code

Point ();

That is, the declaration of the default constructor is not required in the header file, and there is no need to write an implementation of the default constructor outside. The corresponding code in the metadata needs to be generated here based on the following rules:

    1. If the declaration of a type has a constructor (non-copy constructor), a static function, new, is generated to create an object in the scripting language. See Tutorial1.ic in the Point::new (). This allows you to create a point object in the scripting language by calling Point.new ().
    2. If the declaration of a type has a copy constructor, a static function clone is generated that is used to copy an object in the scripting language.
    3. If the declaration of a type has a default constructor, a static function, NewArray, is generated to create an array of objects in the scripting language.

Then take a look at the contents of the script tutorial1.py:

Import== = 1= 2; Print (pt.x); Print (PT.Y); Print (pt.x._); Print (pt.y._);


Compile run results such as:

This line

PT = Paf.tutorial.Point.New ();

is a new point object, and the variable PT holds its reference.

Equivalent in C + +:: Tutorial::P oint* pt = new:: Tutorial::P oint ();

The following two lines

Pt.x = 1;

Pt.y = 2;

Equivalent to C + +

Pt->x = 1;

Pt->y = 2;

The next two lines print out the first two lines of the result. When using Idlcpp, any type in C + + (including native types such as int, float, etc.) is pyobject in Python. To convert a C + + native type to a corresponding type in Python, use the. _ syntax, see the last two lines of the print statement.

Idlcpp Tutorials for C + + Hybrid Programming Python chapter (3)

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.