PageRenderTime 24ms CodeModel.GetById 22ms app.highlight 1ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/rel-1-3-26/SWIG/Lib/python/std_map.i

#
Swig | 167 lines | 148 code | 16 blank | 3 comment | 0 complexity | b4510e8ab5f5732d0d4379a0d0070ecf MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1/*
  2  Maps
  3*/
  4
  5%fragment("StdMapTraits","header",fragment="StdSequenceTraits")
  6{
  7  namespace swig {
  8    template <class PySeq, class K, class T >
  9    inline void
 10    assign(const PySeq& pyseq, std::map<K,T > *map) {
 11      typedef typename std::map<K,T>::value_type value_type;
 12      typename PySeq::const_iterator it = pyseq.begin();
 13      for (;it != pyseq.end(); ++it) {
 14	map->insert(value_type(it->first, it->second));
 15      }
 16    }
 17
 18    template <class K, class T>
 19    struct traits_asptr<std::map<K,T> >  {
 20      typedef std::map<K,T> map_type;
 21      static int asptr(PyObject *obj, map_type **val) {
 22	if (PyDict_Check(obj)) {
 23	  PyObject_var items = PyMapping_Items(obj);
 24	  return traits_asptr_stdseq<std::map<K,T>, std::pair<K, T> >
 25	    ::asptr(items, val);
 26	}
 27	if (val) {
 28	  PyErr_SetString(PyExc_TypeError, "a dictionary is expected");
 29	}
 30	return 0;
 31      }      
 32    };
 33      
 34    template <class K, class T >
 35    struct traits_from<std::map<K,T> >  {
 36      typedef std::map<K,T> map_type;
 37      typedef typename map_type::const_iterator const_iterator;
 38      typedef typename map_type::size_type size_type;
 39            
 40      static PyObject *from(const map_type& map) {
 41	size_type size = map.size();
 42	int pysize = (size <= (size_type) INT_MAX) ? (int) size : -1;
 43	if (pysize < 0) {
 44	  PyErr_SetString(PyExc_OverflowError,
 45			  "map size not valid in python");
 46	  return NULL;
 47	}
 48	PyObject *obj = PyDict_New();
 49	for (const_iterator i= map.begin(); i!= map.end(); ++i) {
 50	  PyDict_SetItem(obj,
 51			 swig::from(i->first),
 52			 swig::from(i->second));
 53	}
 54	return obj;
 55      }
 56    };
 57  }
 58}
 59
 60%define %swig_map_methods(Map...)
 61  %swig_container_methods(Map)
 62
 63  %extend {
 64    mapped_type __getitem__(const key_type& key) const throw (std::out_of_range) {
 65      Map::const_iterator i = self->find(key);
 66      if (i != self->end())
 67	return i->second;
 68      else
 69	throw std::out_of_range("key not found");
 70    }
 71    
 72    void __setitem__(const key_type& key, const mapped_type& x) throw (std::out_of_range) {
 73      self->insert(Map::value_type(key,x));
 74    }
 75    
 76    void __delitem__(const key_type& key) throw (std::out_of_range) {
 77      Map::iterator i = self->find(key);
 78      if (i != self->end())
 79	self->erase(i);
 80      else
 81	throw std::out_of_range("key not found");
 82    }
 83    
 84    bool has_key(const key_type& key) const {
 85      Map::const_iterator i = self->find(key);
 86      return i != self->end();
 87    }
 88    
 89    PyObject* keys() {
 90      Map::size_type size = self->size();
 91      int pysize = (size <= (Map::size_type) INT_MAX) ? (int) size : -1;
 92      if (pysize < 0) {
 93	PyErr_SetString(PyExc_OverflowError,
 94			"map size not valid in python");
 95	return NULL;
 96      }
 97      PyObject* keyList = PyList_New(pysize);
 98      Map::const_iterator i = self->begin();
 99      for (int j = 0; j < pysize; ++i, ++j) {
100	PyList_SetItem(keyList, j, swig::from(i->first));
101      }
102      return keyList;
103    }
104    
105    PyObject* values() {
106      Map::size_type size = self->size();
107      int pysize = (size <= (Map::size_type) INT_MAX) ? (int) size : -1;
108      if (pysize < 0) {
109	PyErr_SetString(PyExc_OverflowError,
110			"map size not valid in python");
111	return NULL;
112      }
113      PyObject* valList = PyTuple_New(pysize);
114      Map::const_iterator i = self->begin();
115      for (int j = 0; j < pysize; ++i, ++j) {
116	PyTuple_SetItem(valList, j, swig::from(i->second));
117      }
118      return valList;
119    }
120    
121    PyObject* items() {
122      Map::size_type size = self->size();
123      int pysize = (size <= (Map::size_type) INT_MAX) ? (int) size : -1;
124      if (pysize < 0) {
125	PyErr_SetString(PyExc_OverflowError,
126			"map size not valid in python");
127	return NULL;
128      }    
129      PyObject* itemList = PyTuple_New(pysize);
130      Map::const_iterator i = self->begin();
131      for (int j = 0; j < pysize; ++i, ++j) {
132	PyTuple_SetItem(itemList, j, swig::from(*i));
133      }
134      return itemList;
135    }
136    
137    // Python 2.2 methods
138    bool __contains__(const key_type& key) {
139      return self->find(key) != self->end();
140    }
141
142    PyObject* __iter__() {
143      Map::size_type size = self->size();
144      int pysize = (size <= (Map::size_type) INT_MAX) ? (int) size : -1;
145      if (pysize < 0) {
146	PyErr_SetString(PyExc_OverflowError,
147			"map size not valid in python");
148	return NULL;
149      }
150      PyObject* keyTuple = PyTuple_New(pysize);
151      Map::const_iterator i = self->begin();
152      for (int j = 0; j < pysize; ++i, ++j) {
153	PyTuple_SetItem(keyTuple, j, swig::from(i->first));
154      }
155%#if PY_VERSION_HEX >= 0x02020000
156      PyObject* iter = PyObject_GetIter(keyTuple);
157      Py_DECREF(keyTuple);
158      return iter;
159%#else
160      return keyTuple;
161%#endif
162    }
163  }
164%enddef
165
166
167%include <std/std_map.i>