Equivalent of custodian_and_ward?

Asked by nbecker

I'm wrapping some boost::random stuff. Previously, I used boost::python, and needed custodian_and_ward.

example:
 class_<boost_uniform_int_wrap>
    ("uniform_int", "Uniform integer distribution", python::init<rng_t&,int,int>((
        python::arg ("rng"),
        python::arg ("min"),
        python::arg ("max")
        ),
       "__init__(r,min,max)\n"
       "Construct a uniform int variate generator U[min...max)\n\n"
       ":Parameters:\n"
       " `r` : rng\n"
       " Mersenne Twister\n"
       " `min` : int\n"
       " min\n"
       " `max` : int\n"
       " max\n"
       )
     [python::with_custodian_and_ward<1,2>()])
...

struct boost_uniform_int_wrap {
  boost::variate_generator<rng_t&, boost::uniform_int<> > gen;

  boost_uniform_int_wrap (rng_t& r, int min, int max) :
    gen (r, boost::uniform_int<> (min, max)) {}

  int Generate1 () { return gen(); }

  ublas::vector<int> Generate (size_t n) {
    ublas::vector<int> out (n);
    for (ublas::vector<int>::iterator i = out.begin(); i != out.end(); i++)
      *i = Generate1();
    return out;
  }
};

The "uniform_int" object holds a reference to the rng_t object, so the rng_t object must not be deleted while uniform_int is alive.

How to do this with pybindgen?

Question information

Language:
English Edit question
Status:
Answered
For:
PyBindGen Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Gustavo Carneiro (gjc) said :
#1

C++ class pointer parameters/return values can have a 'custodian' parameter. See [1] and [2]. Some sample code can be extracted from the unit tests (grep for 'custodian' in tests/foomodulegen.py).

[1] http://pybindgen.googlecode.com/svn/trunk/apidocs/pybindgen.cppclass_typehandlers.CppClassPtrParameter-class.html#__init__

[2] http://pybindgen.googlecode.com/svn/trunk/apidocs/index.html

Keep in mind, however, that custodian/ward memory management has received somewhat limited testing so far (unlike reference counting which I use and test regularly).

Revision history for this message
Gustavo Carneiro (gjc) said :
#2

Can you help with this problem?

Provide an answer of your own, or ask nbecker for more information if necessary.

To post a message you must log in.