Mixed Language String Arrays

Mixed Language String Arrays

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.

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:

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;

subroutine F90_Populate(ar)

integer, parameter:: PSTRARR = INT_PTR_KIND()

integer(PSTRARR):: ar

subroutine StrAdd(ar, str)
integer(PSTRARR):: ar
character(*):: str
end subroutine
end interface

call StrAdd(ar, "Foo"C)
call StrAdd(ar, "Bar"C)

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

Leave a Comment

Please sign in to add a comment. Not a member? Join today