本文介绍了如何根据变量的数据类型指定要执行的过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个访问图像并读取像素值的模块。图像中的值通常是不同的数据类型( integer(2) integer(4),..)。 )。到目前为止,类型 image 的定义如下:

  type,public :: image 
logical :: initialized = .false。
字符(256):: path =!图片路径
integer :: dimensions(3)= -1!图片尺寸
integer :: datatype = 0!数据类型
包含
过程::初始化
过程:: pxvalues
过程:: getMeta
结束类型映像

现在我的问题是:程序是否有可能根据图像的数据类型自动找到相应的过程(存储在变量图像数据类型%)?例如。如果数据类型为整数,则在执行 image%pxvalues 时调用子程序 pxvalues_integer



谢谢!

解决方案

如果您不想更改代码的结构,那么bdforbes提出的SELECT CASE方法是一种选择。除此之外,您可以使用多态并用不同类型替换数据类型组件,这些类型都是同一父类型的所有扩展。这就是为什么键入绑定过程(类型定义中的contains之后的过程语句)存在的根本原因,因此您可以按照预期使用它们!

  type,public,abstract :: image 
!所有扩展的通用组件
logical :: initialized = .false。
字符(256):: path =!图片路径
integer :: dimensions(3)= -1! image
的尺寸包含
过程::初始化
!抽象接口pxvalues_image指定了什么接口必须
!看起来像。延迟属性意味着图像的扩展名
!必须为pxvalues绑定指定特定的过程。全部
!特定的程序必须具有相同的界面栏,通过
!论据。
过程(pxvalues_image),deferred :: pxvalues
过程:: getMeta
结束类型图像

抽象接口
子例程pxvalues_image(obj,... )
import :: image
class(image),intent(in):: obj
...
结束子程序pxvalues_image
结束界面

!具有整数2数据的图像的类型。
type,public,extends(image):: image_integer2
contains
procedure :: pxvalues => pxvalues_integer2
结束类型image_integer2

!具有整数4数据的图像的类型。
type,public,extends(image):: image_integer4
contains
procedure :: pxvalues => pxvalues_integer4
结束类型image_integer4

具体过程''pxvalues_integer2'',''pxvalues_integer4' '等等,然后接受一个扩展类型的初始参数。



与其设置数据类型组件,不如创建初始image对象的代码然后创建该对象作为图像的适当扩展,或许:

$ p $子程序create_image(object)
class(image) ,intent(out),allocatable :: object
...
!我们想要一个整数2的
分配的图像(image_integer2 :: object)

这种方法需要更多关于代码的知识,而不是单独的示例代码片段。


I'm writing a module that access images and reads pixel values. The values in the images are usually of different data types (integer(2), integer(4), ...). Up to now, type image is defined in the following way:

type, public :: image
  logical        :: initialized   = .false.
  character(256) :: path          = ""      ! Path to image
  integer        :: dimensions(3) = -1      ! Dimensions of image
  integer        :: datatype      = 0       ! Datatype
  contains
    procedure :: initialize
    procedure :: pxvalues
    procedure :: getMeta
end type image

My question now: Is there a possibility that the program automatically finds the corresponding procedures depending on the data type of the image (stored in variable image%datatype)? E.g. if the data type is integer, the subroutine pxvalues_integer is called during the execution of image%pxvalues.

Thanks!

解决方案

If you are unwilling to change the structure of your code, then the SELECT CASE approach proposed by bdforbes is an option.

Beyond that you may be able to use polymorphism and replace the datatype component by different types that are all extensions of the same parent type. This is the fundamental reason why type bound procedures (the procedure statements after the contains in the type definition) exist, so you might as well use them as intended!

type, public, abstract :: image
  ! Common components to all extensions
  logical        :: initialized   = .false.
  character(256) :: path          = ""      ! Path to image
  integer        :: dimensions(3) = -1      ! Dimensions of image
  contains
    procedure :: initialize
    ! Abstract interface pxvalues_image specified what the interface must
    ! look like.  Deferred attribute means that extensions of image
    ! must specify a specific procedure for the pxvalues binding.  All
    ! specific procedures must have the same interface bar the passed
    ! argument.
    procedure(pxvalues_image), deferred :: pxvalues
    procedure :: getMeta
end type image

abstract interface
  subroutine pxvalues_image(obj, ...)
    import :: image
    class(image), intent(in) :: obj
    ...
  end subroutine pxvalues_image
end interface

! A type for images that have integer2 data.
type, public, extends(image) :: image_integer2
  contains
    procedure :: pxvalues => pxvalues_integer2
end type image_integer2

! A type for images that have integer4 data.
type, public, extends(image) :: image_integer4
  contains
    procedure :: pxvalues => pxvalues_integer4
end type image_integer4

The specific procedures ''pxvalues_integer2'', ''pxvalues_integer4'', etc, then take an initial argument that is of the extension type.

Rather than setting the datatype component, the code that creates the initial "image" object should instead then create the object as the appropriate extension of image, perhaps:

subroutine create_image(object)
  class(image), intent(out), allocatable :: object
  ...
  ! We want an image that for integer2's
  allocate(image_integer2 :: object)

There are implication of this approach that require more knowledge of your code than can be given in isolated example snippets.

这篇关于如何根据变量的数据类型指定要执行的过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 04:53