Boost Python properties/getter functions for strings

2007-03-19 Thread Shawn McGrath
Hi, I'm trying to expose a C++ class' internals to python via
boost::python.  I can do integer/boolean functions fine, but as soon
as I do a string get/set it craps out.

boost::python::class_ >("Entity")
//publics
.def("isActive", &Entity::isActive) //bool
.def("activate", &Entity::activate) //bool
.def("deactivate", &Entity::deactivate) //bool
//...
.add_property("name", &Entity::getName) //compile error 
(1)
.def("getName", &Entity::getName,
boost::python::return_internal_reference<>()); 
//runtime error(2)


Compile error (1) shows this: C:/MinGW/include/boost/python/detail/
invoke.hpp: In function `PyObject*
boost::python::detail::invoke(boost::python::detail::invoke_tag_<
false,  true>, const RC&, F&, TC&) [with RC =
boost::python::detail::specify_a_return_value_policy_to_wrap_functions_returning, F = const std::string&(rsblsb::Entity::*)() const, TC =
boost::python::arg_from_python]':
C:/MinGW/include/boost/python/detail/caller.hpp:199:   instantiated
from `PyObject* boost::python::detail::caller_arity<1u>::impl::operator()(PyObject*, PyObject*) [with F = const
std::string&(rsblsb::Entity::*)() const, Policies =
boost::python::default_call_policies, Sig = boost::mpl::vector2]'
C:/MinGW/include/boost/python/object/py_function.hpp:38:
instantiated from `PyObject*
boost::python::objects::caller_py_function_impl::operator()
(PyObject*, PyObject*) [with Caller =
boost::python::detail::caller >]'
C:\Game\svn\Platform\Framework\Python\PyModuleSetup.cc:58:
instantiated from here
C:/MinGW/include/boost/python/detail/invoke.hpp:88: error: no match
for call to `(const
boost::python::detail::specify_a_return_value_policy_to_wrap_functions_returning) (const std::basic_string,
std::allocator >&)'

Runtime error 2 just crashes whenever I try:
import modulename
I = modulename.Entity()
I.getName()

Anyone have any idea what I can try? thanks a lot!

-Shawn.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Boost Python properties/getter functions for strings

2007-03-19 Thread Shawn McGrath
I forgot to mention, getname is defined as:
const std::string &Entity::getName() const;

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Boost Python properties/getter functions for strings

2007-03-19 Thread Shawn McGrath
On Mar 19, 12:00 pm, "Shawn  McGrath" <[EMAIL PROTECTED]> wrote:
> I forgot to mention, getname is defined as:
> const std::string &Entity::getName() const;

After more reading I found the copy_const_reference, and replaced:
 boost::python::return_internal_reference<>());
with:
 
boost::python::return_value_policy());

and it fixed my problem.  Is there any downside to using
copy_const_reference over return_internal_reference?

Thanks,
Shawn.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Boost Python properties/getter functions for strings

2007-03-19 Thread Shawn McGrath
On Mar 19, 12:49 pm, "Jon Clements" <[EMAIL PROTECTED]> wrote:
> On 19 Mar, 16:40, "Shawn  McGrath" <[EMAIL PROTECTED]> wrote:
>
> > On Mar 19, 12:00 pm, "Shawn  McGrath" <[EMAIL PROTECTED]> wrote:
>
> > > I forgot to mention, getname is defined as:
> > > const std::string &Entity::getName() const;
>
> > After more reading I found the copy_const_reference, and replaced:
> >  boost::python::return_internal_reference<>());
> > with:
>
> > boost::python::return_value_policy());
>
> > and it fixed my problem.  Is there any downside to using
> > copy_const_reference over return_internal_reference?
>
> You might get some answers here; if not, can I 
> suggesthttp://mail.python.org/mailman/listinfo/c++-sig? I think a lot of the
> Boost.Python developers hang around on that list.
>
> hth,
>
> Jon.

Cool thanks a lot.

The problem is actually due to python's strings being immutable (I
knew this, but I thought returning const std::string& would do it).
return_internal_reference<> works for other pointers/references, just
not strings.

(I just answered it so if it gets searched later on people will find
the solution)

-Shawn.

-- 
http://mail.python.org/mailman/listinfo/python-list