summaryrefslogtreecommitdiff
path: root/Examples/test-suite/director_ref.i
blob: 8bb8a9b631dd9b167b26c425c911b50ab86920fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
%module(directors="1") director_ref

%{
#include <string>

class Foo {
public:
        Foo(int i = -1) : count(0) {}
	virtual void OnDelete() {}
	virtual ~Foo() {}
	virtual std::string Msg(std::string msg = "default") { return "Foo-" + msg; }

	std::string GetMsg() { return Msg(); }
	std::string GetMsg(std::string msg) { return Msg(msg); }

	void Ref() { ++count; }
	void Unref() { --count; if (count == 0) { OnDelete(); delete this; } }
	int GetRefCount() { return count; }
private:
	int count;
};

class FooPtr {
public:
        FooPtr(Foo* f = NULL) : my_f(f) { if (my_f) { my_f->Ref(); } }
        ~FooPtr() { if (my_f) { my_f->Unref(); } }
        void Reset(Foo* f = NULL) {
		if (f) { f->Ref(); }
		if (my_f) { my_f->Unref(); }
		my_f = f;
	}
	int GetOwnedRefCount() {
		if (my_f) { return my_f->GetRefCount(); }
		return 0;
	}

private:
        Foo* my_f;
};

%}

%include <std_string.i>

%feature("director") Foo;
%feature("ref") Foo "$this->Ref();"
%feature("unref") Foo "$this->Unref();"

class Foo {
public:
        Foo(int i = -1) : count(0) {}
	virtual void OnDelete() {}
	virtual ~Foo() {}
	virtual std::string Msg(std::string msg = "default") { return "Foo-" + msg; }

	std::string GetMsg() { return Msg(); }
	std::string GetMsg(std::string msg) { return Msg(msg); }

	void Ref() { ++count; }
	void Unref() { --count; if (count == 0) { OnDelete(); delete this; } }
	int GetRefCount() { return count; }
private:
	int count;
};

class FooPtr {
public:
        FooPtr(Foo* f = NULL) : my_f(f) { if (my_f) { my_f->Ref(); } }
        ~FooPtr() { if (my_f) { my_f->Unref(); } }
        void Reset(Foo* f = NULL) {
		if (f) { f->Ref(); }
		if (my_f) { my_f->Unref(); }
		my_f = f;
	}
	int GetOwnedRefCount() {
		if (my_f) { return my_f->GetRefCount(); }
		return 0;
	}

private:
        Foo* my_f;
};