X-Git-Url: http://woldlab.caltech.edu/gitweb/?p=mussa.git;a=blobdiff_plain;f=py%2Fstl_container_adapter.hpp;h=37f66fe64d56b7daabd11d7e12ccc1512f78dcfe;hp=87582476eec0ef9ed47c5d10eb6fc78dc9412cf6;hb=c417e847eccd0af3bf9dfed0bc252115bbbf3d83;hpb=4f83d2a3cb065e7219e83bd6d45ee11d0318facf diff --git a/py/stl_container_adapter.hpp b/py/stl_container_adapter.hpp index 8758247..37f66fe 100644 --- a/py/stl_container_adapter.hpp +++ b/py/stl_container_adapter.hpp @@ -2,73 +2,98 @@ #define STL_CONTAINER_ADAPTER_HPP_ #include +#include + #include // This template started off life at // http://wiki.python.org/moin/boost.python/StlContainers -void IndexError() { PyErr_SetString(PyExc_IndexError, "Index out of range"); } +struct IndexError : std::exception +{ + explicit IndexError(): std::exception() {}; +}; + +void translate_index_error(IndexError const &e); + + template struct std_item { - typedef typename T::value_type V; - static V& get(T& x, int i) - { - if( i<0 ) i+=x.size(); - if( i>=0 && i=0 && i=0 && i=0 && i=0 && i=0 && i=0 && i struct map_item { typedef typename T::key_type K; typedef typename T::mapped_type V; - static V& get(T const& x, K const& i) + static V& get(T & x, K const& i) { if( x.find(i) != x.end() ) return x[i]; - KeyError(); + throw KeyError(); } - static void set(T const& x, K const& i, V const& v) + static void set(T & x, K const& i, V const& v) { x[i]=v; // use map autocreation feature } - static void del(T const& x, K const& i) + static void del(T & x, K const& i) { if( x.find(i) != x.end() ) x.erase(i); - else KeyError(); + else throw KeyError(); } static bool in(T const& x, K const& i) { @@ -77,28 +102,28 @@ struct map_item static boost::python::list keys(T const& x) { boost::python::list t; - for(typename T::const_iterator it=x.begin; it!=x.end(); ++it) + for(typename T::const_iterator it=x.begin(); it!=x.end(); ++it) t.append(it->first); return t; } static boost::python::list values(T const& x) { boost::python::list t; - for(typename T::const_iterator it=x.begin; it!=x.end(); ++it) + for(typename T::const_iterator it=x.begin(); it!=x.end(); ++it) t.append(it->second); return t; } static boost::python::list items(T const& x) { boost::python::list t; - for(typename T::const_iterator it=x.begin; it!=x.end(); ++it) + for(typename T::const_iterator it=x.begin(); it!=x.end(); ++it) t.append(boost::python::make_tuple(it->first,it->second)); return t; } static int index(T const& x, K const& k) { int i=0; - for(typename T::const_iterator it=x.begin; it!=x.end(); ++it,++i) + for(typename T::const_iterator it=x.begin(); it!=x.end(); ++it,++i) if( it->first == k ) return i; return -1; }