Initial revision

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@768 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Thien-Thi Nguyen 2000-08-31 19:18:56 +00:00
commit 11cb5aa8a6
13 changed files with 254 additions and 0 deletions

View file

@ -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)

View file

@ -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)

View file

@ -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

28
Examples/C++/common.mk Normal file
View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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 */