Re: Unlimited polymorphic pointers
- From: Joe Krahn <jkrahn@xxxxxxxxx>
- Date: Sun, 12 Feb 2006 16:45:33 GMT
Richard Maine wrote:
Joe <jkrahn@xxxxxxxxx> wrote: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 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.
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)
.
- Follow-Ups:
- Re: Unlimited polymorphic pointers
- From: Richard Maine
- Re: Unlimited polymorphic pointers
- References:
- Unlimited polymorphic pointers
- From: Rich Townsend
- Re: Unlimited polymorphic pointers
- From: Joe
- Re: Unlimited polymorphic pointers
- From: Richard Maine
- Unlimited polymorphic pointers
- Prev by Date: Re: Timeline for F2003 compilers
- Next by Date: Re: Open many data files
- Previous by thread: Re: Unlimited polymorphic pointers
- Next by thread: Re: Unlimited polymorphic pointers
- Index(es):
Relevant Pages
|