__QUERYPOINTER

Definition

This operator is not specified by the IEC 61131-3 standard.

At runtime, __QUERYPOINTER is assigning an interface reference to an untyped pointer. The operator returns a result with type BOOL. TRUE implies, that the conversion has been successfully executed.

Syntax

__QUERYPOINTER (<ITF_Source>, < Pointer_Dest>

For the first operand, the operator requires an interface reference or a function block instance of the intended type and for the second operand an untyped pointer. After execution of __QUERYPOINTER, the Pointer_Dest holds the address of the reference to the intended interface. In this case, the conversion is successful and the result of the operator returns TRUE. In all other cases, the operator returns FALSE. Pointer_Dest is untyped and can be cast to any type. The programmer has to ensure the actual type. For example, the interface could provide a method returning a type code.

A precondition for an explicit conversion is that the ITF_Source is an extension of the interface __System.IQueryInterface. This interface is provided implicitly and needs no library.

Example

TYPE KindOfFB
    (FB1 := 1, FB2 := 2, UNKOWN := -1);
END_TYPE
INTERFACE Itf EXTENDS __System.IQueryInterface
METHOD KindOf : KindOfFB
END_METHOD
FUNCTION_BLOCK F_BLOCK_1 IMPLEMENTS ITF
METHOD KindOf : KindOfFB
    KindOf := KindOfFB.FB1;
END_METHOD
FUNCTION_BLOCK F_BLOCK_2 IMPLEMENTS ITF
METHOD KindOf : KindOfFB
    KindOf := KindOfFB.FB2;
END_METHOD
FUNCTION CAST_TO_ANY_FB : BOOL
VAR_INPUT
    itf_in : Itf;
END_VAR
VAR_OUTPUT
    pfb_1: POINTER TO F_BLOCK_1 := 0;
    pfb_2: POINTER TO F_BLOCK_2 := 0;
END_VAR
VAR
    xResult1, xResult2 : BOOL;
END_VAR
IF itf_in <> 0
    CASE itf_in.KindOf OF
        KindOfFB.FB1:
            xResult1 := __QUERYPOINTER(itf_in, pfb_1);
        KindOfFB.FB2 THEN
            xResult2 := __QUERYPOINTER(itf_in, pfb_2);
    END_CASE
END_IF
CAST_TO_ANY_FB := xResult1 OR xResult2;