Welcome to our new forum
All users of the legacy CODESYS Forums, please create a new account at account.codesys.com. But make sure to use the same E-Mail address as in the old Forum. Then your posts will be matched. Close

Check class relationship

Omar Ampyx
2018-08-29
2018-09-03
  • Omar Ampyx - 2018-08-29

    Hi everyone,

    Is there a function or way to check whether a class instance is a subclass of a class? Sorry for the alliteration

    Something similar to issubclass() in Phyton.

    Thank you in advance,
    Omar

     
  • alb123 - 2018-08-30

    As far as I know there is no function as easy as issubclass().
    https://help.codesys.com/webapp/_cds_operator_querypointer;product=codesys;version=3.5.13.0
    Using querypointer you have to know which sublass you are looking for.

    Your base class must implement __SYSTEM.IQueryInterface

    I put together the following test of this function a while a go in e!cockpit:

    First defined FB_animal, FB_Cat, and FB_Dog

    I was then able to test like this:

    METHOD MakeSound : STRING
    VAR_INPUT
    Β  Β pAnimal : POINTER TO FB_Animal;
    END_VAR
    VAR
    Β  Β pCat : POINTER TO FB_CAT;
    Β  Β pDog : POINTER TO FB_Dog;
    Β  Β sRespons : STRING;
    END_VAR
    IF __QUERYPOINTER (pAnimal^,pCat) THEN
    Β  Β MakeSound := pCat^.Mjau();
    END_IF
    IF __QUERYPOINTER(pAnimal^, pDog) THEN
    Β  Β MakeSound := pDog^.Bark();
    END_IF
    

    If you are testing if an instance of FB_Dog is a subclass of FB_Animal I believe you could define a POINTER TO FB_Animal and use __Querypointer. I have not tested this

    IF __QUERYPOINTER(fbDog, pAnimal) THEN
    Β  Β //It is possible to point to fbDog using POINTER TO FB_Animal. It must be a subclass of animals
    END_IF
    
     
  • Omar Ampyx - 2018-08-30

    sorry I submitted this by accident.

     
  • Omar Ampyx - 2018-08-30

    Dear alb123,

    Thanks for replying. I am confused with your second example, shouldn't it be the other way around like in the first example? My understanding is that it is allowed to point to a child class where a parent type is expected but not the other way around?

    I think your example is of no use to me, because in my case the pointer to be checked is not known. More specifically, the reference to be checked (which is initally defined as a reference to a base class) can be referenced to any child class at the implementation and the compiler will be happy about it:

    VAR
    rReferenceToBeChecked REFERENCE TO CLS_BaseClass
    END_VAR

    IMPLEMENTATION CASE 1
    rReferenceToBeChecked REF=clsInstanceOfBaseClass

    IMPLEMENTATION CASE 2
    rReferenceToBeChecked REF=clsInstanceOfChildClass

    At this point what I want is to ensure that the developer has supplied a child class (case 2) and generate an error if otherwise. That is why my intention is to confirm whether rReferenceToBeChecked is a subclass of a CLS_BaseClass.

    Thank you in advance,
    Omar

     
  • Omar Ampyx - 2018-09-03

    Hi there,

    I changed the approach so that I don't have this issue anymore.

    Regards,
    Omar

     

Log in to post a comment.