diff --git a/SWIG/Examples/python/enum/Makefile b/SWIG/Examples/python/enum/Makefile new file mode 100644 index 000000000..71af176f9 --- /dev/null +++ b/SWIG/Examples/python/enum/Makefile @@ -0,0 +1,19 @@ +TOP = ../.. +SWIG = $(TOP)/../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:: + rm -f *_wrap* *.o *~ *.so mypython *.pyc .~* core + +check: all diff --git a/SWIG/Examples/python/enum/example.cxx b/SWIG/Examples/python/enum/example.cxx new file mode 100644 index 000000000..6785e57ac --- /dev/null +++ b/SWIG/Examples/python/enum/example.cxx @@ -0,0 +1,37 @@ +/* File : example.c */ + +#include "example.h" +#include + +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/SWIG/Examples/python/enum/example.h b/SWIG/Examples/python/enum/example.h new file mode 100644 index 000000000..525d62afc --- /dev/null +++ b/SWIG/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/SWIG/Examples/python/enum/example.i b/SWIG/Examples/python/enum/example.i new file mode 100644 index 000000000..23ee8a822 --- /dev/null +++ b/SWIG/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/SWIG/Examples/python/enum/example.py b/SWIG/Examples/python/enum/example.py new file mode 100644 index 000000000..92979783e --- /dev/null +++ b/SWIG/Examples/python/enum/example.py @@ -0,0 +1,30 @@ +# file: example.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.new_Foo() + +example.Foo_enum_test(f,example.Foo_IMPULSE) +example.Foo_enum_test(f,example.Foo_WARP) +example.Foo_enum_test(f,example.Foo_LUDICROUS) diff --git a/SWIG/Examples/python/enum/index.html b/SWIG/Examples/python/enum/index.html new file mode 100644 index 000000000..e5c7c578a --- /dev/null +++ b/SWIG/Examples/python/enum/index.html @@ -0,0 +1,37 @@ + + +SWIG:Examples:python:enum + + + + + +SWIG/Examples/python/enum/ +
+ +

Wrapping enumerations

+ +$Header$
+ +

+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. + +

+ +

Notes

+ + + +
+ +