Jump to content
  • Advertisement
Sign in to follow this  

Wrapping std::map using Boost::Python

This topic is 4088 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I'm trying to wrap std::map using boost::python so my embedded python code can access maps. I have the following wrapper class:
//Map - takes e.g std::map<std::string,thing*> as MapType
template<class MapType>
struct MapItem
	//Typedefine the key and value types for ease
	typedef typename MapType::key_type KeyType;
	typedef typename MapType::mapped_type ValueType;
	static ValueType& Get(MapType & x,KeyType const& i)
		if( x.find(i) != x.end() ) 
			return x;
		//Some kind of error!
	static void Set(MapType & x,KeyType const& i,ValueType const& v)
		x=v; // use map autocreation feature
	static void Del(MapType & x,KeyType const& i)
		if( x.find(i) != x.end() ) 


And the following Boost::Python class definition to wrap a particular map:
boost::python::class_<std::map<std::string,SubGrid> >("SubGridMap")
	.def("__getitem__",&MapItem<std::map<std::string,SubGrid> >::Get,boost::python::return_value_policy<boost::python::copy_non_const_reference>())
	.def("__setitem__",&MapItem<std::map<std::string,SubGrid> >::Set,boost::python::with_custodian_and_ward<1,2>()) // to let container keep value
	.def("__delitem__",&MapItem<std::map<std::string,SubGrid> >::Del)

I really need iterator support and following the Python Wiki entry, have written a function like this to convert the std::map to a std::list of tuples as boost::python::iterator doesn't seem to be compatible with std::map (which seems a bit icky but makes sense):
static std::list<boost::tuple<KeyType,ValueType> > Items(MapType const& x)
	std::list<boost::tuple<KeyType,ValueType> > t;
	typename MapType::const_iterator it;
	for(it=x.begin(); it!=x.end(); ++it)
	return t;

But the wiki entry irritatingly stops short of telling you how the hell you wrap it into an __iter__ and the attachement to the wiki entry seems to be empty...Has anyone done this or have any ideas? Cheers Jamie

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!