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

Nikolay Mladenov nickm at sitius.com
Mon Apr 26 22:52:43 CEST 2004



Tony Djordjevski wrote:
> 
> 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)

You have to do this instead:

         .add_property("parent"
			, make_function(&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

-- 
Nikolay Mladenov
Sitius Automation Inc.
www.sitius.com





More information about the Cplusplus-sig mailing list