Advertisement
Guest User

oop_resource.f90

a guest
Sep 12th, 2021
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module streams
  2.  
  3.     type :: iostream
  4.         character(16), private :: name = 'null'
  5.     contains
  6.         final :: ios_close
  7.         procedure :: read => ios_read
  8.     end type
  9.     interface iostream
  10.         module procedure :: ios_open
  11.     end interface
  12.  
  13. contains
  14.  
  15.     function ios_open(name) result(self)
  16.         type(iostream) :: self
  17.         character(*), intent(in) :: name
  18.         self%name = name
  19.         write(*,*) "iostream open "//self%name
  20.     end function
  21.  
  22.     subroutine ios_close(self)
  23.         type(iostream) :: self
  24.         write(*,*) "iostream close "//self%name
  25.     end subroutine
  26.  
  27.     subroutine ios_read(self)
  28.         class(iostream), intent(in) :: self
  29.         write(*,*) "iostream read "//self%name
  30.     end subroutine
  31.  
  32. end module
  33.  
  34. program main
  35.     use streams
  36.     call test
  37. contains
  38.  
  39.     subroutine test
  40.         ! If this were in main directly, destructor of 'A' not required to fire.
  41.         type(iostream) :: s
  42.         s = iostream('A')
  43.         call do_stuff(iostream('B'))
  44.         call do_stuff(iostream('C'))
  45.     end subroutine
  46.  
  47.     subroutine do_stuff(s)
  48.         type(iostream), intent(in) :: s
  49.         call s%read()
  50.     end subroutine
  51.  
  52. end program
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement