diff --git a/SWIG/Examples/python/check.list b/SWIG/Examples/python/check.list index add3a8a6d..c6a2932e3 100644 --- a/SWIG/Examples/python/check.list +++ b/SWIG/Examples/python/check.list @@ -23,6 +23,7 @@ shadow simple smartptr std_vector +std_map swigrun template varargs diff --git a/SWIG/Examples/python/std_map/.cvsignore b/SWIG/Examples/python/std_map/.cvsignore new file mode 100644 index 000000000..d111f30e8 --- /dev/null +++ b/SWIG/Examples/python/std_map/.cvsignore @@ -0,0 +1,13 @@ +example.py +example.pyc +*_wrap.c +*_wrap.cxx +*.dll +*.dsw +*.exp +*.lib +*.ncb +*.opt +*.plg +Release +Debug diff --git a/SWIG/Examples/python/std_map/Makefile b/SWIG/Examples/python/std_map/Makefile new file mode 100644 index 000000000..2d4c1b4a3 --- /dev/null +++ b/SWIG/Examples/python/std_map/Makefile @@ -0,0 +1,24 @@ +TOP = ../.. +SWIG = $(TOP)/../preinst-swig +CXXSRCS = +TARGET = example +INTERFACE = example.i +LIBS = -lm +SWIGOPT = + +all:: + $(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \ + SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' python_cpp + +static:: + $(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \ + SWIGOPT='$(SWIGOPT)' TARGET='mypython' INTERFACE='$(INTERFACE)' python_cpp_static + +clean:: + $(MAKE) -f $(TOP)/Makefile python_clean + rm -f $(TARGET).py + +run: + python runme.py + +check: all diff --git a/SWIG/Examples/python/std_map/example.h b/SWIG/Examples/python/std_map/example.h new file mode 100644 index 000000000..79ccc6478 --- /dev/null +++ b/SWIG/Examples/python/std_map/example.h @@ -0,0 +1,17 @@ +/* File : example.h */ + +#include +#include + +template +std::map half_map(const std::map& v) { + typedef typename std::map::const_iterator iter; + std::map w; + for (iter i = v.begin(); i != v.end(); ++i) { + w[i->first] = (i->second)/2; + } + return w; +} + + + diff --git a/SWIG/Examples/python/std_map/example.i b/SWIG/Examples/python/std_map/example.i new file mode 100644 index 000000000..eb5794a57 --- /dev/null +++ b/SWIG/Examples/python/std_map/example.i @@ -0,0 +1,26 @@ +/* File : example.i */ +%module example + +%{ +#include "example.h" +%} + +%include std_string.i +%include std_pair.i +%include std_map.i + +/* instantiate the required template specializations */ +namespace std { + /* remember to instantiate the key,value pair! */ + %template() pair; + %template() pair; + %template(DoubleMap) map; + %template() map; +} + +/* Let's just grab the original header file here */ +%include "example.h" + +%template(halfd) half_map; +%template(halfi) half_map; + diff --git a/SWIG/Examples/python/std_map/runme.py b/SWIG/Examples/python/std_map/runme.py new file mode 100644 index 000000000..1a7e52524 --- /dev/null +++ b/SWIG/Examples/python/std_map/runme.py @@ -0,0 +1,36 @@ +# file: runme.py + +import example + +dmap = {} +dmap["hello"] = 1.0 +dmap["hi"] = 2.0 + +print dmap.items() +print dmap.keys() +print dmap.values() + +print dmap +print example.halfd(dmap) + +dmap = example.DoubleMap() +dmap["hello"] = 1.0 +dmap["hi"] = 2.0 + +print dmap.items() +print dmap.keys() +print dmap.values() + +hmap = example.halfd(dmap) +print hmap.keys() +print hmap.values() + + +dmap = {} +dmap["hello"] = 2 +dmap["hi"] = 4 + +hmap = example.halfi(dmap) +print hmap +print hmap.keys() +print hmap.values()