C ++ idlcpp tutorial Python (4), idlcpppython

Source: Internet
Author: User

C ++ idlcpp tutorial Python (4), idlcpppython

Last article in this C ++ mixed programming idlcpp tutorial Python Article (3)

Article 1 idlcpp tutorial in C ++ hybrid programming (I)

Similar to the previous project, pythontutorial has added three files: PythonTutorial2.cpp, Tutorial2. I, and tutorial2.py. The content of PythonTutorial2.cpp is basically the same as that of PythonTutorial1.cpp. First, let's take a look at Tutorial2. I:

 

namespace tutorial{    struct Point    {        float x;        float y;    meta:        Point();        Point(float a, float b);        $*        Point()        {}        Point(float a, float b)        {            x = a;            y = b;        }        *$    };    struct Rectangle    {        Point m_min;        Point m_max;        float left set get;        float right set get;    meta:        float bottom set get;        float top set get;        float area get;        float getArea();    all:        Rectangle(const Point ref min, const Point ref max);        Rectangle();    meta:        Rectangle(const Rectangle ref pt);        $*        void set_bottom(float bottom)        {            m_min.y = bottom;        }        float get_bottom()        {            return m_min.y;        }        void set_top(float top)        {            m_max.y = top;        }        float get_top()        {            return m_max.y;        }        float get_area()        {            return (m_max.x - m_min.x)*(m_max.y - m_min.y);        }        float getArea()        {            return (m_max.x - m_min.x)*(m_max.y - m_min.y);        }        *$    };    $*    inline Rectangle::Rectangle(const Point& min, const Point& max) : m_min(min), m_max(max)    {    }    inline Rectangle::Rectangle()    {}    inline float Rectangle::get_left()    {        return m_min.x;    }    inline void Rectangle::set_left(float left)    {        m_min.x = left;    }    inline float Rectangle::get_right()    {        return m_max.x;    }    inline void Rectangle::set_right(float right)    {        m_max.x = right;    }    *$}

 

There is still a struct Point here. Compared with the struct Point in PythonTutorial1, in addition to the original default constructor, a constructor with two parameters is added.

Point (float a, float B );

Both constructors are in the meta: segment, so idlcpp does not generate the corresponding function declaration in Tutorial2.h, And the implementation code of the constructor is directly written in $ *** $, the code is inserted to the corresponding location in Tutorial2.h. Of course, you can also choose not to use meta:. In this case, the declaration part of the two constructors will appear in the struct Point of Tutorial2.h, so the implementation code will be written outside. A new type of struct Rectangle is added after the struct Point. First two rows

Point m_min;

Point m_max;

Two data members are declared. Then

Float left set get;

Float right set get;

Here, a new syntax: attribute appears. The attribute syntax comes from C #. The format is: Type + name + optional set and get. In C ++, two corresponding member functions are generated. The function names are set _ + attribute names and get _ + attribute names respectively, for example, the two member functions generated for the left attribute are void set_left (float) and float get_left (). Then there are three attribute declarations.

Float bottom set get;

Float top set get;

Float area get;

The attribute area is a read-only attribute, that is, only the float get_area () member function is generated. Then

Float getArea ();

This is a member function. The function format generated in C ++ is the same as that in this example. Then

Rectangle (const Point ref min, const Point ref max );

This is a constructor, where a new keyword ref appears. The keyword ref also comes from C #, which is equivalent to the & in the function parameter declaration in C ++ &. Similarly, there is a keyword ptr, which is equivalent to * in the function parameter declaration in C ++ *. The reason for using ref is not because of the pointer. In C ++, pointer usage is relatively free. In idlcpp, pointer usage is limited. Considering the differences, to avoid omissions in Porting existing C ++ code to idl, we decided to use ptr instead of *. In order to look uniform, we also replaced & with ref &. Therefore, the corresponding C ++ code here is

Rectangle (const Point & min, const Point & max );

The following is the implementation code of a specific function. Put them in $ ** $ to copy them to the header file.

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

 

//DO NOT EDIT THIS FILE, it is generated by idlcpp//http://www.idlcpp.org#pragma once#include "./Tutorial2.h"namespace tutorial{ struct Rectangle; }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);        Point()        {}        Point(float a, float b)        {            x = a;            y = b;        }            };    struct Rectangle    {    public:        Point m_min;        Point m_max;        void set_left( float);        float get_left();        void set_right( float);        float get_right();        Rectangle(const Point& min,const Point& max);        Rectangle();    public:        static Rectangle* New(const Point& min,const Point& max);        static Rectangle* New();        static Rectangle* NewArray(unsigned int count);        static Rectangle* Clone(const Rectangle& pt);        void set_bottom(float bottom)        {            m_min.y = bottom;        }        float get_bottom()        {            return m_min.y;        }        void set_top(float top)        {            m_max.y = top;        }        float get_top()        {            return m_max.y;        }        float get_area()        {            return (m_max.x - m_min.x)*(m_max.y - m_min.y);        }        float getArea()        {            return (m_max.x - m_min.x)*(m_max.y - m_min.y);        }            };    inline Rectangle::Rectangle(const Point& min, const Point& max) : m_min(min), m_max(max)    {    }    inline Rectangle::Rectangle()    {}    inline float Rectangle::get_left()    {        return m_min.x;    }    inline void Rectangle::set_left(float left)    {        m_min.x = left;    }    inline float Rectangle::get_right()    {        return m_max.x;    }    inline void Rectangle::set_right(float right)    {        m_max.x = right;    }    }

 

Basically, the content corresponds to a pair of Tutorial2. I. There are several static functions in Point and Rectangle, whose names are New, Clone, and NewArray. These are all generated in the form of constructor. The implementation code of these functions is in Tutorial2.ic. The content of the compiled Tutorial2.ic is as follows:

 

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

 

Then let's take a look at the content of the script tutorial2.py:

 

import pafpython;paf = pafpython.paf;rect1 = paf.tutorial.Rectangle();rect1.m_min.x = 1;rect1.m_min.y = 2;print(rect1.left._);print(rect1.bottom._);rect1.right = 3;rect1.top = 4;print(rect1.m_max.x._);print(rect1.m_max.y._);print(rect1.area._);rect2 = paf.tutorial.Rectangle(rect1.m_min, paf.tutorial.Point(5,5));print(rect2.getArea()._);rect3 = paf.tutorial.Rectangle.Clone(rect2);print(rect3.getArea()._);

Rect1 = paf. tutorial. Rectangle ();

This is a simplified method of rect1 = paf. tutorial. Rectangle. New.

Rect1 is operated by data members and attributes respectively.

Rect2 = paf. tutorial. Rectangle (rect1.m _ min, paf. tutorial. Point (5, 5 ));

The Rectangle constructor with parameters is called (in fact, the static function is New ).

Rect3 = paf. tutorial. Rectangle. Clone (rect2 );

Equivalent to Rectangle * rect3 = new Rectangle (* rect2) in C ++ );

The compilation and running result is as follows:

 

 

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.