diff options
author | Olly Betts <olly@survex.com> | 2023-04-21 17:43:32 +1200 |
---|---|---|
committer | Olly Betts <olly@survex.com> | 2023-04-21 17:50:25 +1200 |
commit | 6cd19ba92612dfca3ac0a66740050bf539263543 (patch) | |
tree | c8d311e721091e00da9bba570804f84e50b9116a /Examples | |
parent | 903b3d5ac3908ade77623144a5c33cbcbfe9ce59 (diff) | |
download | swig-6cd19ba92612dfca3ac0a66740050bf539263543.tar.gz |
[php] Support INPUT,INOUT,OUTPUT for std::string&
By default SWIG/PHP wraps std::string& as a pass-by-reference PHP
string parameter, but sometimes such a parameter is only for input
or only for output, so add support for the named typemaps that other
target languages support.
Diffstat (limited to 'Examples')
-rw-r--r-- | Examples/test-suite/li_std_string.i | 20 | ||||
-rw-r--r-- | Examples/test-suite/php/li_std_string_runme.php | 18 |
2 files changed, 36 insertions, 2 deletions
diff --git a/Examples/test-suite/li_std_string.i b/Examples/test-suite/li_std_string.i index 47a9090b7..209cf883f 100644 --- a/Examples/test-suite/li_std_string.i +++ b/Examples/test-suite/li_std_string.i @@ -1,9 +1,10 @@ %module li_std_string %include <std_string.i> -#if defined(SWIGUTL) +#if defined(SWIGPHP) || defined(SWIGUTL) %apply std::string& INPUT { std::string &input } %apply std::string& INOUT { std::string &inout } +%apply std::string& OUTPUT { std::string &output } #endif // throw is invalid in C++17 and later, only SWIG to use it @@ -53,13 +54,28 @@ std::string& test_reference_out() { } std::string test_reference_input(std::string &input) { - return input; + // For PHP to allow checking that we haven't used the default string& + // typemap which wraps as a PHP pass-by-reference string parameter. + std::string copy = input; + input = "MODIFIED"; + return copy; } void test_reference_inout(std::string &inout) { inout += inout; } +void test_reference_output(std::string &output) { + output = "output"; +} + +#ifdef SWIGPHP +// Test PHP-specific default wrapping string& as pass-by-ref PHP string. +void test_reference_php(std::string &s) { + s += ".php"; +} +#endif + void test_throw() TESTCASE_THROW1(std::string){ static std::string x = "test_throw message"; throw x; diff --git a/Examples/test-suite/php/li_std_string_runme.php b/Examples/test-suite/php/li_std_string_runme.php index 680b7ce98..169f947a7 100644 --- a/Examples/test-suite/php/li_std_string_runme.php +++ b/Examples/test-suite/php/li_std_string_runme.php @@ -49,6 +49,24 @@ Structure::StaticMemberString2($s); check::equal(Structure::StaticMemberString2(), $s, "StaticMemberString2 test 2"); check::equal(Structure::ConstStaticMemberString(), "const static member string", "ConstStaticMemberString test"); +// Test INPUT, INOUT and OUTPUT string& typemaps: +$input = "hello"; +check::equal(li_std_string::test_reference_input($input), "hello"); +// $input should be unchanged - this check is to catch if we incorrectly used +// the default string& typemap: +check::equal($input, "hello"); +$s = li_std_string::test_reference_inout($input); +check::equal($s, "hellohello"); +// $input should be unchanged - this check is to catch if we incorrectly used +// the default string& typemap: +check::equal($input, "hello"); +check::equal(li_std_string::test_reference_output(), "output"); + +// Test default PHP wrapping of std::string& as a by-ref PHP string parameter: +$s = "byref"; +check::equal(li_std_string::test_reference_php($s), null); +check::equal($s, "byref.php"); + // This used to give "Undefined variable: r" li_std_string::test_const_reference_returning_void("foo"); |