Skip to content

Commit

Permalink
Fix %apply typemap
Browse files Browse the repository at this point in the history
  • Loading branch information
sethrj committed Aug 22, 2023
1 parent f94cae6 commit 95905b0
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Doc/Manual/Fortran.html
Original file line number Diff line number Diff line change
Expand Up @@ -1471,7 +1471,7 @@ <H3><a name="Fortran_the_stdstring_class">25.5.1 The std::string class</a></H3>


<div class="code"><pre><code>%include &lt;std_string.i&gt;
%apply std::string { std::string&amp; }</code></pre></div>
%apply std::string const&amp; { std::string&amp; }</code></pre></div>

<H3><a name="Fortran_stdvector">25.5.2 std::vector</a></H3>

Expand Down
2 changes: 1 addition & 1 deletion Doc/Manual/src/Fortran.md
Original file line number Diff line number Diff line change
Expand Up @@ -1160,7 +1160,7 @@ like pass-by-value strings (where changes to the value in one language will
*not* make changes in the other), use `%apply`:
```swig
%include <std_string.i>
%apply std::string { std::string& }
%apply std::string const& { std::string& }
```

## std::vector
Expand Down
4 changes: 4 additions & 0 deletions Examples/fortran/std_string/example.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ std::string halved_str(std::string s);

std::string reversed_str(const std::string &input);

inline std::string reversed_str_mutable(std::string &input) {
return reversed_str(const_cast<const std::string &>(input));
}

3 changes: 3 additions & 0 deletions Examples/fortran/std_string/example.i
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
// Support native string conversions
%include <std_string.i>

// Demonstrate using a typemap to treat a mutable string as reference
%apply const std::string & { std::string& };

%include "example.h"


Expand Down
8 changes: 7 additions & 1 deletion Examples/fortran/std_string/runme.f90
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

program main
use, intrinsic :: ISO_C_BINDING
use example, only : print_str, halved_str, reversed_str
use example
use ISO_FORTRAN_ENV
implicit none
integer, parameter :: STDOUT = OUTPUT_UNIT
Expand Down Expand Up @@ -39,6 +39,12 @@ program main
tempstr = reversed_str(tempstr)

write(STDOUT, *) "'"//sampletext//"' -> '"//tempstr//"'"

write(STDOUT, *) "Reversed mutable"
tempstr = reversed_str_mutable(tempstr)

write(STDOUT, *) "double reversed: -> '"//tempstr//"'"

end program


Expand Down

0 comments on commit 95905b0

Please sign in to comment.