- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Greetings!
I need a container that holds multiple intrinsic types (real, character, etc.). Specfically, I'm looking to store an array of intrinsic type, along with some metadata, into a derived type. The intrinsic type array needs to be resizable and accessed from the container as I loop through the contents of a file that contains a mix of real, integer and character "values".
Something like:
type charvar
character(len=6) :: mnemonic
character(len=255) :: description
character(len=40) :: units
character(len=255), dimension(:), allocatable :: values
contains
procedure :: setMNEM
procedure :: getMNEM
...
procedure :: addValue
procedure :: getValues
end type charvar
type realvar
character(len=6) :: mnemonic
character(len=255) :: description
character(len=40) :: units
real, dimension(:), allocatable :: values
contains
procedure :: setMNEM
procedure :: getMNEM
...
procedure :: addValue
procedure :: getValues
end type realvar
Because I have a file that contains a mix of real and character "values", I need to store them in a container and then access the container at some later point.
What is the best way to approach this?
I have tried creating a superclass of 'variable', like:
type variable
character(len=6) :: mnemonic
character(len=255) :: description
character(len=40) :: units
contains
procedure :: setMNEM
procedure :: getMNEM
...
procedure(add_Value),deferred :: addValue
procedure(get_Value),deferred :: getValues
end type variable
abstract interface
subroutine add_Value(this, val)
import variable
class(variable) :: this
class(*),intent(in) :: val
end subroutine add_Valuesubroutine get_Value(this,val)
import variable
class(variable) :: this
class(*), allocatable, dimension(:),intent(inout) :: val
end subroutine get_Value
end interface
and then charvar and realvar extends variable and implements the deferred procedures, like this:
subroutine addValue(this, val)
class(realvar) :: this
real, allocatable, dimension(:) :: realarr
integer :: s
!real, intent(in) :: val
class(*), intent(in) :: valselect type (val)
type is (real)if (allocated(this%values) .eqv. .false.) then
allocate(this%values(1))
this%values(1) = val
else
allocate(realarr(size(this%values)+1))
realarr(1:size(this%values)) = this%values
call move_alloc(from=realarr,to=this%values)
s = size(this%values)
this%values(s) = val
endifend select
end subroutine addValuesubroutine getValues(this, val)
class(realvar) :: this
class(*), allocatable, dimension(:),intent(inout) :: val
if (allocated(this%values) .eqv. .true.) then
select type (val)
type is (real)
allocate(val(size(this%values)),source=this%values)
end select
else
stop 'No Values'
endif
end subroutine getValues
Then I create a container that holds the superclass
type :: container
class(variable), allocatable :: value
end type container
and in my main program make that congtainer allocatable
type(container), dimension(:), allocatable :: message_array
and then attempt to access (add/get) the values as such:
type(realvar) :: rv1
type(charvar) :: cv1real, dimension(:), allocatable :: real_vals
character(len=255), dimension(:), allocatable :: char_valsallocate(message_array(2))
rv1 = realvar('ER45GW')
cv1 = charvar('L0MR67')
allocate(message_array(1)%contents, source=rv1)
allocate(message_array(2)%contents, source=cv1)call message_array(1)%contents%addValue(-56.579)
call message_array(1)%contents%addValue(219.08)
call message_array(2)%contents%addValue('Yadda yadda yadda')
call message_array(2)%contents%addValue('Blah, blah, blah')
call message_array(1)%contents%getValues(real_vals)
call message_array(2)%contents%getValues(char_vals)
I have not yet had success with this (running 12.1.5), particularly getting the values back out.
SO BOTTOM LINE: is this best way to go about storing multiple instrinsic data types, or should I be barking up another tree? I've attemprted linked list but still haven't got it quite figured out either. If there is a more straightforward or elegant solution I would love to hear it! Any general guidance would be greatly appreciated!
-kevin.
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page