forked from swig/swig
-
Notifications
You must be signed in to change notification settings - Fork 12
Open
Labels
Description
Currently, instantiating and cleaning up classes is done in a very C++-like manner, with a constructor statement and a release statement. Assignment of proxy class objects generally acts like pointer association (=> in Fortran)
type(MyProxyClass) :: owned, referenced, also_referenced
owned = MyProxyClass(1, 2, 3) ! Construct a class
referenced = get_a_ref() ! Make 'referenced' point to some C++ data
referenced = owned ! Pointer assignment, doesn't change any datawhich prohibits assignment of class data; so the following can't be used to change the contents of the vector:
type(VectorString), intent(in) :: vec
type(String) :: vec_ref
vec_ref = vec%get_ref(0) ! get a reference to a C++ string
vec_ref = String("new string") ! change the reference to point to a new string, doesn't change `vec`'s dataIs there a way to allocate, assign, point to, and deallocate with SWIG proxy types? Can we make it at least as safe as the current method, and as safe as native Fortran?
For example, something like
type(MyProxyClass), allocatable :: owned
type(MyProxyClass), pointer :: referenced, also_referenced
allocate(owned, source=MyProxyClass(1, 2, 3)) ! Construct a class from fortran
referenced => get_a_ref() ! Get a proxy class pointer to C++ data
also_referenced => owned ! Make a Fortran pointer also reference the origin class
owned = referenced ! Do assignment on underlying C++ objects
dellocate(owned) ! optional??? if FINAL behaves correctly
! deallocate(referenced)??
! deallocate(also_referenced)??I don't think that native Fortran pointers are reference-counted, so I'm not sure how to safely obtain a C++ reference and make it appear as a Fortran pointer.
Reactions are currently unavailable