Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Mixed Language String Arrays

Intel_C_Intel
Employee
551 Views
This may turn out to be a bit of a C++ question but...
A while ago I established a method (with Jugoslav's help) of creating a SafeArray in C++, passing it to Fortran where it was populated with strings, and then returning to the C++ layer to extract/reference the strings.
I was wondering if there are any other more flexible string-array constructs available in C++ with which I could do the same trick? SafeArrayGetElement is a bit clumsy - "strings" would be more useful. Something like an STL ? (I don't know enough about STL to know if it is 'compatible' with CVF (6.6))
0 Kudos
2 Replies
Jugoslav_Dujic
Valued Contributor II
551 Views
The problem is that STL strings were not meant to be interoperable with other languages, while SafeArrays are. You could overcome the problem by writing a typedef/struct/class in C++, containing the stl string, and exposing its interfaces to the Fortran program, so that its contents are in effect opaque (but you still have to use those routines to manipulate the string in Fortran).

A rough sketch:
//C++=========
typedef std::array<:STRING> CStrArray;

extern "C" void StrAdd(CStrArray& ar, char* str)
{ ar.push_back(string(str));}

extern "C" void StrSet(CStrArray& ar, char* str, int index)
{ ar[index] = string(str);}

extern "C" void StrGet(CStrArray& ar, int index, char* str,
int strlen)
{ strncpy(ar[index].c_str(), str, strlen);}
...
CStrArray ar;
F90_Populate(&ar);

//Fortran====
subroutine F90_Populate(ar)

integer, parameter:: PSTRARR = INT_PTR_KIND()

integer(PSTRARR):: ar

interface
subroutine StrAdd(ar, str)
!DEC$ATTRIBUTES C, REFERENCE, ...
integer(PSTRARR):: ar
!DEC$ATTRIBUTES REFERENCE:: str
character(*):: str
end subroutine
end interface

call StrAdd(ar, "Foo"C)
call StrAdd(ar, "Bar"C)
0 Kudos
Intel_C_Intel
Employee
551 Views
Thanks Jugoslav...seems like safearrays might be easier after all!
0 Kudos
Reply