Simulation Core
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CVoidEventHandlerCollection.f90 3.2 KiB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. module CVoidEventHandlerCollection
  2. use CVoidEventHandler
  3. implicit none
  4. public
  5. type, public :: VoidEventHandlerCollection
  6. type(VoidEventHandler), allocatable :: Delegates(:)
  7. contains
  8. procedure :: Length => Length
  9. procedure :: Add => Add
  10. procedure :: Remove => Remove
  11. procedure :: Empty => Empty
  12. procedure :: IsEmpty => IsEmpty
  13. procedure :: RunAll => RunAll
  14. end type VoidEventHandlerCollection
  15. contains
  16. integer function Length(this)
  17. implicit none
  18. class(VoidEventHandlerCollection), intent(in) :: this
  19. if(allocated(this%Delegates)) then
  20. Length = size(this%Delegates)
  21. return
  22. end if
  23. Length = 0
  24. end function
  25. subroutine Add(this, proc)
  26. implicit none
  27. class(VoidEventHandlerCollection), intent(inout) :: this
  28. type(VoidEventHandler), allocatable :: tempArr(:)
  29. procedure (ActionVoid), pointer, intent(in) :: proc
  30. integer :: i, isize
  31. if(allocated(this%Delegates)) then
  32. isize = size(this%Delegates)
  33. allocate(tempArr(isize+1))
  34. do i=1,isize
  35. tempArr(i) = this%Delegates(i)
  36. end do
  37. call tempArr(isize+1)%MakeNull()
  38. call tempArr(isize+1)%AssignTo(proc)
  39. deallocate(this%Delegates)
  40. call move_alloc(tempArr, this%Delegates)
  41. else
  42. allocate(this%Delegates(1))
  43. call this%Delegates(1)%MakeNull()
  44. call this%Delegates(1)%AssignTo(proc)
  45. end if
  46. end subroutine
  47. subroutine Remove(this, index)
  48. implicit none
  49. class(VoidEventHandlerCollection), intent(inout) :: this
  50. integer, intent(in) :: index
  51. type(VoidEventHandler), allocatable :: tempArr(:)
  52. integer :: i
  53. logical :: found
  54. if(index <= 0 .or. index > size(this%Delegates)) return
  55. if(.not.allocated(this%Delegates))return
  56. allocate(tempArr(size(this%Delegates)-1))
  57. found = .false.
  58. do i=1, size(this%Delegates)
  59. if(i==index) then
  60. found = .true.
  61. cycle
  62. end if
  63. if(found) then
  64. tempArr(i-1) = this%Delegates(i)
  65. else
  66. tempArr(i) = this%Delegates(i)
  67. endif
  68. end do
  69. deallocate(this%Delegates)
  70. call move_alloc(tempArr, this%Delegates)
  71. end subroutine
  72. subroutine Empty(this)
  73. implicit none
  74. class(VoidEventHandlerCollection), intent(inout) :: this
  75. if(allocated(this%Delegates)) deallocate(this%Delegates)
  76. end subroutine
  77. logical function IsEmpty(this)
  78. implicit none
  79. class(VoidEventHandlerCollection), intent(in) :: this
  80. IsEmpty = .not.allocated(this%Delegates)
  81. end function
  82. subroutine RunAll(this)
  83. implicit none
  84. class(VoidEventHandlerCollection), intent(inout) :: this
  85. integer :: i
  86. do i=1, size(this%Delegates)
  87. call this%Delegates(i)%Run()
  88. end do
  89. end subroutine
  90. end module CVoidEventHandlerCollection