Skip to content

Commit 6c4dcbb

Browse files
committed
R shared_ptr fixes
Fix problems in shared_ptr wrappers where the class names were not consistent when using the shared_ptr template or the actual underlying type. Move $R_class substitution to typemaps. Issue swig#2386
1 parent 0fec14b commit 6c4dcbb

File tree

4 files changed

+57
-73
lines changed

4 files changed

+57
-73
lines changed

CHANGES.current

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ 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-10-24: wsfulton, AndLLA
11+
[R] #2386 Fix problems in shared_ptr wrappers where the class names
12+
were not consistent when using the shared_ptr template or the actual
13+
underlying type.
14+
1015
2022-10-24: wsfulton
1116
[R] Add support for special variable replacement in the $typemap()
1217
special variable macro for R specific typemaps (rtype, rtypecheck,

Examples/test-suite/r/li_boost_shared_ptr_runme.R

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ testSuite <- function() {
8383
if (FALSE) {
8484
# pass by shared_ptr pointer reference
8585
k = Klass("me oh my")
86-
kret = smartpointerpointerreftest(k) # undefined class _p_p_SwigBoost__shared_ptrT_Space__Klass_t
86+
kret = smartpointerpointerreftest(k)
8787
val = kret$getValue()
8888
unittest("me oh my smartpointerpointerreftest", val)
8989
testSuite_verifyCount(2, k)
@@ -93,7 +93,7 @@ testSuite <- function() {
9393
if (FALSE) {
9494
# pass by shared_ptr pointer reference
9595
k = Klass("me oh my");
96-
kret = smartpointerpointerreftest(k); # undefined class _p_p_SwigBoost__shared_ptrT_Space__Klass_t
96+
kret = smartpointerpointerreftest(k);
9797
val = kret$getValue()
9898
unittest("me oh my smartpointerpointerreftest", val);
9999
testSuite_verifyCount(2, k);
@@ -104,10 +104,10 @@ testSuite <- function() {
104104
{
105105
k = Klass("me oh my");
106106
kret = constsmartpointertest(k);
107-
val = Klass_getValue(kret) # kret$getValue(); - method not defined
107+
val = Klass_getValue(kret);
108108
unittest("me oh my", val);
109109
testSuite_verifyCount(2, k);
110-
# testSuite_verifyCount(2, kret); # -> use_count not defined on _p_SwigBoost__shared_ptrT_Space__Klass_const_t
110+
testSuite_verifyCount(2, kret);
111111
}
112112

113113
# const pass by shared_ptr pointer
@@ -117,7 +117,7 @@ testSuite <- function() {
117117
val = Klass_getValue(kret)
118118
unittest("me oh my", val)
119119
testSuite_verifyCount(2, k)
120-
# testSuite_verifyCount(2, kret) # -> use_count not defined on _p_SwigBoost__shared_ptrT_Space__Klass_const_t
120+
testSuite_verifyCount(2, kret)
121121
}
122122

123123
# const pass by shared_ptr reference
@@ -127,7 +127,7 @@ testSuite <- function() {
127127
val = Klass_getValue(kret)
128128
unittest("me oh my", val)
129129
testSuite_verifyCount(2, k)
130-
# testSuite_verifyCount(2, kret) # -> use_count not defined for _p_SwigBoost__shared_ptrT_Space__Klass_const_t
130+
testSuite_verifyCount(2, kret)
131131
}
132132

133133
# pass by value
@@ -137,7 +137,7 @@ testSuite <- function() {
137137
val = kret$getValue();
138138
unittest("me oh my valuetest", val);
139139
testSuite_verifyCount(1, k);
140-
# testSuite_verifyCount(1, kret); # -> use_count not defined on _p_Space__Klass
140+
testSuite_verifyCount(1, kret);
141141
}
142142

143143
# pass by pointer
@@ -147,7 +147,7 @@ testSuite <- function() {
147147
val = kret$getValue();
148148
unittest("me oh my pointertest", val);
149149
testSuite_verifyCount(1, k);
150-
# testSuite_verifyCount(1, kret); # -> use_count not defined on _p_Space__Klass
150+
testSuite_verifyCount(1, kret);
151151
}
152152

153153
# pass by reference
@@ -157,17 +157,17 @@ testSuite <- function() {
157157
val = kret$getValue();
158158
unittest("me oh my reftest", val);
159159
testSuite_verifyCount(1, k);
160-
#testSuite_verifyCount(1, kret); # -> use_count not defined on _p_Space__Klass
160+
testSuite_verifyCount(1, kret);
161161
}
162162

163163
# pass by pointer reference
164164
{
165165
k = Klass("me oh my");
166-
kret = pointerreftest(k); # -> class not defined _p_p_Space__Klass
166+
kret = pointerreftest(k);
167167
val = kret$getValue();
168168
unittest("me oh my pointerreftest", val);
169169
testSuite_verifyCount(1, k);
170-
# testSuite_verifyCount(1, kret); # -> use_count not defined on _p_Space__Klass
170+
testSuite_verifyCount(1, kret);
171171
}
172172

173173
# null tests
@@ -239,7 +239,7 @@ testSuite <- function() {
239239
k = pointerownertest();
240240
val = k$getValue();
241241
unittest("pointerownertest", val);
242-
# testSuite_verifyCount(1, k); # -> use_count not defined for _p_Space__Klass
242+
testSuite_verifyCount(1, k);
243243
}
244244

245245
{
@@ -286,7 +286,7 @@ testSuite <- function() {
286286
# pass by shared_ptr pointer reference (mixed)
287287
if (FALSE) {
288288
k = KlassDerived("me oh my");
289-
kret = smartpointerpointerreftest(k); # undefined class _p_p_SwigBoost__shared_ptrT_Space__Klass_t
289+
kret = smartpointerpointerreftest(k);
290290
val = kret$getValue();
291291
unittest("me oh my derivedsmartptrpointerreftest-Derived", val);
292292
testSuite_verifyCount(2, k); # includes two extra references for upcasts in the proxy classes
@@ -313,7 +313,7 @@ testSuite <- function() {
313313
val = kret$getValue();
314314
unittest("me oh my derivedpointertest-Derived", val);
315315
testSuite_verifyCount(1, k);
316-
# testSuite_verifyCount(1, kret); -> use_count not defined for _p_Space__KlassDerived
316+
testSuite_verifyCount(1, kret);
317317
}
318318

319319
# pass by ref (mixed)
@@ -340,12 +340,12 @@ testSuite <- function() {
340340
unittest(overload_rawbyval(k), "rawbyval")
341341
unittest(overload_rawbyref(k), "rawbyref")
342342
unittest(overload_rawbyptr(k), "rawbyptr")
343-
# unittest(overload_rawbyptrref(k), "rawbyptrref") --> undefined for _p_SwigBoost__shared_ptrT_Space__Klass_t
343+
unittest(overload_rawbyptrref(k), "rawbyptrref")
344344

345345
unittest(overload_smartbyval(k), "smartbyval")
346346
unittest(overload_smartbyref(k), "smartbyref")
347347
unittest(overload_smartbyptr(k), "smartbyptr")
348-
# unittest(overload_smartbyptrref(k), "smartbyptrref") --> undefined for _p_SwigBoost__shared_ptrT_Space__Klass_t
348+
unittest(overload_smartbyptrref(k), "smartbyptrref")
349349
}
350350

351351
# Derived class
@@ -355,12 +355,12 @@ testSuite <- function() {
355355
unittest(overload_rawbyval(k), "rawbyval")
356356
unittest(overload_rawbyref(k), "rawbyref")
357357
unittest(overload_rawbyptr(k), "rawbyptr")
358-
# unittest(overload_rawbyptrref(k), "rawbyptrref") --> undefined for _p_SwigBoost__shared_ptrT_Space__KlassDerived_t
358+
unittest(overload_rawbyptrref(k), "rawbyptrref")
359359

360360
unittest(overload_smartbyval(k), "smartbyval")
361361
unittest(overload_smartbyref(k), "smartbyref")
362362
unittest(overload_smartbyptr(k), "smartbyptr")
363-
# unittest(overload_smartbyptrref(k), "smartbyptrref") --> undefined for _p_SwigBoost__shared_ptrT_Space__KlassDerived_t
363+
unittest(overload_smartbyptrref(k), "smartbyptrref")
364364
}
365365

366366
# 3rd derived class
@@ -370,12 +370,12 @@ testSuite <- function() {
370370
unittest(overload_rawbyval(k), "rawbyval")
371371
unittest(overload_rawbyref(k), "rawbyref")
372372
unittest(overload_rawbyptr(k), "rawbyptr")
373-
# unittest(overload_rawbyptrref(k), "rawbyptrref") --> undefined for _p_SwigBoost__shared_ptrT_Space__Klass3rdDerived_t
373+
unittest(overload_rawbyptrref(k), "rawbyptrref")
374374

375375
unittest(overload_smartbyval(k), "smartbyval")
376376
unittest(overload_smartbyref(k), "smartbyref")
377377
unittest(overload_smartbyptr(k), "smartbyptr")
378-
# unittest(overload_smartbyptrref(k), "smartbyptrref") --> undefined for _p_SwigBoost__shared_ptrT_Space__Klass3rdDerived_t
378+
unittest(overload_smartbyptrref(k), "smartbyptrref")
379379
}
380380

381381
#
@@ -573,7 +573,7 @@ testSuite <- function() {
573573
kglobal = GlobalValue_get();
574574
val = kglobal$getValue();
575575
unittest("global value", val);
576-
# testSuite_verifyCount(1, kglobal); # -> use_count undefined for _p_Space__Klass
576+
testSuite_verifyCount(1, kglobal);
577577
testSuite_verifyCount(1, k);
578578
unittest("global value", GlobalValue_get()$getValue());
579579

@@ -600,7 +600,7 @@ testSuite <- function() {
600600
kglobal = GlobalPointer_get();
601601
val = kglobal$getValue();
602602
unittest("global pointer", val);
603-
# testSuite_verifyCount(1, kglobal); -> use_count undefined for _p_Space__Klass
603+
testSuite_verifyCount(1, kglobal);
604604
testSuite_verifyCount(1, k);
605605
GlobalPointer_set(NULL);
606606
}
@@ -614,7 +614,7 @@ testSuite <- function() {
614614
kglobal = GlobalReference_get();
615615
val = kglobal$getValue();
616616
unittest("global reference", val);
617-
# testSuite_verifyCount(1, kglobal); # -> use_count undefined for _p_Space__Klass
617+
testSuite_verifyCount(1, kglobal);
618618
testSuite_verifyCount(1, k);
619619

620620
bNotCatched = F
@@ -652,16 +652,16 @@ for (i in 1:10) {
652652

653653
# wait for the GC to collect unused objects
654654

655-
for (i in 1:10) {
656-
invisible(gc(verbose = F, full = T))
657-
658-
if (Klass_getTotal_count() == 1) {
659-
break
660-
}
661-
662-
print(paste("Still waiting for GC to collect ", Klass_getTotal_count()-1, " objects, ", i))
663-
Sys.sleep(1)
664-
}
655+
#for (i in 1:10) {
656+
# invisible(gc(verbose = F, full = T))
657+
#
658+
# if (Klass_getTotal_count() == 1) {
659+
# break
660+
# }
661+
#
662+
# print(paste("Still waiting for GC to collect ", Klass_getTotal_count()-1, " objects, ", i))
663+
# Sys.sleep(1)
664+
#}
665665

666666
# Expect
667667
unittest(shared_ptr_wrapper_count(), NOT_COUNTING())

Lib/r/boost_shared_ptr.i

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,25 @@
394394
#error "typemaps for $1_type not available"
395395
%}
396396

397+
%typemap(rtype) SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE >,
398+
SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > &,
399+
SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *,
400+
SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *&
401+
"$typemap(rtype, TYPE)"
402+
403+
%typemap(scoercein) SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE >,
404+
SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > &,
405+
SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *,
406+
SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *&
407+
%{ if (inherits($input, "ExternalReference")) $input = slot($input,"ref"); %}
408+
409+
%typemap(scoerceout) SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE >,
410+
SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > &,
411+
SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *,
412+
SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE > *&
413+
%{ $result <- if (is.null($result)) $result
414+
else new("$typemap(rtype, TYPE)", ref=$result); %}
415+
397416

398417
%template() SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE >;
399418

Source/Modules/r.cxx

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2088,21 +2088,6 @@ int R::functionWrapper(Node *n) {
20882088
/*If the user gave us something to convert the result in */
20892089
if ((tm = Swig_typemap_lookup("scoerceout", n, Swig_cresult_name(), sfun))) {
20902090
Replaceall(tm,"$result","ans");
2091-
if (constructor) {
2092-
Node * parent = Getattr(n, "parentNode");
2093-
String * smartname = Getattr(parent, "feature:smartptr");
2094-
if (smartname) { // SmartName handling - has to be aligned to the other implementation in this file
2095-
SwigType *spt = Swig_cparse_type(smartname);
2096-
String *smart = SwigType_typedef_resolve_all(spt);
2097-
String *smart_rname = SwigType_manglestr(smart);
2098-
String *smart_rname_p = NewStringf("_p%s", smart_rname);
2099-
Replaceall(tm, "$R_class", smart_rname_p);
2100-
Delete(spt);
2101-
Delete(smart);
2102-
Delete(smart_rname);
2103-
Delete(smart_rname_p);
2104-
}
2105-
}
21062091
if (debugMode) {
21072092
Printf(stdout, "Calling replace B: %s, %s, %s\n", Getattr(n, "type"), Getattr(n, "sym:name"), getNSpace());
21082093
}
@@ -2328,31 +2313,6 @@ void R::registerClass(Node *n) {
23282313

23292314
Printf(s_classes, "setClass('%s', contains = %s)\n", sname, base);
23302315
Delete(base);
2331-
String *smartptr = Getattr(n, "feature:smartptr");
2332-
if (smartptr) {// SmartName handling - has to be aligned to the other implementation in this file
2333-
List *l = Getattr(n, "bases");
2334-
SwigType *spt = Swig_cparse_type(smartptr);
2335-
String *smart = SwigType_typedef_resolve_all(spt);
2336-
String *smart_rname = SwigType_manglestr(smart);
2337-
Printf(s_classes, "setClass('_p%s', contains = c('%s'", smart_rname, sname);
2338-
Delete(spt);
2339-
Delete(smart);
2340-
Delete(smart_rname);
2341-
for(int i = 0; i < Len(l); i++) {
2342-
Node * b = Getitem(l, i);
2343-
smartptr = Getattr(b, "feature:smartptr");
2344-
if (smartptr) {
2345-
spt = Swig_cparse_type(smartptr);
2346-
smart = SwigType_typedef_resolve_all(spt);
2347-
smart_rname = SwigType_manglestr(smart);
2348-
Printf(s_classes, ", '_p%s'", smart_rname);
2349-
Delete(spt);
2350-
Delete(smart);
2351-
Delete(smart_rname);
2352-
}
2353-
}
2354-
Printf(s_classes, "))\n");
2355-
}
23562316
}
23572317
}
23582318

0 commit comments

Comments
 (0)