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

Niall Douglas s_sourceforge at nedprod.com
Sun Oct 12 03:22:17 CEST 2003


On 11 Oct 2003 at 19:45, Niall Douglas wrote:

Fixed it!

The below works lovely, and my thanks for the advice. The problem was 
the boost::noncopyable in the MyListCArray declaration.

Ok, next question: How do you determine the return type of a function 
pointer? It must be possible as bpl determines return type in def() - 
get_signature() from signature.hpp looked hopeful, but it appears to 
be a run-time not compile-time function.

To clarify:

std::string *foo();

typedef result_of<&foo>::value return_type;

.... and return_type is std::string *.

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; }
	int getDataLen() const { return 4; }
};

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

// ****** Where the CArray.h stuff begins ********

template<class T> class CArray
{
	// Sets subType::value to the type of the array's contents
	template<class sT> struct subType
	{
		typedef sT value;
	};
	template<class sT> struct subType<sT *>
	{
		typedef sT value;
	};
	//! The type the array contains
	typedef typename subType<T>::value memberType;
public:
	//! Type used to access array (and sets its upper and lower members)
	typedef boost::python::indexing::iterator_pair<memberType *> 
mTypeIt;
	//! Returns an appropriately wrapped array
	typename CArray<T>::mTypeIt getArray(MyList &l)
	{
		CArray<T>::memberType *data=l.getData();
		return CArray<T>::mTypeIt(data, data+1000); // Issue if l is near 
the 4Gb mark
	}
};



BOOST_PYTHON_MODULE(TestCArrays)
{
	boost::python::class_< CArray<std::string *>::mTypeIt 
>("CArray_string", 
		boost::python::init<std::string *, std::string *>())
		.def(boost::python::indexing::container_suite< CArray<std::string 
*>::mTypeIt >());

    boost::python::class_< MyList, boost::noncopyable >("MyList")
		.def("getData", &getArray<std::string *>)
		;

}




-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 208 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20031012/0a91e0df/attachment.pgp>


More information about the Cplusplus-sig mailing list