Size: 1193
Comment:
|
Size: 1415
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 21: | Line 21: |
boost::python::module("test_pointer_adoption_ext") .def("num_a_instances", num_a_instances) |
def("num_a_instances", num_a_instances); |
Line 26: | Line 25: |
.def("create", create, return_value_policy<manage_new_object>()) | def("create", create, return_value_policy<manage_new_object>()); |
Line 28: | Line 27: |
.add( class_<A>() |
class_<A>() |
Line 33: | Line 30: |
) | |
Line 38: | Line 34: |
2. | 2. ''What is the relation between this no_init and boost::noncopyable?'' The only relationship is that they both deal with constructors. * no_init means "do not try to define a Python __init__ function" * noncopyable means "do not try to register a converter which can convert C++ T return values to python". ---- |
From comp.python.c++ newsgroup:
1. The constructors of some classes I am trying to wrap are private because instances must be created by using a factory. Is it possible to wrap such classes using bpl v2?
Sure. Of course you can only create the instances using the factory...
If you look at libs/python/test/test_pointer_adoption.cpp you'll see the factory function "create" is being used to generate new instances of class A. It uses return_value_policy<manage_new_object> to instruct Python to adopt the A* to hold the instance.
A* create(std::string const& s) { return new A(s); } BOOST_PYTHON_MODULE_INIT(test_pointer_adoption_ext) { def("num_a_instances", num_a_instances); // Specify the manage_new_object return policy to take // ownership of create's result def("create", create, return_value_policy<manage_new_object>()); class_<A>() .def("content", &A::content) .def("get_inner", &A::get_inner, return_internal_reference<>()) ; }
2. What is the relation between this no_init and boost::noncopyable?
The only relationship is that they both deal with constructors.
no_init means "do not try to define a Python init function"
- noncopyable means "do not try to register a converter which can
- convert C++ T return values to python".