new example
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@861 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
3d800bf53c
commit
4fd0a45a9b
6 changed files with 147 additions and 0 deletions
19
Examples/python/enum/Makefile
Normal file
19
Examples/python/enum/Makefile
Normal file
|
|
@ -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
|
||||||
37
Examples/python/enum/example.cxx
Normal file
37
Examples/python/enum/example.cxx
Normal file
|
|
@ -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");
|
||||||
|
}
|
||||||
|
}
|
||||||
13
Examples/python/enum/example.h
Normal file
13
Examples/python/enum/example.h
Normal file
|
|
@ -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);
|
||||||
|
|
||||||
11
Examples/python/enum/example.i
Normal file
11
Examples/python/enum/example.i
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
/* File : example.i */
|
||||||
|
%module example
|
||||||
|
|
||||||
|
%{
|
||||||
|
#include "example.h"
|
||||||
|
%}
|
||||||
|
|
||||||
|
/* Let's just grab the original header file here */
|
||||||
|
|
||||||
|
%include "example.h"
|
||||||
|
|
||||||
30
Examples/python/enum/example.py
Normal file
30
Examples/python/enum/example.py
Normal file
|
|
@ -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)
|
||||||
37
Examples/python/enum/index.html
Normal file
37
Examples/python/enum/index.html
Normal file
|
|
@ -0,0 +1,37 @@
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>SWIG:Examples:python:enum</title>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body bgcolor="#ffffff">
|
||||||
|
|
||||||
|
|
||||||
|
<tt>SWIG/Examples/python/enum/</tt>
|
||||||
|
<hr>
|
||||||
|
|
||||||
|
<H2>Wrapping enumerations</H2>
|
||||||
|
|
||||||
|
<tt>$Header$</tt><br>
|
||||||
|
|
||||||
|
<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>
|
||||||
Loading…
Add table
Add a link
Reference in a new issue