add std_map example
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@8291 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
165b0032e9
commit
532fa355ea
6 changed files with 117 additions and 0 deletions
|
|
@ -23,6 +23,7 @@ shadow
|
||||||
simple
|
simple
|
||||||
smartptr
|
smartptr
|
||||||
std_vector
|
std_vector
|
||||||
|
std_map
|
||||||
swigrun
|
swigrun
|
||||||
template
|
template
|
||||||
varargs
|
varargs
|
||||||
|
|
|
||||||
13
SWIG/Examples/python/std_map/.cvsignore
Normal file
13
SWIG/Examples/python/std_map/.cvsignore
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
example.py
|
||||||
|
example.pyc
|
||||||
|
*_wrap.c
|
||||||
|
*_wrap.cxx
|
||||||
|
*.dll
|
||||||
|
*.dsw
|
||||||
|
*.exp
|
||||||
|
*.lib
|
||||||
|
*.ncb
|
||||||
|
*.opt
|
||||||
|
*.plg
|
||||||
|
Release
|
||||||
|
Debug
|
||||||
24
SWIG/Examples/python/std_map/Makefile
Normal file
24
SWIG/Examples/python/std_map/Makefile
Normal file
|
|
@ -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
|
||||||
17
SWIG/Examples/python/std_map/example.h
Normal file
17
SWIG/Examples/python/std_map/example.h
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
/* File : example.h */
|
||||||
|
|
||||||
|
#include <map>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
template<class Key, class Value>
|
||||||
|
std::map<Key,Value> half_map(const std::map<Key,Value>& v) {
|
||||||
|
typedef typename std::map<Key,Value>::const_iterator iter;
|
||||||
|
std::map<Key,Value> w;
|
||||||
|
for (iter i = v.begin(); i != v.end(); ++i) {
|
||||||
|
w[i->first] = (i->second)/2;
|
||||||
|
}
|
||||||
|
return w;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
26
SWIG/Examples/python/std_map/example.i
Normal file
26
SWIG/Examples/python/std_map/example.i
Normal file
|
|
@ -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<std::string,double>;
|
||||||
|
%template() pair<std::string,int>;
|
||||||
|
%template(DoubleMap) map<std::string,double>;
|
||||||
|
%template() map<std::string,int>;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Let's just grab the original header file here */
|
||||||
|
%include "example.h"
|
||||||
|
|
||||||
|
%template(halfd) half_map<std::string,double>;
|
||||||
|
%template(halfi) half_map<std::string,int>;
|
||||||
|
|
||||||
36
SWIG/Examples/python/std_map/runme.py
Normal file
36
SWIG/Examples/python/std_map/runme.py
Normal file
|
|
@ -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()
|
||||||
Loading…
Add table
Add a link
Reference in a new issue