New director example

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@5087 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
William S Fulton 2003-09-06 15:54:07 +00:00
commit e871ca2862
6 changed files with 102 additions and 0 deletions

View file

@ -0,0 +1,13 @@
*.class
*.java
*_wrap.c
*_wrap.cxx
*.dll
*.dsw
*.exp
*.lib
*.ncb
*.opt
*.plg
Release
Debug

View file

@ -0,0 +1,18 @@
TOP = ../..
SWIG = $(TOP)/../swig
CXXSRCS = example.cxx
TARGET = example
INTERFACE = example.i
SWIGOPT =
all:: java
java::
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' java_cpp
javac *.java
clean::
$(MAKE) -f $(TOP)/Makefile java_clean
check: all

View file

@ -0,0 +1,4 @@
/* File : example.cxx */
#include "example.h"

View file

@ -0,0 +1,23 @@
/* File : example.h */
#include <cstdio>
#include <iostream>
class Callback {
public:
virtual ~Callback() { std::cout << "Callback::~Callback()" << std:: endl; }
virtual void run() { std::cout << "Callback::run()" << std::endl; }
};
class Caller {
private:
Callback *_callback;
public:
Caller(): _callback(0) {}
~Caller() { delCallback(); }
void delCallback() { _callback = 0; }
void setCallback(Callback *cb) { delCallback(); _callback = cb; }
void call() { if (_callback) _callback->run(); }
};

View file

@ -0,0 +1,15 @@
/* File : example.i */
%module(directors="1") example
%{
#include "example.h"
%}
// %include "typemaps.i"
// %include "std_vector.i"
%include "std_string.i"
/* turn on director wrapping Callback */
%feature("director") Callback;
%include "example.h"

View file

@ -0,0 +1,29 @@
<html>
<head>
<title>SWIG:Examples:java:callback</title>
</head>
<body bgcolor="#ffffff">
<tt>SWIG/Examples/python/extend/</tt>
<hr>
<H2>Implementing C++ callbacks in Java</H2>
<tt>$Header$</tt><br>
<p>
This example illustrates how to use directors to implement C++ callbacks in Java.
</p>
<p>
Please note that unlike Python, Java does not have an equivalent concept of
weak references, so the program has to manually clean up after objects. The
implication of this is that the <code>Caller</code> class cannot claim
ownership of the <code>Callback</code> object in <code>Caller.setCallback()</code>
and delete it.
</p>
<hr>
</body>
</html>