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

Source: Internet
Author: User

Previous Idlcpp Tutorial in this C + + mixed programming Python chapter (7)

The first one in this C + + mixed programming Idlcpp tutorial (i)

similar to the previous project, four documents were added to the project PythonTutorial6: PythonTutorial6.cpp, Tutorial6.cpp, tutorial6.i, tutorial6.py. The content of PythonTutorial6.cpp basically and PythonTutorial5.cpp identical, no longer repeat. First Look at the contents of tutorial6.i:

#import".. /.. /PAF/SRC/PAFCORE/TYPEDEF.I"namespacetutorial{structVector3<n>{Vector3 (); Vector3 (Constvector3&v);        Vector3 (n A, n B, n c); Vector3 (ConstNp);        N GetLength (); N lengthGet; N LengthsquareGet; StaticVector3<n>S_zero;        Nocode N x;        Nocode N y;        Nocode N Z; Nocode N v[$3]; $*Union {struct{N x, y, Z;            }; N v[3];        }; *$    }; Export Vector3<float>; Export Vector3<Double>; typedef VECTOR3<float>vector3f; typedef VECTOR3<Double>Vector3D; $*Template<typename n>Vector3<N> Vector3<n>::s_zero (0,0,0); Template<typename n>inline Vector3<N>:: Vector3 () {} template<typename n>inline Vector3<n>::vector3 (Constvector3<n>&v): X (v.x), Y (v.y), Z (v.z) {} template<typename n>inline Vector3<N>:: Vector3 (n A, n B, n C): X (a), Y (b), Z (c) {} template<typename n>inline Vector3<n>::vector3 (Constn p): X (p[0]), Y (p[1]), Z (p[2]) {} template<typename n>inline N Vector3<N>:: GetLength () {returnN (sqrt (x * x + y * y + z *z)); } template<typename n>inline N Vector3<N>:: Get_length () {returnN (sqrt (x * x + y * y + z *z)); } template<typename n>inline N Vector3<N>:: Get_lengthsquare () {return(x * x + y * y + z *z); }    *$}

struct vector3<n>

This is a template class, C + + templates are complex and powerful, the compiler is really difficult to write. So the advanced features of most C + + templates are not supported in Idlcpp, after all, Idlcpp is only responsible for providing the interface to the scripting language, and there are some simple template functions that suffice. In addition, because the values are not prepared as template parameters, only types are supported as template parameters, so syntax takes the form of C #. is not the same as the template syntax for C + +.  

Static Vector3 S_zero;

This line declares a static member variable. Idlcpp supports static member variables, static member functions, and static properties (which are actually static member functions).

Nocode N x;

Nocode N y;

Nocode N Z;

Nocode N v[$3];

$*

Union

{

struct

{

N x, y, Z;

};

N V[3];

};

*$

Idlcpp did not provide a union. Fortunately, the respective contents can be provided by Nocode and $**$ respectively in the generated metadata description code and the C + + header file.

The following two lines of code

Export vector3<float>;

Export vector3<double>;

Used to generate metadata code. such declaration statements in Idlcpp generate metadata information of the corresponding type.

The following two lines of code

typedef vector3<float> VECTOR3F;

typedef vector3<double> Vector3D;

The type alias is declared for the template class instance type. Because the two type names are::tutorial::vector3<float> and:: Tutorial::vector3<double>, the use of the script is inconvenient, with the type alias can be passed: tutorial::vector3f and:: Tutorial::vector3d to use.

The following is the implementation of the member function code, not repeat.

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

//Do not EDIT the This FILE, it's generated by Idlcpp//http://www.idlcpp.org#pragmaOncenamespacetutorial{Template<typename n>structVector3 { Public: Vector3 (); Vector3 (Constvector3&v);        Vector3 (N a,n b,n c); Vector3 (ConstNp);        N GetLength ();        N get_length ();        N Get_lengthsquare (); StaticVector3<n>S_zero; Union {struct{N x, y, Z;            }; N v[3];            };    }; typedef VECTOR3<float>vector3f; typedef VECTOR3<Double>Vector3D; Template<typename n>Vector3<N> Vector3<n>::s_zero (0,0,0); Template<typename n>inline Vector3<N>:: Vector3 () {} template<typename n>inline Vector3<n>::vector3 (Constvector3<n>&v): X (v.x), Y (v.y), Z (v.z) {} template<typename n>inline Vector3<N>:: Vector3 (n A, n B, n C): X (a), Y (b), Z (c) {} template<typename n>inline Vector3<n>::vector3 (Constn p): X (p[0]), Y (p[1]), Z (p[2]) {} template<typename n>inline N Vector3<N>:: GetLength () {returnN (sqrt (x * x + y * y + z *z)); } template<typename n>inline N Vector3<N>:: Get_length () {returnN (sqrt (x * x + y * y + z *z)); } template<typename n>inline N Vector3<N>:: Get_lengthsquare () {return(x * x + y * y + z *z); }    }

Idlcpp generates the corresponding function declarations get_length and get_lengthsquare for read-only property length and Lengthsquare.

Other things, C + + and IDL are basically the same.

And then the Tutorial6.cpp.

" Tutorial6.h "  "tutorial6.mh " "Tutorial6.ic"   "tutorial6.mc"

Because the template class code is written in the header file, so Tutorial6.cpp only need to include the corresponding four files.  

Finally, take a look at the contents of tutorial6.py

Import== Paf.tutorial.Vector3f.New (1,1,2= 1; Print  = Paf.tutorial.Vector3d (2,2,1); v2.v[2] = 2; Print (V2.getlength (). _);

compile execution with results such as:

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

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.