[C++-sig] IntWrapper.hpp in indexing_suite

Niall Douglas s_sourceforge at nedprod.com
Sat Oct 11 05:50:22 CEST 2003


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Firstly, let me confirm I have this right:

IntWrapper is converted into an int by bpl using the operator 
boost::shared_ptr<IntWrapper>() const yes? Or is it the 
implicitly_convertible? If it is the latter, why do we need the 
shared_ptr<> operator?

Anyway I came up with the below by basically following the 
testlinear.cpp example but unfortunately it complains "TypeError: No 
to_python (by-value) converter found for C++ type: class 
boost::python::indexing::iterator_pair<class CArrayMember<class 
std::basic_string<char,struct std::char_traits<char>,class 
std::allocator<char> > > *>". The code I used was:

>>> from TestCArrays import *
>>> a=MyList()
>>> b=a.getData()

I don't get why it's failing to find the conversion with mine whereas 
it works with the test :(

Any help much appreciated.

Cheers,
Niall

- --- cut ---
#include <boost/python.hpp>
#include <boost/python/suite/indexing/container_suite.hpp>
#include <boost/python/suite/indexing/iterator_pair.hpp>
#include <boost/python/suite/indexing/container_proxy.hpp>
#include <string>

class MyList
{
	std::string *array;
public:
	MyList();
	std::string *getData() { return array; }
};

MyList::MyList()
{
	static std::string data[]={ std::string("Niall"),
								std::string("is"),
								std::string("a"),
								std::string("teapot")
								};
	arrayÚta;
}

template<class T> class CArrayMember : public T
{
public:
	CArrayMember() : T() { }
	CArrayMember(const CArrayMember &o) : T(o) { }
	explicit CArrayMember(const T &o) : T(o) { }
	operator boost::shared_ptr<CArrayMember>() const
	{
		return boost::shared_ptr<CArrayMember>(new CArrayMember(*this));
	}
};

typedef 
boost::python::indexing::iterator_pair<CArrayMember<std::string> *> 
MyListCArray;

MyListCArray MyList_getArray(MyList &l)
{
	CArrayMember<std::string> 
*data=static_cast<CArrayMember<std::string> *>(l.getData());
	return 
boost::python::indexing::iterator_pair<CArrayMember<std::string> *>
		(data, data+100000); // Issue if l is near the 4Gb mark
}

BOOST_PYTHON_MODULE(TestCArrays)
{
	boost::python::implicitly_convertible<std::string, 
CArrayMember<std::string> >();

	boost::python::class_< MyListCArray, 
boost::noncopyable>("MyListCArray", 
		boost::python::init<CArrayMember<std::string> *, 
CArrayMember<std::string> *>())
		.def(boost::python::indexing::container_suite< MyListCArray >());

    boost::python::class_< MyList, boost::noncopyable >("MyList")
		.def("getData", &MyList_getArray)
		;
}





-----BEGIN PGP SIGNATURE-----
Version: idw's PGP-Frontend 4.9.6.1 / 9-2003 + PGP 8.0.2

iQA/AwUBP4d9/8EcvDLFGKbPEQLYxwCfSIKfJ6mtqNtS76pbNYxTF4OvZZUAoL+3
4O1w8HK0sgRIaFPbxZ5WXbfV
=d8TG
-----END PGP SIGNATURE-----




More information about the Cplusplus-sig mailing list