Hi,<br><br>I&#39;m trying to wrap std::map using boost::python so my embedded python code can access maps.&nbsp; I have the following wrapper class:<br><br>//Map - takes e.g std::map&lt;std::string,thing*&gt; as MapType<br>template&lt;class MapType&gt;
<br>struct MapItem<br>{<br>&nbsp;&nbsp; &nbsp;//Typedefine the key and value types for ease<br>&nbsp;&nbsp; &nbsp;typedef typename MapType::key_type KeyType;<br>&nbsp;&nbsp; &nbsp;typedef typename MapType::mapped_type ValueType;<br>&nbsp;&nbsp; &nbsp;<br>&nbsp;&nbsp; &nbsp;static ValueType&amp; Get(MapType &amp; x,KeyType const&amp; i)
<br>&nbsp;&nbsp; &nbsp;{<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if( x.find(i) != x.end() ) <br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;return x[i];<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;//Some kind of error!<br>&nbsp;&nbsp; &nbsp;}<br>&nbsp;&nbsp; &nbsp;static void Set(MapType &amp; x,KeyType const&amp; i,ValueType const&amp; v)<br>&nbsp;&nbsp; &nbsp;{<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;x[i]=v; // use map autocreation feature
<br>&nbsp;&nbsp; &nbsp;}<br>&nbsp;&nbsp; &nbsp;static void Del(MapType &amp; x,KeyType const&amp; i)<br>&nbsp;&nbsp; &nbsp;{<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if( x.find(i) != x.end() ) <br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;x.erase(i);<br><br>&nbsp;&nbsp; &nbsp;}<br>}<br><br>And the following Boost::Python class definition to wrap a particular map:
<br><br>[source lang=&quot;cpp&quot;]<br>boost::python::class_&lt;std::map&lt;std::string,SubGrid&gt; &gt;(&quot;SubGridMap&quot;)<br>&nbsp;&nbsp; &nbsp;.def(&quot;__len__&quot;,&amp;std::map&lt;std::string,SubGrid&gt;::size)<br>&nbsp;&nbsp; &nbsp;.def(&quot;clear&quot;,&amp;std::map&lt;std::string,SubGrid&gt;::clear)
<br>&nbsp;&nbsp; &nbsp;.def(&quot;__getitem__&quot;,&amp;MapItem&lt;std::map&lt;std::string,SubGrid&gt; &gt;::Get,boost::python::return_value_policy&lt;boost::python::copy_non_const_reference&gt;())<br>&nbsp;&nbsp; &nbsp;.def(&quot;__setitem__&quot;,&amp;MapItem&lt;std::map&lt;std::string,SubGrid&gt; &gt;::Set,boost::python::with_custodian_and_ward&lt;1,2&gt;()) // to let container keep value
<br>&nbsp;&nbsp; &nbsp;.def(&quot;__delitem__&quot;,&amp;MapItem&lt;std::map&lt;std::string,SubGrid&gt; &gt;::Del)<br>;<br><br>I really need iterator support and following the Python Wiki entry on STL containers, have written a function like this to convert the std::map to a std::list of tuples as boost::python::iterator doesn&#39;t seem to be compatible with std::map (which seems a bit icky but makes sense):
<br><br>static std::list&lt;boost::tuple&lt;KeyType,ValueType&gt; &gt; Items(MapType const&amp; x)<br>{<br>&nbsp;&nbsp; &nbsp;std::list&lt;boost::tuple&lt;KeyType,ValueType&gt; &gt; t;<br>&nbsp;&nbsp; &nbsp;typename MapType::const_iterator it;<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;
<br>&nbsp;&nbsp; &nbsp;for(it=x.begin(); it!=x.end(); ++it)<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;t.push_back(boost::make_tuple(it-&gt;first,it-&gt;second));<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;<br>&nbsp;&nbsp; &nbsp;return t;<br>}<br><br><br>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?
<br><br>Cheers<br><br>Jamie