diff options
author | Lorry Tar Creator <lorry-tar-importer@baserock.org> | 2009-08-18 20:56:02 +0000 |
---|---|---|
committer | Lorry <lorry@roadtrain.codethink.co.uk> | 2012-09-25 16:59:08 +0000 |
commit | 9f8a09ed743cedd9547bf0661d518647966ab114 (patch) | |
tree | 9c7803d3b27a8ec22e91792ac7f7932efa128b20 /Examples/python/enum | |
download | swig-tarball-master.tar.gz |
Imported from /srv/lorry/lorry-area/swig-tarball/swig-1.3.40.tar.gz.HEADswig-1.3.40master
Diffstat (limited to 'Examples/python/enum')
-rw-r--r-- | Examples/python/enum/Makefile | 21 | ||||
-rw-r--r-- | Examples/python/enum/example.cxx | 37 | ||||
-rw-r--r-- | Examples/python/enum/example.h | 13 | ||||
-rw-r--r-- | Examples/python/enum/example.i | 11 | ||||
-rw-r--r-- | Examples/python/enum/index.html | 35 | ||||
-rw-r--r-- | Examples/python/enum/runme.py | 31 |
6 files changed, 148 insertions, 0 deletions
diff --git a/Examples/python/enum/Makefile b/Examples/python/enum/Makefile new file mode 100644 index 0000000..74625b9 --- /dev/null +++ b/Examples/python/enum/Makefile @@ -0,0 +1,21 @@ +TOP = ../.. +SWIG = $(TOP)/../preinst-swig +CXXSRCS = example.cxx +TARGET = example +INTERFACE = example.i +LIBS = -lm + +all:: + $(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \ + TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' python_cpp + +static:: + $(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \ + TARGET='mypython' INTERFACE='$(INTERFACE)' python_cpp_static + +clean:: + $(MAKE) -f $(TOP)/Makefile python_clean + rm -f $(TARGET).py + +check: all + $(MAKE) -f $(TOP)/Makefile python_run diff --git a/Examples/python/enum/example.cxx b/Examples/python/enum/example.cxx new file mode 100644 index 0000000..6785e57 --- /dev/null +++ b/Examples/python/enum/example.cxx @@ -0,0 +1,37 @@ +/* File : example.c */ + +#include "example.h" +#include <stdio.h> + +void Foo::enum_test(speed s) { + if (s == IMPULSE) { + printf("IMPULSE speed\n"); + } else if (s == WARP) { + printf("WARP speed\n"); + } else if (s == LUDICROUS) { + printf("LUDICROUS speed\n"); + } else { + printf("Unknown speed\n"); + } +} + +void enum_test(color c, Foo::speed s) { + if (c == RED) { + printf("color = RED, "); + } else if (c == BLUE) { + printf("color = BLUE, "); + } else if (c == GREEN) { + printf("color = GREEN, "); + } else { + printf("color = Unknown color!, "); + } + if (s == Foo::IMPULSE) { + printf("speed = IMPULSE speed\n"); + } else if (s == Foo::WARP) { + printf("speed = WARP speed\n"); + } else if (s == Foo::LUDICROUS) { + printf("speed = LUDICROUS speed\n"); + } else { + printf("speed = Unknown speed!\n"); + } +} diff --git a/Examples/python/enum/example.h b/Examples/python/enum/example.h new file mode 100644 index 0000000..525d62a --- /dev/null +++ b/Examples/python/enum/example.h @@ -0,0 +1,13 @@ +/* File : example.h */ + +enum color { RED, BLUE, GREEN }; + +class Foo { + public: + Foo() { } + enum speed { IMPULSE, WARP, LUDICROUS }; + void enum_test(speed s); +}; + +void enum_test(color c, Foo::speed s); + diff --git a/Examples/python/enum/example.i b/Examples/python/enum/example.i new file mode 100644 index 0000000..23ee8a8 --- /dev/null +++ b/Examples/python/enum/example.i @@ -0,0 +1,11 @@ +/* File : example.i */ +%module example + +%{ +#include "example.h" +%} + +/* Let's just grab the original header file here */ + +%include "example.h" + diff --git a/Examples/python/enum/index.html b/Examples/python/enum/index.html new file mode 100644 index 0000000..776030f --- /dev/null +++ b/Examples/python/enum/index.html @@ -0,0 +1,35 @@ +<html> +<head> +<title>SWIG:Examples:python:enum</title> +</head> + +<body bgcolor="#ffffff"> + + +<tt>SWIG/Examples/python/enum/</tt> +<hr> + +<H2>Wrapping enumerations</H2> + +<p> +This example tests SWIG's ability to wrap enumerations. By default, SWIG +converts enumeration specifications into integer constants. Further use +of enumerated types are handled as integers. + +<ul> +<li><a href="example.h">example.h</a>. Header file containing some enums. +<li><a href="example.i">example.i</a>. Interface file. +<li><a href="example.py">example.py</a>. Sample Python script. +</ul> + +<h2>Notes</h2> + +<ul> +<li>SWIG allows arbitrary integers to be passed as enum values. However, +the result of passing an integer not corresponding to any of the values +specified in the <tt>enum</tt> specification is undefined. +</ul> + +<hr> +</body> +</html> diff --git a/Examples/python/enum/runme.py b/Examples/python/enum/runme.py new file mode 100644 index 0000000..10c4a26 --- /dev/null +++ b/Examples/python/enum/runme.py @@ -0,0 +1,31 @@ +# file: runme.py + +import example + +# ----- Object creation ----- + +# Print out the value of some enums +print "*** color ***" +print " RED =", example.RED +print " BLUE =", example.BLUE +print " GREEN =", example.GREEN + +print "\n*** Foo::speed ***" +print " Foo_IMPULSE =", example.Foo.IMPULSE +print " Foo_WARP =", example.Foo.WARP +print " Foo_LUDICROUS =", example.Foo.LUDICROUS + +print "\nTesting use of enums with functions\n" + +example.enum_test(example.RED, example.Foo.IMPULSE) +example.enum_test(example.BLUE, example.Foo.WARP) +example.enum_test(example.GREEN, example.Foo.LUDICROUS) +example.enum_test(1234,5678) + +print "\nTesting use of enum with class method" +f = example.Foo() + +f.enum_test(example.Foo.IMPULSE) +f.enum_test(example.Foo.WARP) +f.enum_test(example.Foo.LUDICROUS) + |