Skip to content

Commit

Permalink
looking into class vs type
Browse files Browse the repository at this point in the history
  • Loading branch information
pletzer committed Dec 5, 2024
1 parent 9ed6423 commit f1819fd
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 5 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ enable_language(Fortran)

enable_testing()

add_subdirectory(class_vs_type)
add_subdirectory(polymorphism2)
add_subdirectory(class2)
add_subdirectory(generic_type_bound)
Expand Down
10 changes: 6 additions & 4 deletions class/test_myclass.f90
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ module myclass_mod

function myclass_new(n) result(this)
implicit none
type(myclass_type) :: this
integer, intent(in) :: n
type(myclass_type) :: this
integer, intent(in) :: n
allocate(this%arr(n))
ref_count = ref_count + 1
print *, 'constructor was called ', ref_count
print *, 'constructor was called ', ref_count, %LOC(this)
end function myclass_new

subroutine myclass_set(this, vals)
Expand All @@ -44,7 +44,8 @@ subroutine myclass_del(this)
type(myclass_type), intent(inout) :: this
integer :: ier
! some compilers have already deallocated this%arr
deallocate(this%arr, stat=ier)
if (allocated(this%arr)) deallocate(this%arr)

ref_count = ref_count - 1
print *, 'destructor was called ', ref_count
end subroutine myclass_del
Expand All @@ -64,6 +65,7 @@ subroutine test()

! call constructor
mc = myclass_type(n)
print *, %LOC(mc)

! call method
call mc%set(vals)
Expand Down
2 changes: 1 addition & 1 deletion class2/test_class2.f90
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module myclass_mod

subroutine myclass_init(this, n)
implicit none
class(myclass_type), intent(inout) :: this
class(myclass_type), intent(inout) :: this
integer, intent(in) :: n
allocate(this%arr(n))
print *, 'constructor was called'
Expand Down
22 changes: 22 additions & 0 deletions class_vs_type/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
include_directories(
${CMAKE_CURRENT_SOURCE_DIR}
)

set(LIB_FILES
test_class_vs_type.f90
)

set(HEADER_FILES
)

add_executable(test_class_vs_type test_class_vs_type.f90)

add_test(NAME test_class_vs_type
COMMAND test_class_vs_type)
set_tests_properties(test_class_vs_type PROPERTIES
PASS_REGULAR_EXPRESSION "destructor was called")


# Install headers
install(FILES ${HEADER_FILES} DESTINATION include)

79 changes: 79 additions & 0 deletions class_vs_type/test_class_vs_type.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
module myclass_mod

implicit none
type myclass_type
private
! members
integer, allocatable :: arr(:)
contains
! methods
procedure :: init => myclass_init
procedure :: set => myclass_set
procedure :: get => myclass_get
final :: myclass_del
end type myclass_type

contains

subroutine myclass_init(this, n)
implicit none
class(myclass_type), intent(inout) :: this
integer, intent(in) :: n
allocate(this%arr(n))
print *, 'constructor was called'
end subroutine myclass_init

subroutine myclass_set(this, vals)
implicit none
class(myclass_type), intent(inout) :: this
integer :: vals(:)
this%arr = vals
print *, 'set method was called'
end subroutine myclass_set

subroutine myclass_get(this, array)
class(myclass_type) :: this
integer, allocatable :: array(:)
allocate(array, source=this%arr)
print *, 'get method was called'
end subroutine myclass_get

subroutine myclass_del(this)
implicit none
type(myclass_type), intent(inout) :: this
integer :: ier
! some compilers have already deallocated this%arr
deallocate(this%arr, stat=ier)
print *, 'destructor was called'
end subroutine myclass_del

end module myclass_mod

subroutine test()
use myclass_mod
implicit none
type(myclass_type) :: mc
integer :: n
integer, allocatable :: vals(:), vals2(:)

n = 10
allocate(vals(n))
vals = 1

! call constructor
call mc%init(n)

! call method
call mc%set(vals)

call mc%get(vals2)

! mc will be destroyed when going out of scope
end subroutine

program test_myclass
implicit none
! moved code to a subroutine as destructor might not be called
! from program
call test
end program test_myclass

0 comments on commit f1819fd

Please sign in to comment.