#pragma implementation "python.h"
#include "python.h"

namespace python {
   template<class T>
      MapBase<T>::MapBase (PyObject *pyob, bool owned): Object(pyob, owned) {
         validate();
      }

    template<class T>
      MapBase<T>::MapBase (const MapBase<T>& ob): Object(ob) {
         validate();
      }
   // Assignment acquires new ownership of pointer

    template<class T>
      MapBase<T>& MapBase<T>::operator= (const Object& rhs) {
         return (*this = *rhs);
      }

    template<class T>
      MapBase<T>& MapBase<T>::operator= (PyObject* rhsp) {
         if(ptr() == rhsp) return *this;
         set (rhsp);
         return *this;
      }
   // Membership
    template<class T>
      bool MapBase<T>::accepts (PyObject *pyob) const {
         return pyob && PyMapping_Check(pyob);
      }

   // Clear -- PyMapping Clear is missing
   //

    template<class T>
      void MapBase<T>::clear () {
         List k = keys();
         for(List::iterator i = k.begin(); i != k.end(); i++) {
            del_item(*i);
         }
      }      

   // Element Access
    template<class T>
      T MapBase<T>::operator[](const std::string& key) const {
         return get_item(key);
      }

    template<class T>
      T MapBase<T>::operator[](const Object& key) const {
         return get_item(key);
      }

    template<class T>
      mapref<T> MapBase<T>::operator[](const std::string& key) {
         return mapref<T>(*this, key);
      }

    template<class T>
      mapref<T> MapBase<T>::operator[](const Object& key) {
         return mapref<T>(*this, key);
      }

    template<class T>
      int MapBase<T>::length () const {
         return PyMapping_Length (ptr());
      }

    template<class T>
      int MapBase<T>::has_key (const std::string& s) const {
         return PyMapping_HasKeyString (ptr(),const_cast<char*>(s.c_str()));
      }

    template<class T>
      int MapBase<T>::has_key (const Object& s) const {
         return PyMapping_HasKey (ptr(), s.ptr());
      }

    template<class T>
      T MapBase<T>::get_item (const std::string& s) const {
         PyObject * tmp = (PyMapping_GetItemString (ptr(),const_cast<char*>(s.c_str())));
         if ( tmp == NULL ) { throw KeyError(s + " does not exist in " + this->as_string()); }
         return T( asObject(tmp));
      }

    template<class T>
      T MapBase<T>::get_item (const Object& s) const {
         return T( asObject(PyObject_GetItem (ptr(), s.ptr())));
      }

    template<class T>
      void MapBase<T>::set_item (const std::string& s, const Object& ob) {
         if (PyMapping_SetItemString (ptr(), const_cast<char*>(s.c_str()), *ob)
               == -1)
         {
            throw Exception();
         }
      }

    template<class T>
      void MapBase<T>::set_item (const Object& s, const Object& ob) {
         if (PyObject_SetItem (ptr(), s.ptr(), ob.ptr())
               == -1)
         {
            throw Exception();
         }
      }

    template<class T>
      void MapBase<T>::del_item (const std::string& s) {
         if (PyMapping_DelItemString (ptr(), const_cast<char*>(s.c_str())) == -1){
            throw Exception();
         }
      }

    template<class T>
      void MapBase<T>::del_item (const Object& s) {
         if (PyMapping_DelItem (ptr(), *s) == -1){
            throw Exception();
         }
      }
   // Queries
    template<class T>
      List MapBase<T>::keys () const {
         return List(PyMapping_Keys(ptr()), true);
      }

    template<class T>
      List MapBase<T>::values () const { // each returned item is a (key, value) pair
         return List(PyMapping_Values(ptr()), true);
      }

    template<class T>
      List MapBase<T>::items () const {
         return List(PyMapping_Items(ptr()), true);
      }	   

   // create an instance of MapBase<python::Object>
   namespace {
      Mapping mapping;
   }
}


syntax highlighted by Code2HTML, v. 0.9.1