summaryrefslogtreecommitdiff
path: root/Examples/test-suite/ruby_track_objects.i
blob: f4bbb37e78eeeac549f697e07350d3d2040a8925 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
%module ruby_track_objects

%include typemaps.i

%trackobjects Foo;

%newobject Bar::get_new_foo;

%typemap(in, numinputs=0) Foo** foo (Foo *temp) {
	/* %typemap(in, numinputs=0) Foo** foo */
	$1 = &temp;
}

%typemap(argout) Foo** foo {
	/* %typemap(argout) Foo** foo */
	$result = SWIG_NewPointerObj((void *) *$1, $*1_descriptor, 0);
}	

%apply SWIGTYPE *DISOWN {Foo* ownedFoo};


%trackobjects ItemA;
%trackobjects ItemB;

%inline %{

class Foo
{
public:
	Foo() {}
	~Foo() {}

	/* Helper method that can be called from Ruby that checks
	   that two Ruby objects are pointing to the same underlying
		C++ object */
	bool cpp_equal(const Foo* other)
	{
		return (this == other);
	}

	/* Just a simple method to call on Foo*/
	const char* say_hello()
	{
		return "Hello";
	}
};


class Bar
{
private:
	Foo* owned_;
	Foo* unowned_;
public:
	Bar(): owned_(new Foo), unowned_(0)
	{
	}

	~Bar()
	{
		delete owned_;
	}

	/* Test that track objects works with %newobject */
	static Foo* get_new_foo()
	{
		return new Foo;
	}

	/* Test the same foo Ruby object is created each time */
	Foo* get_owned_foo()
	{
		return owned_;
	}

	/* Test that track objects works with argout parameters.*/
	void get_owned_foo_by_argument(Foo** foo)
	{
		*foo = owned_;
	}

	/* Test that track objects works with the DISOWN typemap.*/
	void set_owned_foo(Foo* ownedFoo)
	{
		delete owned_;
		owned_ = ownedFoo;
	}

	Foo* get_unowned_foo()
	{
		return unowned_;
	}

	void set_unowned_foo(Foo* foo)
	{
		unowned_ = foo;
	}
};

class ItemA
{
};

class ItemB: public ItemA
{
public:
};

ItemB* downcast(ItemA* item)
{
	return static_cast<ItemB*>(item);
}

class Factory
{
public:
	Factory() {}

	ItemA* createItem()
	{
		return new ItemB;
	}
};

%}