Differences between revisions 1 and 2
Revision 1 as of 2002-09-25 19:23:59
Size: 1193
Editor: MikeRovner
Comment:
Revision 2 as of 2002-09-26 00:58:07
Size: 1089
Editor: MikeRovner
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:
            )

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.

boost.python/FAQ (last edited 2010-10-26 02:02:04 by c-67-188-38-165)

Unable to edit the page? See the FrontPage for instructions.