Add text streamer to Android example to replace missing stdout
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@13833 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
9e34a0d376
commit
608d43f0f9
4 changed files with 44 additions and 6 deletions
|
|
@ -2,3 +2,13 @@
|
|||
|
||||
#include "example.h"
|
||||
|
||||
static Streamer * streamerInstance = 0;
|
||||
|
||||
void setStreamer(Streamer* streamer) {
|
||||
streamerInstance = streamer;
|
||||
}
|
||||
|
||||
Streamer& getStreamer() {
|
||||
return *streamerInstance;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -5,6 +5,21 @@
|
|||
#include <vector>
|
||||
#include <string>
|
||||
#include <cmath>
|
||||
#include <sstream>
|
||||
|
||||
struct Streamer {
|
||||
virtual void display(std::string text) const = 0;
|
||||
virtual ~Streamer() {}
|
||||
};
|
||||
void setStreamer(Streamer* streamer);
|
||||
Streamer& getStreamer();
|
||||
|
||||
template<typename T> Streamer& operator<<(Streamer& stream, T const& val) {
|
||||
std::ostringstream s;
|
||||
s << val;
|
||||
stream.display(s.str());
|
||||
return stream;
|
||||
}
|
||||
|
||||
class Employee {
|
||||
private:
|
||||
|
|
@ -14,7 +29,7 @@ public:
|
|||
virtual std::string getTitle() { return getPosition() + " " + getName(); }
|
||||
virtual std::string getName() { return name; }
|
||||
virtual std::string getPosition() const { return "Employee"; }
|
||||
virtual ~Employee() { printf("~Employee() @ %p\n", this); }
|
||||
virtual ~Employee() { getStreamer() << "~Employee() @ " << this << "\n"; }
|
||||
};
|
||||
|
||||
|
||||
|
|
@ -35,10 +50,10 @@ public:
|
|||
}
|
||||
void addEmployee(Employee *p) {
|
||||
list.push_back(p);
|
||||
std::cout << "New employee added. Current employees are:" << std::endl;
|
||||
getStreamer() << "New employee added. Current employees are:" << "\n";
|
||||
std::vector<Employee*>::iterator i;
|
||||
for (i=list.begin(); i!=list.end(); i++) {
|
||||
std::cout << " " << (*i)->getTitle() << std::endl;
|
||||
getStreamer() << " " << (*i)->getTitle() << "\n";
|
||||
}
|
||||
}
|
||||
const Employee *get_item(int i) {
|
||||
|
|
@ -46,11 +61,11 @@ public:
|
|||
}
|
||||
~EmployeeList() {
|
||||
std::vector<Employee*>::iterator i;
|
||||
std::cout << "~EmployeeList, deleting " << list.size() << " employees." << std::endl;
|
||||
getStreamer() << "~EmployeeList, deleting " << list.size() << " employees." << "\n";
|
||||
for (i=list.begin(); i!=list.end(); i++) {
|
||||
delete *i;
|
||||
}
|
||||
std::cout << "~EmployeeList empty." << std::endl;
|
||||
getStreamer() << "~EmployeeList empty." << "\n";
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -11,5 +11,8 @@
|
|||
%feature("director") Employee;
|
||||
%feature("director") Manager;
|
||||
|
||||
/* A base class for callbacks from C++ to output text on the Java side */
|
||||
%feature("director") Streamer;
|
||||
|
||||
%include "example.h"
|
||||
|
||||
|
|
|
|||
|
|
@ -8,7 +8,6 @@ import android.widget.TextView;
|
|||
import android.widget.ScrollView;
|
||||
import android.text.method.ScrollingMovementMethod;
|
||||
|
||||
|
||||
// CEO class, which overrides Employee::getPosition().
|
||||
class CEO extends Manager {
|
||||
public CEO(String name) {
|
||||
|
|
@ -29,6 +28,15 @@ public class SwigExtend extends Activity
|
|||
TextView outputText = null;
|
||||
ScrollView scroller = null;
|
||||
|
||||
/** Handles upcalls from C++ so that C++ code can display text on the TextView */
|
||||
class TextViewStreamer extends Streamer {
|
||||
public void display(String text) {
|
||||
outputText.append(text);
|
||||
}
|
||||
}
|
||||
|
||||
TextViewStreamer textViewStreamer = new TextViewStreamer();
|
||||
|
||||
/** Called when the activity is first created. */
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState)
|
||||
|
|
@ -41,6 +49,8 @@ public class SwigExtend extends Activity
|
|||
outputText.setMovementMethod(new ScrollingMovementMethod());
|
||||
|
||||
scroller = (ScrollView)findViewById(R.id.Scroller);
|
||||
|
||||
example.setStreamer(textViewStreamer);
|
||||
}
|
||||
|
||||
public void onRunButtonClick(View view)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue