Call by name (in a variable) 
Author Message
 Call by name (in a variable)

Hello,

I would like to call a procedure by using its name, which is stored in
a character variable, i.e., I'd like to do something like this:

MY_FUNC = "SOME_FUNCTION"
CALL $MYFUNC(Arg1,Arg2)

where $MY_FUNC should be replaced by its value, SOME_FUNCTION in the
above case. It would be similar to CALL SYSTEM(MY_FUNC), but it would
all remain inside the same program (no shell escape). Is this even
possible?

Could it be done in some different way, like providing a pointer to
SOME_FUNCTION instead of a variable with its name? I'm afraid passing
procedures as arguments is not flexible enough for my needs...

Thanks



Fri, 19 Sep 2008 20:58:31 GMT  
 Call by name (in a variable)
Well, in general, no. fortran is compiled language, and in general,
call references must be resolved in link time.

On the other hand, Windows offers somewhat similar mechanism for
working with dlls. Maybe Unix has something equivalent, but I'm not
sufficiently versatile with it. Windows API LoadLibrary loads a dll
identified by filename within process' address space, and
GetProcAddress returns the address of a routine identified by exported
name. The only thing to do is to dereference the pointer to the routine
(which, again, is not the easiest thing to do in Fortran, as it's not
portable) and call it.

So, here's non-standard code in Visual Fortran extensions, which calls
2 routines of different name but the same prototype from one dll:

use DFWIN

integer:: hDll
interface
  subroutine Any(k)
  integer k
  end subroutine Any
end interface
pointer (pAny, Any)

hDll = LoadLibrary("MyDll.dll"//char(0))
if (hDll.ne.0) then
   pAny = GetProcAddress(hDll, "_ROUTINE1")
   if (pAny.ne.0) call Any(42)
   pAny = GetProcAddress(hDll, "_ANOTHERROUTINE1")
   if (pAny.ne.0) call Any(11)
end if

--
Jugoslav
http://www.xeffort.com



Sat, 20 Sep 2008 01:55:51 GMT  
 Call by name (in a variable)

Quote:

> Well, in general, no. Fortran is compiled language, and in general,
> call references must be resolved in link time.

> On the other hand, Windows offers somewhat similar mechanism for
> working with dlls. Maybe Unix has something equivalent, but I'm not
> sufficiently versatile with it. Windows API LoadLibrary loads a dll
> identified by filename within process' address space, and
> GetProcAddress returns the address of a routine identified by exported
> name. The only thing to do is to dereference the pointer to the routine
> (which, again, is not the easiest thing to do in Fortran, as it's not
> portable) and call it.

> So, here's non-standard code in Visual Fortran extensions, which calls
> 2 routines of different name but the same prototype from one dll:

> use DFWIN

> integer:: hDll
> interface
>   subroutine Any(k)
>   integer k
>   end subroutine Any
> end interface
> pointer (pAny, Any)

> hDll = LoadLibrary("MyDll.dll"//char(0))
> if (hDll.ne.0) then
>    pAny = GetProcAddress(hDll, "_ROUTINE1")
>    if (pAny.ne.0) call Any(42)
>    pAny = GetProcAddress(hDll, "_ANOTHERROUTINE1")
>    if (pAny.ne.0) call Any(11)
> end if

> --
> Jugoslav
> http://www.xeffort.com

POSIX systems have a similar C library functions called dlopen() and
dlsym(). You will have to deal with interfacing the C calls yourself. In
both cases, you need to know what your compiler uses for symbol names.

Also, standard Fortran does not have procedure-pointer variables until
F2003. Up to F95, you can reference a procedure name, but it is like a
constant.

One portable way to do it is to build a list of functions that you want
callable by name, and create a procedure-calling subroutine with
something like:

select case(sub_name)
case ("abort")
   call abort(arg1)
...
end select

I also assume that all procedures have identical argument lists. If not,
  you have an even bigger challenge.

Joe



Sun, 21 Sep 2008 05:08:56 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Using a variable name for a variable name??

2. Calling a procedure with a variable procedure name

3. call procedure named in a variable?

4. how to call a functino through a variable name

5. calling a method using a variable name?

6. field name and key name as a variable

7. field name and key name as a variable

8. variable contents to variable name

9. array name in variable variable

10. getting variable name from the variable itself?

11. Variables containing variable names

12. name of a variable stored in another variable

 

 
Powered by phpBB® Forum Software