#pragma implementation "python.h" #include "python.h" namespace python { template MapBase::MapBase (PyObject *pyob, bool owned): Object(pyob, owned) { validate(); } template MapBase::MapBase (const MapBase& ob): Object(ob) { validate(); } // Assignment acquires new ownership of pointer template MapBase& MapBase::operator= (const Object& rhs) { return (*this = *rhs); } template MapBase& MapBase::operator= (PyObject* rhsp) { if(ptr() == rhsp) return *this; set (rhsp); return *this; } // Membership template bool MapBase::accepts (PyObject *pyob) const { return pyob && PyMapping_Check(pyob); } // Clear -- PyMapping Clear is missing // template void MapBase::clear () { List k = keys(); for(List::iterator i = k.begin(); i != k.end(); i++) { del_item(*i); } } // Element Access template T MapBase::operator[](const std::string& key) const { return get_item(key); } template T MapBase::operator[](const Object& key) const { return get_item(key); } template mapref MapBase::operator[](const std::string& key) { return mapref(*this, key); } template mapref MapBase::operator[](const Object& key) { return mapref(*this, key); } template int MapBase::length () const { return PyMapping_Length (ptr()); } template int MapBase::has_key (const std::string& s) const { return PyMapping_HasKeyString (ptr(),const_cast(s.c_str())); } template int MapBase::has_key (const Object& s) const { return PyMapping_HasKey (ptr(), s.ptr()); } template T MapBase::get_item (const std::string& s) const { PyObject * tmp = (PyMapping_GetItemString (ptr(),const_cast(s.c_str()))); if ( tmp == NULL ) { throw KeyError(s + " does not exist in " + this->as_string()); } return T( asObject(tmp)); } template T MapBase::get_item (const Object& s) const { return T( asObject(PyObject_GetItem (ptr(), s.ptr()))); } template void MapBase::set_item (const std::string& s, const Object& ob) { if (PyMapping_SetItemString (ptr(), const_cast(s.c_str()), *ob) == -1) { throw Exception(); } } template void MapBase::set_item (const Object& s, const Object& ob) { if (PyObject_SetItem (ptr(), s.ptr(), ob.ptr()) == -1) { throw Exception(); } } template void MapBase::del_item (const std::string& s) { if (PyMapping_DelItemString (ptr(), const_cast(s.c_str())) == -1){ throw Exception(); } } template void MapBase::del_item (const Object& s) { if (PyMapping_DelItem (ptr(), *s) == -1){ throw Exception(); } } // Queries template List MapBase::keys () const { return List(PyMapping_Keys(ptr()), true); } template List MapBase::values () const { // each returned item is a (key, value) pair return List(PyMapping_Values(ptr()), true); } template List MapBase::items () const { return List(PyMapping_Items(ptr()), true); } // create an instance of MapBase namespace { Mapping mapping; } }