// Copyright (C) 2002-2003 Gideon May (gideon@computer.org) // // Permission to copy, use, sell and distribute this software is granted // provided this copyright notice appears in all copies. // Permission to modify the code and to distribute modified code is granted // provided this copyright notice appears in all copies, and a notice // that the code was modified is included with the copyright notice. // // This software is provided "as is" without express or implied warranty, // and with no claim as to its suitability for any purpose. // #include #include #include #include #include #include #include #include #include #include #include "held_ptr.hpp" #include "Math.hpp" using namespace boost::python; template class TemplateArray { public : TemplateArray(const char * name) : _array(name, boost::python::no_init) { _array .def(init<>()) .def(init()) .def("getNumElements", &T::getNumElements) .def("__getitem__", &getitem) .def("__setitem__", &setitem) .def("resize", &resize) .def("__str__", &str) .def("__repr__", &repr) .def("set", &set) .def("reserve", &T::reserve) .def("push_back", &T::push_back) .def("size", &T::size) .def("max_size", &T::max_size) .def("capacity", &T::capacity) .def("empty", &T::empty) .def("__len__", &T::size) .def("__iter__", iterator()) ; } template void def(char const * name, F f) { _array.def(name, f); } static BT getitem(T * self, int idx) { if (self->size() <= (unsigned int) idx) { PyErr_SetString(PyExc_IndexError, "assignment index out of range"); throw_error_already_set(); } return (*self)[idx]; } static void setitem(T * self, int idx, BT val) { if (self->size() <= (unsigned int) idx) { PyErr_SetString(PyExc_IndexError, "list index out of range"); throw_error_already_set(); } else { (*self)[idx] = val; } } static std::string str(T * self) { std::ostringstream ost; ost << "("; for (unsigned int i =0 ; isize() ; i++) { ost << int((*self)[i]); if (i != self->size() -1) ost << ","; } ost << ")"; return ost.str(); } static std::string repr(T * self) { std::ostringstream ost; ost << self->className() << "("; for (unsigned int i =0 ; isize() ; i++) { ost << int((*self)[i]); if (i != self->size() -1) ost << ","; } ost << ")"; return ost.str(); } static void set(T * self, tuple elems) { self->clear(); int elcount = len(elems); if (elcount == 0) return; self->resize(elcount); for (int i=0; i(elems[i]); } } static void resize(T * self, int size) { self->resize(size); } private: class_, bases, boost::noncopyable> _array; };