make_constructor with custodian_and_ward solution?

Previous Topic Next Topic
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

make_constructor with custodian_and_ward solution?

André Anjos
Hi, I'm trying to inject constructors to an existing class (using Boost.Python),
to which I cannot modify the source. The basic Idea is reproduced in the example
bellow. In the way it is, it compiles. If the comments on the lines with the
"custodian_and_ward" are removed (any of them), gcc (3.4)/boost (1.33.1)

error: no matching function for call to `get(mpl_::int_<0>, const
boost::python::detail::offset_args<PyObject*, mpl_::int_<1> >&)'

Here is what I'm trying (in a simple example):

#include <boost/python.hpp>
#include <boost/shared_ptr.hpp>

using namespace boost::python;

class Holder {
    Holder(int x): my_x() { my_x[0] = x; /*...*/ my_x[4] = x; }
    const int* get_c_array() const { return my_x; }
    int my_x[5];

class Pointer {
    Pointer (const int* ref): cannot_copy(ref) {}
    //... methods to access the contents of "cannot_copy"
    const int* cannot_copy; //for a special reason I cannot copy this integer

//Make Foo to use data in Bar, want it to be the custodian in this case
boost::shared_ptr<Pointer> make_pointer(const Holder& h) {
  return boost::shared_ptr<Pointer>(new Pointer(h.get_c_array()));

  class_<Holder>("Holder", init<int>())

  class_<Pointer>("Pointer", no_init)
    //.def("__init__", make_constructor(&make_pointer,
with_custodian_and_ward_postcall<0, 1>())) //if comment out, gives error above
    //.def("__init__", make_constructor(&make_pointer,
with_custodian_and_ward<1, 2>())) //if comment out, gives error above
    .def("__init__", make_constructor(&make_pointer, default_call_policies()))

Always in the same point. Is this what I'm trying not possible? Many thanks
for any hint.


This email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
Boost-langbinding mailing list
[hidden email]