summaryrefslogtreecommitdiff
path: root/Examples/ruby/enum
diff options
context:
space:
mode:
Diffstat (limited to 'Examples/ruby/enum')
-rw-r--r--Examples/ruby/enum/Makefile19
-rw-r--r--Examples/ruby/enum/example.cxx37
-rw-r--r--Examples/ruby/enum/example.h13
-rw-r--r--Examples/ruby/enum/example.i11
-rw-r--r--Examples/ruby/enum/index.html35
-rw-r--r--Examples/ruby/enum/runme.rb30
6 files changed, 145 insertions, 0 deletions
diff --git a/Examples/ruby/enum/Makefile b/Examples/ruby/enum/Makefile
new file mode 100644
index 0000000..56c84c6
--- /dev/null
+++ b/Examples/ruby/enum/Makefile
@@ -0,0 +1,19 @@
+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)' ruby_cpp
+
+static::
+ $(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
+ TARGET='myruby' INTERFACE='$(INTERFACE)' ruby_cpp_static
+
+clean::
+ $(MAKE) -f $(TOP)/Makefile ruby_clean
+
+check: all
diff --git a/Examples/ruby/enum/example.cxx b/Examples/ruby/enum/example.cxx
new file mode 100644
index 0000000..6785e57
--- /dev/null
+++ b/Examples/ruby/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/ruby/enum/example.h b/Examples/ruby/enum/example.h
new file mode 100644
index 0000000..525d62a
--- /dev/null
+++ b/Examples/ruby/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/ruby/enum/example.i b/Examples/ruby/enum/example.i
new file mode 100644
index 0000000..23ee8a8
--- /dev/null
+++ b/Examples/ruby/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/ruby/enum/index.html b/Examples/ruby/enum/index.html
new file mode 100644
index 0000000..d773d9c
--- /dev/null
+++ b/Examples/ruby/enum/index.html
@@ -0,0 +1,35 @@
+<html>
+<head>
+<title>SWIG:Examples:ruby:enum</title>
+</head>
+
+<body bgcolor="#ffffff">
+
+
+<tt>SWIG/Examples/ruby/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="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>
diff --git a/Examples/ruby/enum/runme.rb b/Examples/ruby/enum/runme.rb
new file mode 100644
index 0000000..cd6a76f
--- /dev/null
+++ b/Examples/ruby/enum/runme.rb
@@ -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)