Ruby enum example added
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@867 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
bf6c97e2d3
commit
73cc1bd3bb
7 changed files with 148 additions and 0 deletions
19
SWIG/Examples/ruby/enum/Makefile
Normal file
19
SWIG/Examples/ruby/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)' ruby_cpp
|
||||||
|
|
||||||
|
static::
|
||||||
|
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
|
||||||
|
TARGET='myruby' INTERFACE='$(INTERFACE)' ruby_cpp_static
|
||||||
|
|
||||||
|
clean::
|
||||||
|
rm -f *_wrap* *.o *~ *.so myruby .~* core
|
||||||
|
|
||||||
|
check: all
|
||||||
37
SWIG/Examples/ruby/enum/example.cxx
Normal file
37
SWIG/Examples/ruby/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
SWIG/Examples/ruby/enum/example.h
Normal file
13
SWIG/Examples/ruby/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
SWIG/Examples/ruby/enum/example.i
Normal file
11
SWIG/Examples/ruby/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"
|
||||||
|
|
||||||
37
SWIG/Examples/ruby/enum/index.html
Normal file
37
SWIG/Examples/ruby/enum/index.html
Normal file
|
|
@ -0,0 +1,37 @@
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>SWIG:Examples:ruby:enum</title>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body bgcolor="#ffffff">
|
||||||
|
|
||||||
|
|
||||||
|
<tt>SWIG/Examples/ruby/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="runme.rb">runme.rb</a>. Sample Ruby 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>
|
||||||
30
SWIG/Examples/ruby/enum/runme.rb
Normal file
30
SWIG/Examples/ruby/enum/runme.rb
Normal file
|
|
@ -0,0 +1,30 @@
|
||||||
|
# file: runme.rb
|
||||||
|
|
||||||
|
require 'example'
|
||||||
|
|
||||||
|
# ----- Object creation -----
|
||||||
|
|
||||||
|
# Print out the value of some enums
|
||||||
|
print "*** color ***\n"
|
||||||
|
print " RED = #{Example::RED}\n"
|
||||||
|
print " BLUE = #{Example::BLUE}\n"
|
||||||
|
print " GREEN = #{Example::GREEN}\n"
|
||||||
|
|
||||||
|
print "\n*** Foo::speed ***\n"
|
||||||
|
print " Foo::IMPULSE = #{Example::Foo::IMPULSE}\n"
|
||||||
|
print " Foo::WARP = #{Example::Foo::WARP}\n"
|
||||||
|
print " Foo::LUDICROUS = #{Example::Foo::LUDICROUS}\n"
|
||||||
|
|
||||||
|
print "\nTesting use of enums with functions\n\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\n"
|
||||||
|
f = Example::Foo.new()
|
||||||
|
|
||||||
|
f.enum_test(Example::Foo::IMPULSE)
|
||||||
|
f.enum_test(Example::Foo::WARP)
|
||||||
|
f.enum_test(Example::Foo::LUDICROUS)
|
||||||
|
|
@ -22,6 +22,7 @@ certain C declarations are turned into constants.
|
||||||
<li><a href="reference/index.html">reference</a>. C++ references.
|
<li><a href="reference/index.html">reference</a>. C++ references.
|
||||||
<li><a href="pointer/index.html">pointer</a>. Simple pointer handling.
|
<li><a href="pointer/index.html">pointer</a>. Simple pointer handling.
|
||||||
<li><a href="funcptr/index.html">funcptr</a>. Pointers to functions.
|
<li><a href="funcptr/index.html">funcptr</a>. Pointers to functions.
|
||||||
|
<li><a href="enum/index.html">enum</a>. Enumeration.
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
<h2>Compilation Issues</h2>
|
<h2>Compilation Issues</h2>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue