forked from swig/swig
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #98 from swig-fortran/empty-arrays
Fix empty arrays and add reference-to-view
- Loading branch information
Showing
17 changed files
with
362 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
! File : fortran_std_span_runme.F90 | ||
|
||
#include "fassert.h" | ||
|
||
program fortran_std_span_runme | ||
implicit none | ||
|
||
call test_std_span | ||
|
||
contains | ||
subroutine test_std_span | ||
use fortran_std_span | ||
use ISO_C_BINDING | ||
implicit none | ||
integer(C_INT), dimension(:), pointer :: ptr => null() | ||
integer(C_INT), dimension(:), pointer :: local_ptr => null() | ||
integer(C_INT), dimension(3) :: expected = [1, 2, 3] | ||
integer(C_INT), dimension(:), allocatable, target :: values | ||
|
||
! Get pointer by value | ||
ptr => get_by_value() | ||
ASSERT(associated(ptr) .and. size(ptr) == 3) | ||
ASSERT(all(ptr == expected)) | ||
|
||
! Get by reference | ||
ptr => null() | ||
call get_by_reference(ptr) | ||
ASSERT(associated(ptr) .and. size(ptr) == 3) | ||
ASSERT(all(ptr == expected)) | ||
|
||
! Copy from Fortran pointer | ||
ptr => null() | ||
allocate(values(4), source = [5, 6, 7, 8]) | ||
local_ptr => values | ||
call copy(local_ptr, ptr) | ||
ASSERT(associated(ptr) .and. size(ptr) == 4) | ||
ASSERT(all(ptr == values)) | ||
|
||
! Copy directly from Fortran array | ||
ptr => null() | ||
call copy(values, ptr) | ||
ASSERT(associated(ptr) .and. size(ptr) == 4) | ||
ASSERT(all(ptr == values)) | ||
|
||
! Modify values | ||
ptr => values | ||
call increment_and_disassociate(ptr) | ||
ASSERT(values(4) == 9) | ||
ASSERT(.not. associated(ptr)) | ||
|
||
! Make one pointer point to another | ||
ptr => null() | ||
local_ptr => values | ||
ptr => const_ref(local_ptr) | ||
ASSERT(associated(ptr, local_ptr)) | ||
|
||
end subroutine | ||
|
||
end program | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
! File : li_std_vector_runme.f90 | ||
|
||
#include "fassert.h" | ||
|
||
program li_std_vector_runme | ||
use li_std_vector | ||
use ISO_C_BINDING | ||
|
||
call test_strings | ||
contains | ||
subroutine test_strings | ||
implicit none | ||
type(StringVector) :: strings!, reversed | ||
|
||
strings = StringVector() | ||
call strings%push_back("Sam") | ||
call strings%push_back("I am") | ||
ASSERT(strings%front() == "Sam") | ||
ASSERT(strings%back() == "I am") | ||
|
||
! XXX The typemaps assume the vector holds POD for views. | ||
|
||
! ! Since RevStringVec takes a string vector by const reference, we must provide it a view. | ||
! reversed = RevStringVec(strings%view()) | ||
! ! Note that vectors use Fortran indexing | ||
! ASSERT(strings%get(1) == "I am") | ||
! ASSERT(strings%get(2) == "Sam") | ||
end subroutine | ||
|
||
end program | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* File: fortran_std_span.i */ | ||
%module fortran_std_span | ||
|
||
%{ | ||
// Simplified span class | ||
namespace std { | ||
template<class _Tp, int _Ex = -1> | ||
class span | ||
{ | ||
public: | ||
typedef int index_type; | ||
typedef _Tp* pointer; | ||
|
||
span() : d_ptr(NULL), d_size(0) {} | ||
span(pointer d, index_type s) : d_ptr(d), d_size(s) {} | ||
span(pointer first, pointer last) : d_ptr(first), d_size(last - first) {} | ||
|
||
pointer data() const { return d_ptr; } | ||
index_type size() const { return d_size; } | ||
|
||
private: | ||
pointer d_ptr; | ||
index_type d_size; | ||
}; | ||
} // namespace std | ||
|
||
%} | ||
|
||
%include <std_span.i> | ||
|
||
%template() std::span<int>; | ||
%template() std::span<const int>; | ||
|
||
%inline %{ | ||
std::span<int> get_by_value() { | ||
static int tmp[] = {1, 2, 3}; | ||
return std::span<int>(tmp, tmp + 3); | ||
} | ||
|
||
void get_by_reference(std::span<int>& arr) { | ||
arr = get_by_value(); | ||
} | ||
|
||
void copy(std::span<const int> src, std::span<const int>& dst) { | ||
dst = src; | ||
} | ||
|
||
void increment_and_disassociate(std::span<int>& dst) { | ||
int* ptr = dst.data(); | ||
for (int i = 0; i < dst.size(); ++i) | ||
ptr[i]++; | ||
dst = std::span<int>(); | ||
} | ||
|
||
const std::span<const int>& const_ref(const std::span<const int>& src) { | ||
return src; | ||
} | ||
|
||
%} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.