Skip to content

Commit 5a379d3

Browse files
committed
Add Tcl support for std::unique_ptr and std::auto_ptr
Equivalent to C#/Java implementations.
1 parent 6bf2138 commit 5a379d3

10 files changed

+521
-3
lines changed

CHANGES.current

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ the issue number to the end of the URL: https://github.com/swig/swig/issues/
77
Version 4.1.0 (in progress)
88
===========================
99

10+
2022-08-05: wsfulton
11+
[D] Add support for std::unique_ptr in std_unique_ptr.i.
12+
Add support for std::auto_ptr in std_auto_ptr.i.
13+
1014
2022-08-03: wsfulton
1115
[Javascript] Add support for std::unique_ptr in std_unique_ptr.i.
1216
Add support for std::auto_ptr in std_auto_ptr.i.

Examples/test-suite/cpp11_std_unique_ptr.i

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
%module cpp11_std_unique_ptr
22

3-
#if defined(SWIGCSHARP) || defined(SWIGJAVA) || defined(SWIGPYTHON) || defined(SWIGRUBY) || defined(SWIGPERL) || defined(SWIGTCL) || defined(SWIGOCTAVE) || defined(SWIGJAVASCRIPT)
3+
#if defined(SWIGCSHARP) || defined(SWIGJAVA) || defined(SWIGPYTHON) || defined(SWIGRUBY) || defined(SWIGPERL) || defined(SWIGTCL) || defined(SWIGOCTAVE) || defined(SWIGJAVASCRIPT) || defined(SWIGD)
44

55
%include "std_string.i"
66
%include "std_unique_ptr.i"
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
module cpp11_std_unique_ptr_runme;
2+
3+
import cpp11_std_unique_ptr.cpp11_std_unique_ptr;
4+
import cpp11_std_unique_ptr.Klass;
5+
import cpp11_std_unique_ptr.KlassInheritance;
6+
import std.conv;
7+
import std.algorithm;
8+
9+
void checkCount(int expected_count) {
10+
int actual_count = Klass.getTotal_count();
11+
if (actual_count != expected_count)
12+
throw new Exception("Counts incorrect, expected:" ~ to!string(expected_count) ~ " actual:" ~ to!string(actual_count));
13+
}
14+
15+
void main() {
16+
// unique_ptr as input
17+
{
18+
scope Klass kin = new Klass("KlassInput");
19+
checkCount(1);
20+
string s = takeKlassUniquePtr(kin);
21+
checkCount(0);
22+
if (s != "KlassInput")
23+
throw new Exception("Incorrect string: " ~ s);
24+
if (!is_nullptr(kin))
25+
throw new Exception("is_nullptr failed");
26+
} // dispose should not fail, even though already deleted
27+
checkCount(0);
28+
29+
{
30+
scope Klass kin = new Klass("KlassInput");
31+
checkCount(1);
32+
string s = takeKlassUniquePtr(kin);
33+
checkCount(0);
34+
if (s != "KlassInput")
35+
throw new Exception("Incorrect string: " ~ s);
36+
if (!is_nullptr(kin))
37+
throw new Exception("is_nullptr failed");
38+
bool exception_thrown = false;
39+
try {
40+
takeKlassUniquePtr(kin);
41+
} catch (Exception e) {
42+
if (!canFind(e.msg, "Cannot release ownership as memory is not owned"))
43+
throw new Exception("incorrect exception message: " ~ e.msg);
44+
exception_thrown = true;
45+
}
46+
if (!exception_thrown)
47+
throw new Exception("double usage of takeKlassUniquePtr should have been an error");
48+
} // dispose should not fail, even though already deleted
49+
checkCount(0);
50+
51+
{
52+
scope Klass kin = new Klass("KlassInput");
53+
bool exception_thrown = false;
54+
Klass notowned = get_not_owned_ptr(kin);
55+
try {
56+
takeKlassUniquePtr(notowned);
57+
} catch (Exception e) {
58+
if (!canFind(e.msg, "Cannot release ownership as memory is not owned"))
59+
throw new Exception("incorrect exception message: " ~ e.msg);
60+
exception_thrown = true;
61+
}
62+
if (!exception_thrown)
63+
throw new Exception("Should have thrown 'Cannot release ownership as memory is not owned' error");
64+
checkCount(1);
65+
}
66+
checkCount(0);
67+
68+
/*
69+
{
70+
scope KlassInheritance kini = new KlassInheritance("KlassInheritanceInput");
71+
checkCount(1);
72+
string s = takeKlassUniquePtr(kini);
73+
checkCount(0);
74+
if (s != "KlassInheritanceInput")
75+
throw new Exception("Incorrect string: " ~ s);
76+
if (!is_nullptr(kini))
77+
throw new Exception("is_nullptr failed");
78+
} // dispose should not fail, even though already deleted
79+
checkCount(0);
80+
*/
81+
82+
// unique_ptr as output
83+
Klass k1 = makeKlassUniquePtr("first");
84+
if (k1.getLabel() != "first")
85+
throw new Exception("wrong object label");
86+
87+
Klass k2 = makeKlassUniquePtr("second");
88+
if (Klass.getTotal_count() != 2)
89+
throw new Exception("number of objects should be 2");
90+
91+
k1.dispose();
92+
if (Klass.getTotal_count() != 1)
93+
throw new Exception("number of objects should be 1");
94+
95+
if (k2.getLabel() != "second")
96+
throw new Exception("wrong object label");
97+
98+
k2.dispose();
99+
if (Klass.getTotal_count() != 0)
100+
throw new Exception("no objects should be left");
101+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
module cpp11_std_unique_ptr_runme;
2+
3+
import cpp11_std_unique_ptr.cpp11_std_unique_ptr;
4+
import cpp11_std_unique_ptr.Klass;
5+
import cpp11_std_unique_ptr.KlassInheritance;
6+
import std.conv;
7+
import std.algorithm;
8+
9+
void checkCount(int expected_count) {
10+
int actual_count = Klass.getTotal_count();
11+
if (actual_count != expected_count)
12+
throw new Exception("Counts incorrect, expected:" ~ to!string(expected_count) ~ " actual:" ~ to!string(actual_count));
13+
}
14+
15+
void main() {
16+
// unique_ptr as input
17+
{
18+
scope Klass kin = new Klass("KlassInput");
19+
checkCount(1);
20+
string s = takeKlassUniquePtr(kin);
21+
checkCount(0);
22+
if (s != "KlassInput")
23+
throw new Exception("Incorrect string: " ~ s);
24+
if (!is_nullptr(kin))
25+
throw new Exception("is_nullptr failed");
26+
} // dispose should not fail, even though already deleted
27+
checkCount(0);
28+
29+
{
30+
scope Klass kin = new Klass("KlassInput");
31+
checkCount(1);
32+
string s = takeKlassUniquePtr(kin);
33+
checkCount(0);
34+
if (s != "KlassInput")
35+
throw new Exception("Incorrect string: " ~ s);
36+
if (!is_nullptr(kin))
37+
throw new Exception("is_nullptr failed");
38+
bool exception_thrown = false;
39+
try {
40+
takeKlassUniquePtr(kin);
41+
} catch (Exception e) {
42+
if (!canFind(e.msg, "Cannot release ownership as memory is not owned"))
43+
throw new Exception("incorrect exception message: " ~ e.msg);
44+
exception_thrown = true;
45+
}
46+
if (!exception_thrown)
47+
throw new Exception("double usage of takeKlassUniquePtr should have been an error");
48+
} // dispose should not fail, even though already deleted
49+
checkCount(0);
50+
51+
{
52+
scope Klass kin = new Klass("KlassInput");
53+
bool exception_thrown = false;
54+
Klass notowned = get_not_owned_ptr(kin);
55+
try {
56+
takeKlassUniquePtr(notowned);
57+
} catch (Exception e) {
58+
if (!canFind(e.msg, "Cannot release ownership as memory is not owned"))
59+
throw new Exception("incorrect exception message: " ~ e.msg);
60+
exception_thrown = true;
61+
}
62+
if (!exception_thrown)
63+
throw new Exception("Should have thrown 'Cannot release ownership as memory is not owned' error");
64+
checkCount(1);
65+
}
66+
checkCount(0);
67+
68+
/*
69+
{
70+
scope KlassInheritance kini = new KlassInheritance("KlassInheritanceInput");
71+
checkCount(1);
72+
string s = takeKlassUniquePtr(kini);
73+
checkCount(0);
74+
if (s != "KlassInheritanceInput")
75+
throw new Exception("Incorrect string: " ~ s);
76+
if (!is_nullptr(kini))
77+
throw new Exception("is_nullptr failed");
78+
} // dispose should not fail, even though already deleted
79+
checkCount(0);
80+
*/
81+
82+
// unique_ptr as output
83+
Klass k1 = makeKlassUniquePtr("first");
84+
if (k1.getLabel() != "first")
85+
throw new Exception("wrong object label");
86+
87+
Klass k2 = makeKlassUniquePtr("second");
88+
if (Klass.getTotal_count() != 2)
89+
throw new Exception("number of objects should be 2");
90+
91+
k1.dispose();
92+
if (Klass.getTotal_count() != 1)
93+
throw new Exception("number of objects should be 1");
94+
95+
if (k2.getLabel() != "second")
96+
throw new Exception("wrong object label");
97+
98+
k2.dispose();
99+
if (Klass.getTotal_count() != 0)
100+
throw new Exception("no objects should be left");
101+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
module li_std_auto_ptr_runme;
2+
3+
import li_std_auto_ptr.li_std_auto_ptr;
4+
import li_std_auto_ptr.Klass;
5+
import li_std_auto_ptr.KlassInheritance;
6+
import std.conv;
7+
import std.algorithm;
8+
9+
void checkCount(int expected_count) {
10+
int actual_count = Klass.getTotal_count();
11+
if (actual_count != expected_count)
12+
throw new Exception("Counts incorrect, expected:" ~ to!string(expected_count) ~ " actual:" ~ to!string(actual_count));
13+
}
14+
15+
void main() {
16+
// auto_ptr as input
17+
{
18+
scope Klass kin = new Klass("KlassInput");
19+
checkCount(1);
20+
string s = takeKlassAutoPtr(kin);
21+
checkCount(0);
22+
if (s != "KlassInput")
23+
throw new Exception("Incorrect string: " ~ s);
24+
if (!is_nullptr(kin))
25+
throw new Exception("is_nullptr failed");
26+
} // dispose should not fail, even though already deleted
27+
checkCount(0);
28+
29+
{
30+
scope Klass kin = new Klass("KlassInput");
31+
checkCount(1);
32+
string s = takeKlassAutoPtr(kin);
33+
checkCount(0);
34+
if (s != "KlassInput")
35+
throw new Exception("Incorrect string: " ~ s);
36+
if (!is_nullptr(kin))
37+
throw new Exception("is_nullptr failed");
38+
bool exception_thrown = false;
39+
try {
40+
takeKlassAutoPtr(kin);
41+
} catch (Exception e) {
42+
if (!canFind(e.msg, "Cannot release ownership as memory is not owned"))
43+
throw new Exception("incorrect exception message: " ~ e.msg);
44+
exception_thrown = true;
45+
}
46+
if (!exception_thrown)
47+
throw new Exception("double usage of takeKlassAutoPtr should have been an error");
48+
} // dispose should not fail, even though already deleted
49+
checkCount(0);
50+
51+
{
52+
scope Klass kin = new Klass("KlassInput");
53+
bool exception_thrown = false;
54+
Klass notowned = get_not_owned_ptr(kin);
55+
try {
56+
takeKlassAutoPtr(notowned);
57+
} catch (Exception e) {
58+
if (!canFind(e.msg, "Cannot release ownership as memory is not owned"))
59+
throw new Exception("incorrect exception message: " ~ e.msg);
60+
exception_thrown = true;
61+
}
62+
if (!exception_thrown)
63+
throw new Exception("Should have thrown 'Cannot release ownership as memory is not owned' error");
64+
checkCount(1);
65+
}
66+
checkCount(0);
67+
68+
/*
69+
{
70+
scope KlassInheritance kini = new KlassInheritance("KlassInheritanceInput");
71+
checkCount(1);
72+
string s = takeKlassAutoPtr(kini);
73+
checkCount(0);
74+
if (s != "KlassInheritanceInput")
75+
throw new Exception("Incorrect string: " ~ s);
76+
if (!is_nullptr(kini))
77+
throw new Exception("is_nullptr failed");
78+
} // dispose should not fail, even though already deleted
79+
checkCount(0);
80+
*/
81+
82+
// auto_ptr as output
83+
Klass k1 = makeKlassAutoPtr("first");
84+
if (k1.getLabel() != "first")
85+
throw new Exception("wrong object label");
86+
87+
Klass k2 = makeKlassAutoPtr("second");
88+
if (Klass.getTotal_count() != 2)
89+
throw new Exception("number of objects should be 2");
90+
91+
k1.dispose();
92+
if (Klass.getTotal_count() != 1)
93+
throw new Exception("number of objects should be 1");
94+
95+
if (k2.getLabel() != "second")
96+
throw new Exception("wrong object label");
97+
98+
k2.dispose();
99+
if (Klass.getTotal_count() != 0)
100+
throw new Exception("no objects should be left");
101+
}

0 commit comments

Comments
 (0)