xHarbour Reference Documentation > Function Reference xHarbour Developers Network  

HB_ExecFromArray()

Executes a function, procedure or method indirectly.

Syntax

HB_ExecFromArray( <aExecutableArray> ) --> xResult

HB_ExecFromArray( <bBlock>|<cFuncName>|<pFuncPtr> ;
                [,<aParams>] ) --> xResult

HB_ExecFromArray( <oObject>, <cMethodName>|<pMethodPtr> ;
                [,<aParams>] ) --> xResult

Arguments

<aExecutableArray>
This is a one dimensional array whose first one or two elements contain executable data while all following elements are passed as parameters to the executable portion of the array. See details in the description.
<bBlock>
This is a code block to be executed. It receives the values stored in the array <aParams> as parameters, if specified.
<cFuncName>
This is a character string holding the symbolic name of a function or procedure to be executed. The values stored in the array <aParams> are passed as parameters, if specified.
<pFuncPtr>
This is a pointer to a function or procedure to be executed. It can be obtained using function HB_FuncPtr(). The values stored in the array <aParams> are passed as parameters, if specified.
<oObject>
If the first parameter is an object (Valtype()=="O"), the second parameter specifies the method to call for the object.
<cMethodName>
This is a character string holding the symbolic name of the method to be executed. The values stored in the array <aParams> are passed as parameters, if specified.
<pMethodPrt>
This is a pointer to a method to be executed. It can be obtained using function HB_ObjMsgPtr(). The values stored in the array <aParams> are passed as parameters to the method, if specified.
<aParams>
An optional, one dimensional array can be specified. Its elements are passed as parameters to a code block, function, method or procedure.

Return

The function returns the return value of the executed code.

Description

Function HB_ExecFromArray() is used for indirect execution of program code in xHarbour. The function can be called in different ways and accepts parameters of different data types, the first of which must contain an executable value, or item. Depending on the data type of an executable item, HB_ExecFromArray() uses different approaches for execution:

Paremeters and data types of executable items
1st param.2nd param.3rd param.Description
ArrayNILNILExecutable array is processed
Character string<aParams>NILMacro operator executes function or procedure
Code block<aParams>NILEval() executes code block
Pointer<aParams>NILHB_Exec() executes function call
ObjectCharacter string<aParams>Macro operator executes method of object
ObjectPointer<aParams>HB_Exec() executes method of object

The following notes explain how HB_ExecFromArray() works depending on the data type of the first parameter passed:

Array

When an array is passed as first parameter, it must be a so called "executable array". This is a one dimensional array whose first element contains an "executable item". An executable item can be of Valtype()=="C" (symbolic name of function or procedure to execute), Valtype()=="B" (code block to execute) or Valtype()=="P" (pointer to function or procedure to execute). The following elements of the executable array are passed as parameters to the executable item.

If the executable item is of Valtype()=="O" (object), the second element of the executable array must contain either a character string specifying the name of the method to call, or it must be a pointer to a method obtained with HB_ObjMsgPtr(). All other values stored in the third to the last element of the executable array are passed as parameters to the object's method.

Character string

When the first parameter is a character string, it must contain the symbolic name of a function or procedure which is visible at runtime. HB_ExecFromArray() uses the macro operator to call the corresponding function or procedure. The second parameter can be optionally a one dimensional array. Its elements are passed as parameters to the function or procedure.

Code block

When the first parameter is a code block, the optional second parameter can be a one dimensional array whose elements are passed as parameters to the code block. It is executed with the Eval() function.

Pointer

When the first parameter is a pointer, it must be obtained with the function-reference operator or the HB_FuncPtr() function. The optional second parameter <aParams> contains the parameters to pass to the corresponding function or procedure. It is executed with HB_Exec().

Object

When the first parameter is an object, the second parameter must be one of

1. character string containing the symbolic name of a method to execute

2. pointer to a method otained with HB_ObjMsgPtr()

The optional third parameter <aParams> contains the parameters to pass to the corresponding method.

Info

See also:@(), {|| }, {}, Array(), Eval(), HB_Exec(), HB_FuncPtr(), HB_ObjMsgPtr()
Category: Indirect execution , Pointer functions , xHarbour extensions
Source:vm\eval.c
LIB:xhb.lib
DLL:xhbdll.dll

Examples

// The example demonstrates various calls to HB_ExecFromArray() using
// an executable array, code block, function name and pointer.

   PROCEDURE Main
      LOCAL cFunc := "QOUT"
      LOCAL pFunc := HB_FuncPtr( cFunc )
      LOCAL aExec := { cFunc, "Hello", "World" }

      // executable array with function name
      HB_ExecFromArray( aExec )       // output: Hello World

      // function pointer and parameter array
      HB_ExecFromArray( pFunc, { "Hi", "there" } )
                                      // output: Hi there

      // function name and parameter array
      ? HB_ExecFromArray( "CDOW", { Date() } )
                                      // output: wednesday

      // executable array with code block and parameter
      bBlock := {|d| Year(d) }
      aExec := { bBlock, Date() }

      ? HB_ExecFromArray( aExec )     // output: 2006

   RETURN

 

// This example uses an object for HB_ExecFromArray() and
// demonstrates possibilities of calling a method.

   #include "hbclass.ch"

   PROCEDURE Main
      LOCAL pMethod, oObject
      LOCAL aExec, bBlock

      // function name and no parameters
      oObject := HB_ExecFromArray( "NumStat" )

      // executable array with object, method name and parameters
      aExec   := { oObject, "set", 1, 2, 3, 4, 5 }
      HB_ExecFromArray( aExec )

      // executable array with code block and parameter
      bBlock := {|o| o:total() }
      aExec  := { bBlock, oObject }

      ? HB_ExecFromArray( aExec )                // result: 15

      // executable array with object, method pointer and no parameters
      pMethod := HB_ObjMsgPtr( oObject, "average" )
      aExec  := { oObject, pMethod }

      ? HB_ExecFromArray( aExec )                // result:  3

      // code block and parameter array
      ? HB_ExecFromArray( bBlock, { oObject } )  // result: 15
   RETURN


   CLASS NumStat
      PROTECTED:
      DATA numbers
      DATA count

      EXPORTED:
      METHOD set
      METHOD average
      METHOD total
   ENDCLASS


   METHOD set( ... ) CLASS NumStat
      ::numbers := HB_AParams()
      ::count   := Len( ::numbers )
   RETURN self


   METHOD average CLASS NumStat
   RETURN ::total() / ::count


   METHOD total CLASS NumStat
      LOCAL nTotal := 0
      LOCAL nNumber

      FOR EACH nNumber IN ::numbers
         nTotal += nNumber
      NEXT
   RETURN nTotal

Copyright © 2006-2007 xHarbour.com Inc. All rights reserved.
http://www.xHarbour.com
Created by docmaker.exe