[C++-sig] Class properties with policies?

Tony Djordjevski tony at v-sim.com
Mon Apr 26 22:25:00 CEST 2004


Hi everyone,

I'm fairly new to Boost.Python and I'm trying to set a return value policy
for a class property.  I'm using VC7.1 with Boost 1.31.0.

A simplified example of what I'm trying to do is:

// Node class definition
class Node
{
  private:
    Node *parent;

  public:
    Node();
    ~Node();

    Node* getParent();
    void setParent(Node *newParent);
};

// The following works fine, but not what I'm looking for
BOOST_PYTHON_MODULE(MyModule)
{
    class_< Node >("Node", init<  >())
        .def(init< const Node& >())
        .def("getParent", &Node::getParent,
            return_value_policy< reference_existing_object >())
        .def("setParent", &Node::setParent)
    ;
}

What I'd really like to do is something like this:

BOOST_PYTHON_MODULE(MyModule)
{
    class_< Node >("Node", init<  >())
        .def(init< const Node& >())
        .add_property("parent", &Node::getParent,
            return_value_policy< reference_existing_object >(),
            &Node::setParent)
    ;
}

Is something like this possible?  The compiler complains that I'm passing in
the wrong amount of arguments and after looking at the source for class.hpp,
I'm led to believe that it's currently not possible.  So I guess my question
really is "Is there an easy way around this?".

On a side note:  I realize that Pyste doesn't support the add_property
feature.  I don't have a lot of time, but I'd like to take a crack at adding
it.  If someone could point me in the right direction, I'd appreciate it.

Thanks,
Tony Djordjevski





More information about the Cplusplus-sig mailing list