make_constructor with custodian _and_ward solution?

André Anjos <[email protected]>
Newsgroups gmane.comp.lib.boost.langbinding
Message-ID <[email protected]>
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) 
complains:

/afs/cern.ch/sw/lcg/external/Boost/1.33.1/slc4_ia32_gcc34/include/
boost-1_33_1/boost/python/with_custodian_and_ward.hpp:25:
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 {
  public:
    Holder(int x): my_x() { my_x[0] = x; /*...*/ my_x[4] = x; }
    const int* get_c_array() const { return my_x; }
  private:
    int my_x[5];
};

class Pointer {
  public:
    Pointer (const int* ref): cannot_copy(ref) {}
    //... methods to access the contents of "cannot_copy"
  private:
    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()));
}

BOOST_PYTHON_MODULE(test)
{
  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.

Andre


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.