-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
109 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |