[C++-sig] C arrays mk 2

Niall Douglas s_sourceforge at nedprod.com
Wed Oct 15 03:38:36 CEST 2003


This is for reference only to anyone else that might have this 
problem and searches the list archive in times to come. People who 
don't like macros beware!

My most gracious thanks to Raoul Gough for helping me in this. You're 
now in the acknowledgements for the library.

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 ********

#define DEFINE_MAKECARRAYITER(contType, memberType, getArrayFunction, 
getArrayLengthFunction) \
	boost::python::indexing::iterator_pair<memberType *> Make 
##contType## Iter(contType &c) \
{ \
	typedef boost::python::indexing::iterator_pair<memberType *> 
IterPair; \
	boost::python::class_<IterPair>( #contType "Indirect", \
		boost::python::init<memberType *, memberType *>()) \
		.def(boost::python::indexing::container_suite<IterPair>()); \
	memberType *data=c.##getArrayFunction##; \
	return IterPair(data, data+c.##getArrayLengthFunction##); \
}

DEFINE_MAKECARRAYITER(MyList, std::string, getData(), getDataLen() )

BOOST_PYTHON_MODULE(TestCArrays)
{
    boost::python::class_< MyList, boost::noncopyable >("MyList")
		.def("getData", &MakeMyListIter)
	;
}





-------------- 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/20031015/3efd0efc/attachment.pgp>


More information about the Cplusplus-sig mailing list