Mixed Language String Arrays

Mixed Language String Arrays

Community Admin's picture
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[i]" 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))
3 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.
Jugoslav Dujic's picture

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 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)
Jugoslav www.xeffort.com
Community Admin's picture

Thanks Jugoslav...seems like safearrays might be easier after all!

Login to leave a comment.