-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring_m.f90
83 lines (67 loc) · 2.36 KB
/
string_m.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
!
! Fortran string class
!
! Allows for assignment from character variables and the ability to get /
! set the internal character value.
!
module string_m
! Set defaults for class
implicit none
private
! Declare the type
type, public :: string_t
private
! Internal private character store
character(:), allocatable :: str_m
contains
private
! Getter / setter functions
procedure, public, pass(this) :: get_value => get_value_string_t
procedure, public, pass(this) :: set_value => set_value_string_t
! Assignment operator for other strings and characters
generic, public :: assignment (=) => string_t_assign_string_t, &
string_t_assign_character
! Procedure declaration for internal methods
procedure, private, pass(lhs) :: string_t_assign_string_t, &
string_t_assign_character
end type string_t
! Class constructor
interface string_t
module procedure string_t_constructor
end interface string_t
contains
!
! Constructor - initialize internal storage
!
type (string_t) function string_t_constructor()
string_t_constructor%str_m = ""
end function string_t_constructor
!
! Assignment - for other variables of type(string_t)
!
subroutine string_t_assign_string_t(lhs, rhs)
class (string_t), intent (inout) :: lhs
class (string_t), intent (in) :: rhs
lhs%str_m = rhs%str_m
end subroutine string_t_assign_string_t
!
! Assignment - for characters and character arrays
!
elemental subroutine string_t_assign_character(lhs, rhs)
class (string_t), intent (inout) :: lhs
character(len=*), intent (in) :: rhs
lhs%str_m = rhs
end subroutine string_t_assign_character
! Getter function
pure function get_value_string_t(this) result(res)
character (:), allocatable :: res
class (string_t), intent (in) :: this
res = this%str_m
end function get_value_string_t
! Setter subroutine
pure subroutine set_value_string_t(this, the_value)
class (string_t), intent (inout) :: this
character(len=*), intent (in) :: the_value
this%str_m = trim(the_value)
end subroutine set_value_string_t
end module string_m