RE: [C++-sig] def_readonly and const array members
I am using Boost 1.30. Sorry I forgot to mention that. def_readonly seems to still in 1.30 and its implementation is: template <class D, class B> self& def_readonly(char const* name, D B::*pm_) { D T::*pm = pm_; this->add_property(name, make_getter(pm)); return *this; } Is it deprecated though? However, I tried add_property with make_getter (which I usually use anyway) and I get the same error. Here is my new class_ code: class_<testStruct>("testStruct", init<>()) .add_property("theArray", make_getter(&testStruct::theArray, return_value_policy<return_by_value>())); Thank you for the help, Jeff Brewer jeff@brewer.com -----Original Message----- From: Ralf W. Grosse-Kunstleve [mailto:rwgk@yahoo.com] Posted At: Thursday, July 24, 2003 6:06 PM Posted To: c++-sig Conversation: [C++-sig] def_readonly and const array members Subject: Re: [C++-sig] def_readonly and const array members --- Jeff Brewer <jeff@brewer.com> wrote:
I am trying to wrap structs that have character arrays and I'm having problems since I moved from MSVC 7 to MSVC 7.1 (VS .NET 2003). Here is the struct I'm trying to expose the theArray member of using class_::def_readonly:
Are you also moving to a more recent boost?
.def_readonly("theArray", &testStruct::theArray);
Try this instead: #include <boost/python/return_value_policy.hpp> #include <boost/python/return_by_value.hpp> .add_property("theArray", make_getter(&testStruct::theArray, return_value_policy<return_by_value>())); If you want to support both Boost 1.29 and never versions: #include <boost/version.hpp> #if BOOST_VERSION >= 103000 // use add_property #else // use def_readonly or def_readwrite #endif Ralf __________________________________ Do you Yahoo!? Yahoo! SiteBuilder - Free, easy-to-use web site design software http://sitebuilder.yahoo.com _______________________________________________ C++-sig mailing list C++-sig@python.org http://mail.python.org/mailman/listinfo/c++-sig
"Jeff Brewer" <jeff@brewer.com> writes:
I am using Boost 1.30. Sorry I forgot to mention that.
def_readonly seems to still in 1.30 and its implementation is: template <class D, class B> self& def_readonly(char const* name, D B::*pm_) { D T::*pm = pm_; this->add_property(name, make_getter(pm)); return *this; } Is it deprecated though?
Nope. I don't have time to figure out why this isn't working for you right now, but...
However, I tried add_property with make_getter (which I usually use anyway) and I get the same error. Here is my new class_ code:
class_<testStruct>("testStruct", init<>()) .add_property("theArray", make_getter(&testStruct::theArray, return_value_policy<return_by_value>()));
Why not skip the to_python_converter, and instead build a "get" function for a property, something like: template <class D, unsigned N, class C, D (C::*pm)[N]> get_array { static tuple execute(C& c) { list l; for (unsigned i = 0; i < N; ++i) l.append((c.*pm)[N]); return tuple(l); } }; class_<testStruct>("testStruct", init<>()) .add_property( "theArray" , &get_array< unsigned char , 16 , testStruct , &testStruct::theArray >::execute ); -- Dave Abrahams Boost Consulting www.boost-consulting.com
--- David Abrahams <dave@boost-consulting.com> wrote:
"Jeff Brewer" <jeff@brewer.com> writes:
I am using Boost 1.30. Sorry I forgot to mention that.
def_readonly seems to still in 1.30 and its implementation is: template <class D, class B> self& def_readonly(char const* name, D B::*pm_) { D T::*pm = pm_; this->add_property(name, make_getter(pm)); return *this; } Is it deprecated though?
Nope. I don't have time to figure out why this isn't working for you right now, but...
If David cannot figure it out, maybe it is time to admit that I am lost, too. I am doing all the time what you are trying to do, under many platforms including Visual C++ 7.1. However, I've never tried it with char arrays. What happens if you change your small test to work with, say, int instead of char? I am not sure it will help, but here is my "to_tuple" converter: template <typename ContainerType> struct to_tuple { static PyObject* convert(ContainerType const& a) { boost::python::list result; for(std::size_t i=0;i<a.size();i++) { result.append(boost::python::object(a[i])); } return boost::python::incref(boost::python::tuple(result).ptr()); } }; Your's looks good, too, but maybe there is a subtlety that VC chokes on? I also noticed that your test involves char array[16], but you quote an error message related to char array[8]? Where does that factor of two come from? OK, enough shots in the blue for now. Hope this is useful somehow. Ralf __________________________________ Do you Yahoo!? Yahoo! SiteBuilder - Free, easy-to-use web site design software http://sitebuilder.yahoo.com
participants (3)
-
David Abrahams -
Jeff Brewer -
Ralf W. Grosse-Kunstleve