Python calls C ++ (2) encapsulated by boost Python)

Source: Internet
Author: User

The last time I wrote a command to encapsulate and call C functions using the APIS provided by python. However, because the method of using APIS is too primitive, it is extremely troublesome for classes or structures. Therefore, I chose boost python to encapsulate classes, and similar tools include swig. The reason why I chose boost is that it does not need to introduce other Interface Description Languages, encapsulation is also C ++ Code. In addition, it supports a wide range of C ++ features.

Boost Python documentation, I recommend: http://www.maycode.com/boostdoc/boost-doc/libs/python/doc /. Basically, you can find the answer to your questions.

The following is an example, which covers a wide range of areas and needs to be well understood. This example shows how to encapsulate C ++. In the next section, I will write some advanced usage.

  1. # Include <boost/python. HPP>
  2. # Include <boost/Python/module. HPP>
  3. # Include <boost/Python/DEF. HPP>
  4. # Include <boost/Python/to_python_converter.hpp>
  5. # Include
  6. Using namespace STD;
  7. Using namespace boost: Python;
  8. Namespace hellopython {
  9. // Simple functions
  10. Char const * sayhello (){
  11. Return "hello from boost: Python ";
  12. }
  13. // Simple class
  14. Class helloclass {
  15. Public:
  16. Helloclass (const string & name): Name (name ){
  17. }
  18. Public:
  19. String sayhello (){
  20. Return "hello from helloclass by:" + name;
  21. }
  22. PRIVATE:
  23. String name;
  24. };
  25. // Accept simple functions of this class
  26. String sayhelloclass (helloclass & Hello ){
  27. Return hello. sayhello () + "in function sayhelloclass ";
  28. }
  29. // STL container
  30. Typedef vector <int> ivector;
  31. // Functions with default parameter values
  32. Void showperson (string name, int age = 30, string nationality = "China "){
  33. Cout <name <"" <age <"" <nationality <Endl;
  34. }
  35. // Encapsulate functions with default parameter values
  36. Boost_python_function_overloads (showperson_overloads, showperson, 1, 3) // 1: Minimum number of parameters and maximum number of three parameters
  37. // Encapsulation Module
  38. Boost_python_module (hellopython ){
  39. // Encapsulate simple functions
  40. Def ("sayhello", sayhello );
  41. // Encapsulate simple classes and define the _ init _ Function
  42. Class _ ("helloclass", INIT ())
  43. . Def ("sayhello", & helloclass: sayhello) // Add a regular member function
  44. ;
  45. Def ("sayhelloclass", sayhelloclass); // sayhelloclass can be made a member of module !!!
  46. // Simple STL Encapsulation Method
  47. Class _ ("ivector ")
  48. . Def (vector_indexing_suite ());
  49. Class _> ("ivector_vector ")
  50. . Def (vector_indexing_suite> ());
  51. // Encapsulation method with default parameter values
  52. Def ("showperson", showperson, showperson_overloads ());
  53. }

Series of articles:
Python calls C/C ++ functions (1)
Python calls C ++ (2) encapsulated by boost Python)
C ++ calls Python (3)
C ++ calls Python (4)
C ++ and Python advanced application interoperability (5)

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.