Add PHP support for std::unique_ptr and std::auto_ptr

Equivalent to Python/Ruby implementations.
This commit is contained in:
William S Fulton 2022-08-06 18:34:26 +01:00
commit fa9c7a7197
7 changed files with 265 additions and 5 deletions

View file

@ -1,6 +1,6 @@
%module cpp11_std_unique_ptr
#if defined(SWIGCSHARP) || defined(SWIGJAVA) || defined(SWIGPYTHON) || defined(SWIGRUBY) || defined(SWIGPERL) || defined(SWIGTCL) || defined(SWIGOCTAVE) || defined(SWIGJAVASCRIPT) || defined(SWIGD)
#if defined(SWIGCSHARP) || defined(SWIGJAVA) || defined(SWIGPYTHON) || defined(SWIGRUBY) || defined(SWIGPERL) || defined(SWIGTCL) || defined(SWIGOCTAVE) || defined(SWIGJAVASCRIPT) || defined(SWIGD) || defined(SWIGPHP)
%include "std_string.i"
%include "std_unique_ptr.i"
@ -68,6 +68,10 @@ std::string takeKlassUniquePtr(std::unique_ptr<Klass> k) {
return s;
}
Klass *make_null() {
return nullptr;
}
bool is_nullptr(Klass *p) {
return p == nullptr;
}

View file

@ -12,7 +12,7 @@
#endif
%}
#if defined(SWIGCSHARP) || defined(SWIGJAVA) || defined(SWIGPYTHON) || defined(SWIGRUBY) || defined(SWIGPERL) || defined(SWIGTCL) || defined(SWIGOCTAVE) || defined(SWIGJAVASCRIPT) || defined(SWIGD)
#if defined(SWIGCSHARP) || defined(SWIGJAVA) || defined(SWIGPYTHON) || defined(SWIGRUBY) || defined(SWIGPERL) || defined(SWIGTCL) || defined(SWIGOCTAVE) || defined(SWIGJAVASCRIPT) || defined(SWIGD) || (SWIGPHP)
%include "std_string.i"
//#include <iostream>

View file

@ -0,0 +1,88 @@
<?php
require "tests.php";
function checkCount($expected_count) {
$actual_count = Klass::getTotal_count();
check::equal($actual_count, $expected_count, "Counts incorrect");
}
# Test raw pointer handling involving virtual inheritance
$kini = new KlassInheritance("KlassInheritanceInput");
checkCount(1);
$s = takeKlassUniquePtr($kini);
check::equal($s, "KlassInheritanceInput", "Incorrect string: $s");
$kini = NULL;
checkCount(0);
# unique_ptr as input
$kin = new Klass("KlassInput");
checkCount(1);
$s = takeKlassUniquePtr($kin);
checkCount(0);
check::equal($s, "KlassInput", "Incorrect string: $s");
try {
is_nullptr($kin);
check::fail("is_nullptr check");
} catch (TypeError $e) {
}
$kin = NULL; # Should not fail, even though already deleted
checkCount(0);
$kin = new Klass("KlassInput");
checkCount(1);
$s = takeKlassUniquePtr($kin);
checkCount(0);
check::equal($s, "KlassInput", "Incorrect string: $s");
try {
is_nullptr($kin);
check::fail("is_nullptr check");
} catch (TypeError $e) {
}
try {
} catch (TypeError $e) {
check::equal($e->getMessage(), "Cannot release ownership as memory is not owned for argument 1 of type 'Klass *' of takeKlassUniquePtr", "Unexpected exception: {$e->getMessage()}");
}
$kin = NULL; # Should not fail, even though already deleted
checkCount(0);
$kin = new Klass("KlassInput");
$notowned = get_not_owned_ptr($kin);
try {
takeKlassUniquePtr($notowned);
} catch (TypeError $e) {
check::equal($e->getMessage(), "Cannot release ownership as memory is not owned for argument 1 of type 'Klass *' of takeKlassUniquePtr", "Unexpected exception: {$e->getMessage()}");
}
checkCount(1);
$kin = NULL;
checkCount(0);
$kini = new KlassInheritance("KlassInheritanceInput");
checkCount(1);
$s = takeKlassUniquePtr($kini);
checkCount(0);
check::equal($s, "KlassInheritanceInput", "Incorrect string: $s");
try {
is_nullptr($kini);
check::fail("is_nullptr check");
} catch (TypeError $e) {
}
$kini = NULL; # Should not fail, even though already deleted
checkCount(0);
# unique_ptr as output
$k1 = makeKlassUniquePtr("first");
$k2 = makeKlassUniquePtr("second");
checkCount(2);
$k1 = NULL;
checkCount(1);
check::equal($k2->getLabel(), "second", "proper label");
$k2 = NULL;
checkCount(0);
check::done();

View file

@ -0,0 +1,88 @@
<?php
require "tests.php";
function checkCount($expected_count) {
$actual_count = Klass::getTotal_count();
check::equal($actual_count, $expected_count, "Counts incorrect");
}
# Test raw pointer handling involving virtual inheritance
$kini = new KlassInheritance("KlassInheritanceInput");
checkCount(1);
$s = takeKlassAutoPtr($kini);
check::equal($s, "KlassInheritanceInput", "Incorrect string: $s");
$kini = NULL;
checkCount(0);
# auto_ptr as input
$kin = new Klass("KlassInput");
checkCount(1);
$s = takeKlassAutoPtr($kin);
checkCount(0);
check::equal($s, "KlassInput", "Incorrect string: $s");
try {
is_nullptr($kin);
check::fail("is_nullptr check");
} catch (TypeError $e) {
}
$kin = NULL; # Should not fail, even though already deleted
checkCount(0);
$kin = new Klass("KlassInput");
checkCount(1);
$s = takeKlassAutoPtr($kin);
checkCount(0);
check::equal($s, "KlassInput", "Incorrect string: $s");
try {
is_nullptr($kin);
check::fail("is_nullptr check");
} catch (TypeError $e) {
}
try {
} catch (TypeError $e) {
check::equal($e->getMessage(), "Cannot release ownership as memory is not owned for argument 1 of type 'Klass *' of takeKlassAutoPtr", "Unexpected exception: {$e->getMessage()}");
}
$kin = NULL; # Should not fail, even though already deleted
checkCount(0);
$kin = new Klass("KlassInput");
$notowned = get_not_owned_ptr($kin);
try {
takeKlassAutoPtr($notowned);
} catch (TypeError $e) {
check::equal($e->getMessage(), "Cannot release ownership as memory is not owned for argument 1 of type 'Klass *' of takeKlassAutoPtr", "Unexpected exception: {$e->getMessage()}");
}
checkCount(1);
$kin = NULL;
checkCount(0);
$kini = new KlassInheritance("KlassInheritanceInput");
checkCount(1);
$s = takeKlassAutoPtr($kini);
checkCount(0);
check::equal($s, "KlassInheritanceInput", "Incorrect string: $s");
try {
is_nullptr($kini);
check::fail("is_nullptr check");
} catch (TypeError $e) {
}
$kini = NULL; # Should not fail, even though already deleted
checkCount(0);
# auto_ptr as output
$k1 = makeKlassAutoPtr("first");
$k2 = makeKlassAutoPtr("second");
checkCount(2);
$k1 = NULL;
checkCount(1);
check::equal($k2->getLabel(), "second", "proper label");
$k2 = NULL;
checkCount(0);
check::done();