Re: Unlimited polymorphic pointers



Richard Maine wrote:
Joe <jkrahn@xxxxxxxxx> wrote:


You can make a generic linked list in F90, using the assumption that
non-array pointers are stored as a plain binary address.

...

In fact, instead of a "user data" pointer, you can use a "UNION" of key
types by defining multiple SEQUENCE types whose initial members are the
same,


Well... one could argue that the assumption and the "union" make the
code not exactly f90, though it is true that the odds of it working are
pretty good.
Doesn't the SEQUENCE attribute require that the data items in the binary structure be in the same order, at least up to the point where they deviate?

You can do things like this without that assumption also. I used to do
so in the early 90's. But the code required without the assumption is
awfully ugly and is highly prone to hit compiler bugs. I stopped using
that practice after a while because I was tired of debugging compilers.
You do things like use a variable-sized array (needs to be a pointer to
get the variable size in a derived type in f90) of anything - I used
integer. Then you use size(transfer(...)) to figure out how big you need
to allocate it. Oh yes, and you have to combine that with the usual
trick of making a derived type with a single pointer component.


I use a much safer approach that only transfers among derived types holding a single derived-type pointer, and I only allocate or deallocate in the context of the actual data type. This seems to be very robust. In fact, I suspect it will be less of a problem than the first F2003 implementations.

I use this to create a generic hierarchal database, with routines to read or write the whole thing to a file.

!----------------------------------------------------
! Define a generic un-typed key:
type list_key_unknown_type
sequence
integer :: type = 0
type(list_key_unknown_type), pointer :: p_next => NULL()
! type-specific data follows
end type list_key_unknown_type

type list_type
sequence
type(list_key_unknown_type), pointer :: first
end type list_type

!----------------------------------------------------
! Define a REAL, RANK3 type:
integer, parameter :: list_key_REAL_RANK3_typeid = 43
type list_key_REAL_RANK3_type
sequence
integer :: type
type(list_key_REAL_RANK3_type), pointer :: p_next
REAL, pointer, dimension(3) :: data
end type list_key_REAL_RANK3_type
type list_key_REAL_RANK3_ptr_type
sequence
type(list_key_REAL_RANK3_type), pointer :: p_key
end type list_key_REAL_RANK3_ptr_type

!----------------------------------------------------
! Example of using the above:
type(list_key_unknown_ptr_type) :: linked_list
type(list_key_REAL_RANK3_ptr_type) :: p_real_rank8_key

! Allocate a REAL, RANK8 key:
allocate(p_real_rank8_key%p_key)
allocate(p_real_rank8_key%p_key%typeid = list_key_REAL_RANK3_typeid
allocate(p_real_rank8_key%p_key%data(20,20,20))

! Insert this as a generic key into the first linked list element:
linked_list%first = transfer(p_real_rank8_key,linked_list%first)
.



Relevant Pages

  • Re: Arithmetic operation on derived type
    ... original post, because the member of my derived type is a pointer ... So here I defined a similar but separate type with sequence qualifier. ... doesn't work with pointer like I mentioned. ...
    (comp.lang.fortran)
  • Re: Rvalue of struct type
    ... the expression "funTest.a" yields an rvalue of type array of char, ... how can it be converted to pointer type when we can have pointer ... the next sequence point, or modifying it, results in undefined ...
    (comp.lang.c)
  • Re: Pointers to derived type objects in COMMON
    ... What if there is a SEQUENCE attribute? ... the pointer, only to its target. ... "A data pointer shall be storage associated only with data pointers ... "Data entities in different scoping units also have the same type ...
    (comp.lang.fortran)
  • Re: AfxBeginThread startup times and overhead
    ... If you want to pass in the sequence number, you can pass it in as ... The fact that the pointer value you dereference is complete ... One thing you can be certain of when you use Sleep() like this: ... >calling function sets and the thread clears, ...
    (microsoft.public.vc.mfc)
  • Re: doubt in USING POINTERS
    ... sequence point ... > undefined behaviour for some reason, it doesn't mean that the object's ... > valid pointer any more. ... > duration suitable for those temporary objects returned by a function. ...
    (comp.lang.c)