FUTILS
Some basic Fortran Utilities
Loading...
Searching...
No Matches
enum_generator.f90
Go to the documentation of this file.
2#include "error_handling.h"
4 use strings_m
5
6 !> @page enum_generator_page Enum Generator
7 !> @ref index "< Back to main page"
8
9
10
11
12
13
14 private
15
16#define NAME_MAX_LEN 32
17
18 type, public, abstract :: enumerator_t
19 !private
20 integer :: id
21 character(len=NAME_MAX_LEN) :: name
22 logical :: set = .false.
23 contains
24 private
25 procedure, pass(lhs) :: is_equal
26 procedure, pass(lhs) :: not_equal
27 generic, public :: operator(==) => is_equal
28 generic, public :: operator(/=) => not_equal
29 procedure, public, pass(self) :: unset => unset_enumerator
30 procedure, public, pass(self) :: is_set => is_set_enumerator
31 procedure, public, pass(self) :: get_name => get_enumerator_name
32 end type enumerator_t
33
34 interface assignment(=)
35 module procedure assign_enumerator
36 end interface
37
38
39 ! How to create enums:
40 !
41 ! NEW_ENUM(<name>)
42 ! ENUMERATOR(<name>, <enumerator>)
43 ! END_ENUM(<name>)
44 !
45 ! This creates two derived types:
46 ! type( <name>_t )
47 ! type( <name>_enum_t )
48 !
49 ! and an instance of <name>_enum_t called <name>_enum.
50 ! The type <name>_enum_t contains all of the defined enumerators, which
51 ! are of type <name>_t.
52 ! The user can create their own instances of the type <name>_t, and set them to any of
53 ! the defined enumerators.
54 !
55 ! E.g.:
56 !
57 ! NEW_ENUM(fruit)
58 ! ENUMERATOR(fruit, apple)
59 ! ENUMERATOR(fruit, orange)
60 ! END_ENUM(fruit)
61 !
62 ! type(fruit_t) :: myfruit
63 ! myfruit = fruit_enum%apple
64 ! if(myfruit /= fruit_enum%orange) then ...
65 ! etc
66 !
67 ! The enumerators (e.g. fruit_t) are all extensions of the abstract type < enumerator_t >.
68 ! The enums (e.g. fruit_enum_t) are all extensions of the abstract type < enum_t >.
69
70
71contains
72
73 pure subroutine unset_enumerator(self)
74 implicit none
75 class(enumerator_t), intent(inout) :: self
76
77 self%set = .false.
78 end subroutine unset_enumerator
79
80 pure elemental logical function is_set_enumerator(self)
81 implicit none
82 class(enumerator_t), intent(in) :: self
83
84 is_set_enumerator = self%set
85
86 end function is_set_enumerator
87
88 pure elemental logical function is_equal(lhs, rhs)
89 implicit none
90 class(enumerator_t), intent(in) :: lhs, rhs
91
92 call error_handling%assert(same_type_as(lhs,rhs))
93 call error_handling%assert(lhs%set)
94 call error_handling%assert(rhs%set)
95
96 is_equal = (lhs%id == rhs%id)
97 end function is_equal
98
99 pure logical function not_equal(lhs, rhs)
100 implicit none
101 class(enumerator_t), intent(in) :: lhs, rhs
102
103 call error_handling%assert(same_type_as(lhs,rhs))
104 call error_handling%assert(lhs%set)
105 call error_handling%assert(rhs%set)
106
107 not_equal = (lhs%id /= rhs%id)
108 end function not_equal
109
110 subroutine assign_enumerator(lhs, rhs)
111 implicit none
112 class(enumerator_t), intent(out) :: lhs
113 class(enumerator_t), intent(in) :: rhs
114
115 if(.not.same_type_as(lhs,rhs)) then
116 call error_handling%abort('enumerator_t types not equivalent in setter subroutine')
117 endif
118
119 if(.not.rhs%set) then
120 call error_handling%abort('RHS enumerator_t type not set in comparison')
121 endif
122
123 lhs%id = rhs%id
124 lhs%set = .true.
125
126 end subroutine assign_enumerator
127
128 pure type(string_t) function get_enumerator_name(self) result(name)
129 implicit none
130 class(enumerator_t), intent(in) :: self
131
132 name = trim(adjustl(self%name))
133 end function get_enumerator_name
134
135end module enum_generator_m
pure elemental logical function is_set_enumerator(self)
subroutine assign_enumerator(lhs, rhs)
pure subroutine unset_enumerator(self)
pure elemental logical function is_equal(lhs, rhs)
pure type(string_t) function get_enumerator_name(self)
pure logical function not_equal(lhs, rhs)
type(error_handling_t), parameter, public error_handling