Skip to main content

Operatore: __QUERYINTERFACE

L'operatore è un'estensione della norma IEC 61131-3.

In fase di esecuzione, l'operatore esegue una conversione di tipo di un riferimento di interfaccia in un altro tipo. L'operatore restituisce a BOOL risultato. TRUE significa che CODESYS ha eseguito correttamente la conversione.

__QUERYINTERFACE(<ITF_Source>,<ITF_Dest>);

1° operando: Riferimento interfaccia o interfaccia FB

2° operando: riferimento interfaccia con tipo di destinazione richiesto

Il requisito per la conversione esplicita è che siano derivati sia ITF_Source che ITF_Dest Interface __System.IQueryInterface. Questa interfaccia è implicitamente disponibile non richiede una libreria.

Esempio 151. Esempio
INTERFACE ItfBase EXTENDS __System.IQueryInterface
METHOD mbase : BOOL
END_METHOD

INTERFACE ItfDerived1 EXTENDS ItfBase
METHOD mderived1 : BOOL
END_METHOD

INTERFACE ItfDerived2 EXTENDS ItfBase
METHOD mderived2 : BOOL
END_METHOD

FUNCTION_BLOCK FB1 IMPLEMENTS ItfDerived1
METHOD mbase : BOOL
    mbase := TRUE;
END_METHOD
METHOD mderived1 : BOOL
    mderived1 := TRUE;
END_METHOD
END_FUNCTION_BLOCK

FUNCTION_BLOCK FB2 IMPLEMENTS ItfDerived2
METHOD mbase : BOOL
    mbase := FALSE;
END_METHOD
METHOD mderived2 : BOOL
    mderived2 := TRUE;
END_METHOD
END_FUNCTION_BLOCK

PROGRAMM POU
VAR
    inst1 : FB1;
    inst2 : FB2;
    itfbase1 : ItfBase := inst1;
    itfbase2 : ItfBase := inst2;
    itfderived1 : ItfDerived1 := 0;
    itfderived2 : ItfDerived2 := 0;
    xResult1, xResult2, xResult3, xResult4: BOOL;
END_VAR


xResult1 := __QUERYINTERFACE(itfbase1, itfderived1); // xResult = TRUE, itfderivedi1 <>0
                                                     // references the instance inst1
xResult2 := __QUERYINTERFACE(itfbase1, itfderived2); // xResult = FALSE, itfderived2 = 0
xResult3 := __QUERYINTERFACE(itfbase2, itfderived1); // xResult = FALSE, itfderived1 = 0
xResult4 := __QUERYINTERFACE(itfbase2, itfderived2); // xResult = TRUE, itfderived2 <> 0
                                                     // references the instance inst2