Skip to content

Commit

Permalink
Second implementation of abstract class added
Browse files Browse the repository at this point in the history
  • Loading branch information
krystophny committed Nov 28, 2024
1 parent 4c0b215 commit 74375b8
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 51 deletions.
10 changes: 5 additions & 5 deletions examples/issue41_abstract_classes/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ all: wrapper test_abstract_classes.x
test: wrapper
$(PYTHON) run.py

test_abstract_classes.x: main.f90 myclass_base.o myclass_impl.o myclass_factory.o
test_abstract_classes.x: main.f90 myclass_base.o myclass_impl.o myclass_impl2.o myclass_factory.o
$(FC) $(FCFLAGS) -o $@ $^

wrapper: myclass_base.o myclass_impl.o myclass_factory.o
f90wrap -m itest -P myclass_base.f90 myclass_impl.f90 myclass_factory.f90 -v
wrapper: myclass_base.o myclass_impl.o myclass_impl2.o myclass_factory.o
f90wrap -m itest -P myclass_base.f90 myclass_impl.f90 myclass_impl2.f90 myclass_factory.f90 -v
f2py-f90wrap --build-dir . -c -m _itest --opt="-O0 -g" \
f90wrap_myclass_base.f90 f90wrap_myclass_impl.f90 f90wrap_myclass_factory.f90 \
myclass_base.o myclass_impl.o myclass_factory.o
f90wrap_myclass_base.f90 f90wrap_myclass_impl.f90 f90wrap_myclass_impl2.f90 f90wrap_myclass_factory.f90 \
myclass_base.o myclass_impl.o myclass_impl2.o myclass_factory.o

%.o : %.f90
$(FC) $(FCFLAGS) -c -g -O0 $< -o $@
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
! Module myclass_factory defined in file myclass_factory.f90

subroutine f90wrap_myclass_factory__create_myclass(ret_myobject, impl_type)
use myclass_base, only: myclass_t
use myclass_factory, only: create_myclass
use myclass_base, only: myclass_t
implicit none

type myclass_t_wrapper_type
Expand Down
38 changes: 19 additions & 19 deletions examples/issue41_abstract_classes/f90wrap_myclass_impl.f90
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
! Module myclass_impl defined in file myclass_impl.f90

subroutine f90wrap_myclass_impl__myclass_impl_finalise__binding__mycla4a60(self)
use myclass_impl, only: myclass_impl_finalise, myclass_impl_t
subroutine f90wrap_myclass_impl__myclass_impl_destroy__binding__myclas021a(self)
use myclass_impl, only: myclass_impl_destroy, myclass_impl_t
implicit none

type myclass_impl_t_wrapper_type
Expand All @@ -14,23 +14,7 @@ subroutine f90wrap_myclass_impl__myclass_impl_finalise__binding__mycla4a60(self)
integer, intent(in), dimension(2) :: self
self_ptr = transfer(self, self_ptr)
deallocate(self_ptr%p)
end subroutine f90wrap_myclass_impl__myclass_impl_finalise__binding__mycla4a60

subroutine f90wrap_myclass_impl__myclass_impl_finalise(self)
use myclass_impl, only: myclass_impl_finalise, myclass_impl_t
implicit none

type myclass_impl_t_wrapper_type
class(myclass_impl_t), allocatable :: obj
end type myclass_impl_t_wrapper_type
type myclass_impl_t_ptr_type
type(myclass_impl_t_wrapper_type), pointer :: p => NULL()
end type myclass_impl_t_ptr_type
type(myclass_impl_t_ptr_type) :: self_ptr
integer, intent(in), dimension(2) :: self
self_ptr = transfer(self, self_ptr)
deallocate(self_ptr%p)
end subroutine f90wrap_myclass_impl__myclass_impl_finalise
end subroutine f90wrap_myclass_impl__myclass_impl_destroy__binding__myclas021a

subroutine f90wrap_myclass_impl__myclass_impl_t_initialise(this)
use myclass_impl, only: myclass_impl_t
Expand Down Expand Up @@ -83,5 +67,21 @@ subroutine f90wrap_myclass_impl__get_value_impl(self, value)
call get_value_impl(self=self_ptr%p%obj, value=value)
end subroutine f90wrap_myclass_impl__get_value_impl

subroutine f90wrap_myclass_impl__myclass_impl_destroy(self)
use myclass_impl, only: myclass_impl_destroy, myclass_impl_t
implicit none

type myclass_impl_t_wrapper_type
class(myclass_impl_t), allocatable :: obj
end type myclass_impl_t_wrapper_type
type myclass_impl_t_ptr_type
type(myclass_impl_t_wrapper_type), pointer :: p => NULL()
end type myclass_impl_t_ptr_type
type(myclass_impl_t_ptr_type) :: self_ptr
integer, intent(in), dimension(2) :: self
self_ptr = transfer(self, self_ptr)
call myclass_impl_destroy(self=self_ptr%p%obj)
end subroutine f90wrap_myclass_impl__myclass_impl_destroy

! End of module myclass_impl defined in file myclass_impl.f90

1 change: 1 addition & 0 deletions examples/issue41_abstract_classes/itest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
import numpy
import itest.myclass_factory
import itest.myclass_base
import itest.myclass_impl2
import itest.myclass_impl

4 changes: 2 additions & 2 deletions examples/issue41_abstract_classes/itest/myclass_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Module myclass_factory
Defined at myclass_factory.f90 lines 1-15
Defined at myclass_factory.f90 lines 1-18
"""
from __future__ import print_function, absolute_import, division
Expand All @@ -19,7 +19,7 @@ def create_myclass(impl_type):
myobject = create_myclass(impl_type)
Defined at myclass_factory.f90 lines 6-15
Defined at myclass_factory.f90 lines 7-18
Parameters
----------
Expand Down
31 changes: 15 additions & 16 deletions examples/issue41_abstract_classes/itest/myclass_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,7 @@ def __del__(self):
"""
if self._alloc:
_itest.f90wrap_myclass_impl__myclass_impl_finalise__binding__mycla4a60(self=self._handle)

def __del__(self):
"""
Destructor for class Myclass_Impl_T
Defined at myclass_impl.f90 lines 15-17
Parameters
----------
self : Myclass_Impl_T
"""
if self._alloc:
_itest.f90wrap_myclass_impl__myclass_impl_finalise(self=self._handle)
_itest.f90wrap_myclass_impl__myclass_impl_destroy__binding__myclas021a(self=self._handle)

def __init__(self, handle=None):
"""
Expand Down Expand Up @@ -115,6 +100,20 @@ def get_value_impl(self):
value = _itest.f90wrap_myclass_impl__get_value_impl(self=self._handle)
return value

def myclass_impl_destroy(self):
"""
myclass_impl_destroy(self)
Defined at myclass_impl.f90 lines 15-17
Parameters
----------
self : Myclass_Impl_T
"""
_itest.f90wrap_myclass_impl__myclass_impl_destroy(self=self._handle)


_array_initialisers = []
_dt_array_initialisers = []
Expand Down
3 changes: 3 additions & 0 deletions examples/issue41_abstract_classes/myclass_factory.f90
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module myclass_factory

use myclass_base, only: myclass_t
use myclass_impl, only: myclass_impl_t
use myclass_impl2, only: myclass_impl2_t
implicit none

contains
Expand All @@ -14,6 +15,8 @@ function create_myclass(impl_type) result(myobject)
select case(impl_type)
case("impl")
allocate(myclass_impl_t :: myobject)
case("impl2")
allocate(myclass_impl2_t :: myobject)
case default
print *, "create_field_can: Unknown implementation: ", impl_type
error stop
Expand Down
6 changes: 3 additions & 3 deletions examples/issue41_abstract_classes/myclass_impl.f90
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module myclass_impl
type, extends(myclass_t) :: myclass_impl_t
contains
procedure :: get_value => get_value_impl
final :: myclass_impl_finalise
final :: myclass_impl_destroy
end type myclass_impl_t

contains
Expand All @@ -18,10 +18,10 @@ subroutine get_value_impl(self, value)
value = 1.0
end subroutine get_value_impl

subroutine myclass_impl_finalise(self)
subroutine myclass_impl_destroy(self)
type(myclass_impl_t), intent(inout) :: self

print *, "Finalising myclass_impl_t"
end subroutine myclass_impl_finalise
end subroutine myclass_impl_destroy

end module myclass_impl
27 changes: 27 additions & 0 deletions examples/issue41_abstract_classes/myclass_impl2.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module myclass_impl2

use myclass_base, only: myclass_t
implicit none

type, extends(myclass_t) :: myclass_impl2_t
contains
procedure :: get_value => get_value_impl2
final :: myclass_impl2_destroy
end type myclass_impl2_t

contains

subroutine get_value_impl2(self, value)
class(myclass_impl2_t), intent(in) :: self
real, intent(out) :: value

value = 2.0
end subroutine get_value_impl2

subroutine myclass_impl2_destroy(self)
type(myclass_impl2_t), intent(inout) :: self

print *, "Finalising myclass_impl2_t"
end subroutine myclass_impl2_destroy

end module myclass_impl2
13 changes: 8 additions & 5 deletions examples/issue41_abstract_classes/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@
import itest

REF = 1.0
TOL = 1.0e-14
TOL = 1.0e-6

obj = itest.myclass_factory.create_myclass("impl")

output = obj.get_value()
assert(abs(output-REF)<TOL)
print(f"OK: {output} == {REF}")

# obj2 = itest.myclass_factory.create_myclass("impl")
# output2 = obj2.get_value()
# assert(abs(output-REF)<TOL)
# print(f"OK: {output2} == {REF}")
del obj

REF2 = 2.0
obj2 = itest.myclass_factory.create_myclass("impl2")
output2 = obj2.get_value()
assert(abs(output-REF)<TOL)
print(f"OK: {output2} == {REF2}")

0 comments on commit 74375b8

Please sign in to comment.