-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathCptrWrapper.F90
64 lines (47 loc) · 1.64 KB
/
CptrWrapper.F90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
module ud2f_CptrWrapper
use, intrinsic :: iso_c_binding, only: c_ptr, C_NULL_PTR, c_associated
implicit none
private
public :: CptrWrapper
!================================ CPTRWRAPPER ==================================
! Base class to wrap type(c_ptr) instances used for udunits2 objects that cannot
! interface directly to fortran. Each extended class must provide a subroutine
! to free the memory associated with cptr_
type, abstract :: CptrWrapper
private
type(c_ptr) :: cptr_ = C_NULL_PTR
contains
procedure :: get_cptr
procedure :: set_cptr
procedure :: is_free
procedure :: free
procedure(I_free_memory), deferred :: free_memory
end type CptrWrapper
abstract interface
subroutine I_free_memory(this)
import :: CptrWrapper
class(CptrWrapper), intent(in) :: this
end subroutine I_Free_Memory
end interface
contains
type(c_ptr) function get_cptr(this)
class(CptrWrapper), intent(in) :: this
get_cptr = this%cptr_
end function get_cptr
subroutine set_cptr(this, cptr)
class(CptrWrapper), intent(inout) :: this
type(c_ptr), intent(in) :: cptr
this%cptr_ = cptr
end subroutine set_cptr
logical function is_free(this)
class(CptrWrapper), intent(in) :: this
is_free = .not. c_associated(this%cptr_)
end function is_free
! Free up memory pointed to by cptr_ and set cptr_ to c_null_ptr
subroutine free(this)
class(CptrWrapper), intent(inout) :: this
if(this%is_free()) return
call this%free_memory()
this%cptr_ = c_null_ptr
end subroutine free
end module ud2f_CptrWrapper