summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThien-Thi Nguyen <ttn@glug.org>2000-08-31 19:18:56 +0000
committerThien-Thi Nguyen <ttn@glug.org>2000-08-31 19:18:56 +0000
commit11cb5aa8a64e7d54857c2f994626abb4133e689b (patch)
tree6745691d5502b5692de3ae80b539f98ed3f94949
parent8abd1b9b13bec3846e186161f8441f53569ba5f2 (diff)
downloadswig-11cb5aa8a64e7d54857c2f994626abb4133e689b.tar.gz
Initial revision
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@768 626c5289-ae23-0410-ae9c-e8d60b6d4f22
-rw-r--r--Examples/C++/Java/Makefile20
-rw-r--r--Examples/C++/Python/Makefile16
-rw-r--r--Examples/C++/class/cpptest.H34
-rw-r--r--Examples/C++/common.mk28
-rw-r--r--Examples/C++/interface/cpptest.i28
-rw-r--r--Examples/C++/test_repo/constructor.py8
-rw-r--r--Examples/C++/test_repo/constructor_user_type.py8
-rw-r--r--Examples/C++/test_repo/inheritence_map_to_base_class.py12
-rw-r--r--Examples/C++/test_repo/object_method.py10
-rw-r--r--Examples/C++/test_repo/object_method_user_type.py9
-rw-r--r--Examples/C++/test_repo/primitive_method.py10
-rw-r--r--Examples/C++/test_repo/primitive_method_user_type.py10
-rw-r--r--Examples/C++/typemaps/MyString_typemap.i61
13 files changed, 254 insertions, 0 deletions
diff --git a/Examples/C++/Java/Makefile b/Examples/C++/Java/Makefile
new file mode 100644
index 000000000..c41ac5d58
--- /dev/null
+++ b/Examples/C++/Java/Makefile
@@ -0,0 +1,20 @@
+TARGETS = java_cpp $(JAR)
+JAR = cpptest.jar
+
+include ../common.mk
+
+PACKAGE = cpptest
+SWIGOPT = -o cpptest_wrap.c -package $(PACKAGE) -shadow $(INCLUDE)
+SRCS =
+TARGET = libcpptest
+INTERFACE = ../interface/cpptest.i
+JAR_FILES = cpptestJava.java cpptest.java cpptest_base.java cpptest_empty.java
+
+clean::
+ rm -f *_wrap* *.o *~ .~* core *.so *.sl so_locations
+ rm -f $(JAR_FILES) $(JAR)
+ rm -rf class
+
+PYTHON = jpython
+
+TESTS = $(wildcard ../test_repo/*.py)
diff --git a/Examples/C++/Python/Makefile b/Examples/C++/Python/Makefile
new file mode 100644
index 000000000..ecbbdb8bc
--- /dev/null
+++ b/Examples/C++/Python/Makefile
@@ -0,0 +1,16 @@
+TARGETS = python_cpp
+
+include ../common.mk
+
+SWIGOPT = -o cpptest_wrap.c -shadow $(INCLUDE)
+SRCS =
+TARGET = cpptestc
+INTERFACE = ../interface/cpptest.i
+
+
+clean:
+ rm -f *_wrap* *.py *.pyc *.o *~ .~* core *.so *.sl so_locations
+
+PYTHON = python
+
+TESTS = $(wildcard ../test_repo/*.py)
diff --git a/Examples/C++/class/cpptest.H b/Examples/C++/class/cpptest.H
new file mode 100644
index 000000000..36a62f214
--- /dev/null
+++ b/Examples/C++/class/cpptest.H
@@ -0,0 +1,34 @@
+// Copyright (C) 2000 Tal Shalif
+
+class cpptest_base
+{
+ MyString str;
+public:
+ cpptest_base(const MyString &s) : str(s) {}
+ const MyString &get() const {return str;}
+ void set(const MyString &s) {str = s;}
+};
+
+class cpptest_empty
+{
+public:
+ cpptest_empty() {}
+};
+
+class cpptest : public cpptest_base
+{
+ MyString str;
+ cpptest_base base;
+public:
+ cpptest(const MyString &s) : cpptest_base(s), base(s) {}
+ int getInt() const {return 1;}
+ const char *getString() const {return "string";}
+ const char *getMyString(const MyString &s) const {return s.c_str();}
+ cpptest &getObject() {return *this;}
+ cpptest &getObject2(const char *s) {return *this;}
+ cpptest &getObject3(const MyString &s) {return *this;}
+ cpptest_base &outBaseClass() {return *this;}
+ const MyString &inBaseClass(const cpptest_base &e) {return e.get();}
+};
+
+// cpptest.H ends here
diff --git a/Examples/C++/common.mk b/Examples/C++/common.mk
new file mode 100644
index 000000000..8a8903bf3
--- /dev/null
+++ b/Examples/C++/common.mk
@@ -0,0 +1,28 @@
+TOP =../..
+
+all:: $(TARGETS)
+
+include $(TOP)/Makefile
+
+SWIG = $(TOP)/../swig
+
+ISRCS = $(notdir $(INTERFACE:.i=_wrap.c))
+
+$(JAR): $(JAR_FILES)
+ rm -rf class
+ mkdir class
+ javac -d class $(JAR_FILES)
+ cd class && jar cvf ../$@ *
+
+INCLUDE = -I$(TOP)/../Lib -I../class -I../typemaps -I$(TOP)/../Lib
+
+check:: all
+ bad_status="" ; \
+ for i in $(TESTS); do \
+ LD_LIBRARY_PATH=. PYTHONPATH=. CLASSPATH=./$(JAR) ; \
+ ( $(PYTHON) $$i && echo "$$i: PASSED" ) || echo "$$i: FAILED"; \
+ done
+
+.PHONY: check
+
+# common.mk ends here
diff --git a/Examples/C++/interface/cpptest.i b/Examples/C++/interface/cpptest.i
new file mode 100644
index 000000000..a0d1c1697
--- /dev/null
+++ b/Examples/C++/interface/cpptest.i
@@ -0,0 +1,28 @@
+// -*-c++-*-
+// Copyright (C) 2000 Tal Shalif
+
+/* File : cpptest.i */
+#ifdef SWIGJAVA
+%module cpptestJava
+#else
+%module cpptest
+#endif
+
+%include MyString_typemap.i
+
+%{
+#define MyString string
+#include <string>
+#include "cpptest.H"
+%}
+
+%include "cpptest.H"
+
+#ifdef SWIGJAVA
+%pragma(java) module="
+static {
+ System.loadLibrary(\"cpptest\");
+}";
+#endif
+
+// cpptest.i ends here
diff --git a/Examples/C++/test_repo/constructor.py b/Examples/C++/test_repo/constructor.py
new file mode 100644
index 000000000..c7183f71a
--- /dev/null
+++ b/Examples/C++/test_repo/constructor.py
@@ -0,0 +1,8 @@
+# Copyright (C) 2000 Tal Shalif
+
+execfile('../test_conf.py')
+obj = cpptest_empty()
+if not type(obj) is types.InstanceType:
+ raise "failed"
+
+# constructor.py ends here
diff --git a/Examples/C++/test_repo/constructor_user_type.py b/Examples/C++/test_repo/constructor_user_type.py
new file mode 100644
index 000000000..f03941a5a
--- /dev/null
+++ b/Examples/C++/test_repo/constructor_user_type.py
@@ -0,0 +1,8 @@
+# Copyright (C) 2000 Tal Shalif
+
+execfile('../test_conf.py')
+obj = cpptest("MyString")
+if not type(obj) is types.InstanceType:
+ raise "failed"
+
+# constructor_user_type.py ends here
diff --git a/Examples/C++/test_repo/inheritence_map_to_base_class.py b/Examples/C++/test_repo/inheritence_map_to_base_class.py
new file mode 100644
index 000000000..777c12b3a
--- /dev/null
+++ b/Examples/C++/test_repo/inheritence_map_to_base_class.py
@@ -0,0 +1,12 @@
+# Copyright (C) 2000 Tal Shalif
+
+execfile('../test_conf.py')
+obj1 = cpptest("obj1")
+obj2 = cpptest_base("obj2")
+res1 = obj1.inBaseClass(obj1)
+res2 = obj1.inBaseClass(obj2)
+
+if res1 != "obj1" or res2 != "obj2":
+ raise "failed"
+
+# inheritence_map_to_base_class.py ends here
diff --git a/Examples/C++/test_repo/object_method.py b/Examples/C++/test_repo/object_method.py
new file mode 100644
index 000000000..ee353bbf8
--- /dev/null
+++ b/Examples/C++/test_repo/object_method.py
@@ -0,0 +1,10 @@
+# Copyright (C) 2000 Tal Shalif
+
+execfile('../test_conf.py')
+obj = cpptest("")
+obj2 = obj.getObject()
+obj3 = obj.getObject2("string")
+if not type(obj2) is types.InstanceType or not type(obj3) is types.InstanceType:
+ raise "failed"
+
+# object_method.py ends here
diff --git a/Examples/C++/test_repo/object_method_user_type.py b/Examples/C++/test_repo/object_method_user_type.py
new file mode 100644
index 000000000..65eda3969
--- /dev/null
+++ b/Examples/C++/test_repo/object_method_user_type.py
@@ -0,0 +1,9 @@
+# Copyright (C) 2000 Tal Shalif
+
+execfile('../test_conf.py')
+obj = cpptest("")
+obj2 = obj.getObject3("MyString")
+if not type(obj2) is types.InstanceType:
+ raise "failed"
+
+# object_method_user_type.py ends here
diff --git a/Examples/C++/test_repo/primitive_method.py b/Examples/C++/test_repo/primitive_method.py
new file mode 100644
index 000000000..88ac24dc3
--- /dev/null
+++ b/Examples/C++/test_repo/primitive_method.py
@@ -0,0 +1,10 @@
+# Copyright (C) 2000 Tal Shalif
+
+execfile('../test_conf.py')
+obj = cpptest("")
+if not type(obj.getInt()) is types.IntType:
+ raise "failed"
+if not type(obj.getString()) is types.StringType:
+ raise "failed"
+
+# primitive_method.py ends here
diff --git a/Examples/C++/test_repo/primitive_method_user_type.py b/Examples/C++/test_repo/primitive_method_user_type.py
new file mode 100644
index 000000000..8e0bc688c
--- /dev/null
+++ b/Examples/C++/test_repo/primitive_method_user_type.py
@@ -0,0 +1,10 @@
+# Copyright (C) 2000 Tal Shalif
+
+execfile('../test_conf.py')
+obj = cpptest("")
+
+res = obj.getMyString("string")
+if not type(res) is types.StringType or not "string" == res:
+ raise "failed"
+
+# primitive_method_user_type.py ends here
diff --git a/Examples/C++/typemaps/MyString_typemap.i b/Examples/C++/typemaps/MyString_typemap.i
new file mode 100644
index 000000000..2f73c59f4
--- /dev/null
+++ b/Examples/C++/typemaps/MyString_typemap.i
@@ -0,0 +1,61 @@
+/*
+ * typemaps for standard C++ string
+ * Copyright (C) 2000 Tal Shalif <tal@slt.atr.co.jp>
+ */
+/* what type to use in java source code */
+%typemap(java,jtype) const MyString & {String}
+
+/* what is the corresponding jni type */
+%typemap(java,jni) const MyString & {jstring}
+
+/* how to convert the c++ type to the java type */
+%typemap(java,out) const MyString & {
+ $target = JCALL(NewStringUTF, jenv) $source->c_str());
+}
+
+/* how to convert java type to requested c++ type */
+%typemap(java,in) const MyString & {
+ $target = NULL;
+ if($source != NULL) {
+ /* get the String from the StringBuffer */
+ char *p = (char *)jenv->GetStringUTFChars($source, 0);
+ $target = new string(p);
+ /* free(p); HdH assuming string constructor makes a copy */
+ JCALL(ReleaseStringUTFChars, jenv) $source, p);
+ }
+}
+/* free resource once finished using */
+%typemap(java,freearg) const MyString & {
+ delete $target;
+}
+
+%typemap(java,jtype) MyString & = const MyString &;
+%typemap(java,jni) MyString & = const MyString &;
+%typemap(java,in) MyString & = const MyString &;
+%typemap(java,out) MyString & = const MyString &;
+%typemap(java,freearg) MyString & = const MyString &;
+
+
+%typemap(python,in) const MyString & {
+ if (PyString_Check ($source))
+ {
+ $target = new string((char *)PyString_AsString($source));
+ }
+ else
+ {
+ PyErr_SetString (PyExc_TypeError, "not a string");
+ return NULL;
+ }
+}
+%typemap(python,out) const MyString & {
+ $target = PyString_FromString($source->c_str());
+}
+%typemap (python, freearg) const MyString & {
+ delete $source;
+}
+
+%typemap(python,in) MyString & = const MyString &;
+%typemap(python,out) MyString & = const MyString &;
+%typemap(python,freearg) MyString & = const MyString &;
+
+/* MyString_typemap.i ends here */