C ++ and Python advanced application interoperability (5)

Source: Internet
Author: User

To sum up the problems I encountered in the actual process, please read the python tutorial and Python FAQ first.

1. If the encapsulated C ++What if the class does not copy the constructor?
When defining the class, add the template parameter boost: noncopyable and specify no_init

  1. Class _ <expandemitter, boost: noncopyable> ("expandemitter", no_init );

The purpose of the copy structure is to re-construct an object instance used in Python through the copy structure when the C ++ object instance is passed to Python. Generally, if there is no copy structure, the boost cmd_ptr is required to pass the shared pointer.

2. How do encapsulated C ++ functions return pointers or references?
Return_value_policy <return_by_reference> (). Set the return value policy to return_by_reference.

  1. . Def ("getpostdata", & request: getpostdata, return_value_policy <return_by_reference> ())

3. How can custom strings be automatically converted to Python?
Http://www.maycode.com/boostdoc/boost-doc/libs/python/doc/v2/faq.html#custom_string

 

Struct template_string_to_python_str <br/>{< br/> static pyobject * convert (templatestring const & S) <br/>{< br/> return boost: Python :: incref (boost: Python: Object (S. PTR ()). PTR (); <br/>}< br/>}; </P> <p> struct template_string_from_python_str <br/>{< br/> template_string_from_python_str () <br/>{< br/> boost: Python: converter: Registry: push_back (<br/> & convertible, <br/> & construct, <br/> boost:: Python: type_id <templatestring> (); <br/>}</P> <p> static void * convertible (pyobject * obj_ptr) <br/>{< br/> If (! Pystring_check (obj_ptr) <br/>{< br/> return 0; <br/>}< br/> return obj_ptr; <br/>}</P> <p> static void construct (<br/> pyobject * obj_ptr, boost: Python: converter: rvalue_from_python_stage1_data * Data) <br/>{< br/> const char * value = pystring_asstring (obj_ptr); <br/> If (value = 0) <br/> boost: Python :: throw_error_already_set (); <br/> void * storage = (<br/> (boost: Python: converter: rvalue_from_python_storage <templatestring> *) <br/> data) -> storage. bytes; <br/> New (storage) templatestring (value); <br/> data-> convertible = storage; <br/>}< br/> }; </P> <p> boost_python_module (ctemplate) <br/>{< br/> boost: Python: to_python_converter <br/> templatestring, <br/> template_string_to_python_str> (); </P> <p> template_string_from_python_str (); <br/>}

4. Object release Problems
How can I release an object created in C ++ in Python? Or how to release an object created in Python in C ++?
In my opinion, create and release in one language. If you want to create an object in Python, you can call a C ++ function, such as newclass (), to create an object, return a pointer or reference, and use it, call deleteclass () of C ++ to release the SDK. Otherwise, the python reference count will be particularly troublesome and may easily cause problems such as memory leakage.

5. How to manually release the shared pointer?
Share pointer, which is automatically released by default, but sometimes we don't need to release it automatically. If you want to release it manually, you can define a release function. When creating share pointer, input the release function pointer.

  1. // Define
  2. Typedef boost: shared_ptr <world> world_ptr;
  3. // Define a release function
  4. Void deleteworld (World * w );
  5. // Share pointer, pass in release function pointer
  6. World_ptr worldobjectptr (New World, deleteworld );

6. C ++ encapsulation module, multiple files include, how can there be multiple definitions?

  1. Boost_python_module (Hello)
  2. {
  3. Class _ <world> ("world ")
  4. . Def ("Greet", & World: Greet)
  5. . Def ("set", & World: Set)
  6. ;
  7. Register_ptr_to_python <world_ptr> ();
  8. }

If the above section is in the. h file, multiple CPP file references won't work. At this time, you can define a header file as follows:

  1. Extern "C" Void inithello ();

First declare inithello () in the header file. After moving the above to CPP, boost_python_module will implement a function inithello.

7. How to encapsulate C ++ containers?
Header files under the boost/Python/suite/indexing directory

  1. // Include
  2. # Include <boost/Python/suite/indexing/map_indexing_suite.hpp>
  3. // You can define a map when defining a module.
  4. // String Map
  5. Boost: Python: Class _ <STD: Map <STD: String, STD: String> ("strmap ")
  6. . Def (map_indexing_suite <STD: Map <STD: String, STD: String> ())

I have studied it for a while and applied it in practical projects. I hope this series will be helpful to everyone.
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)

Related Article

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.