CODESYS - the IEC 61131-3 automation software

Welcome to the official CODESYS Forum by 3S-Smart Software Solutions GmbH | A member of the CODESYS Group
Deutsche Version English version russian version 
It is currently Fri Oct 19, 2018 4:13 pm

All times are UTC+01:00




Post new topic  Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Check class relationship
PostPosted: Wed Aug 29, 2018 3:16 pm 
Offline

Joined: Thu Aug 23, 2018 8:42 am
Posts: 18
Hi everyone,

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

Something similar to issubclass() in Phyton.

Thank you in advance,
Omar


Top
   
PostPosted: Thu Aug 30, 2018 7:20 am 
Offline

Joined: Sat Aug 11, 2018 9:18 pm
Posts: 1
As far as I know there is no function as easy as issubclass().
But depending on what you are trying to do you might use operator ‘__QUERYPOINTER’.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:
Code:
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

Code:
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


Top
   
PostPosted: Thu Aug 30, 2018 11:09 am 
Offline

Joined: Thu Aug 23, 2018 8:42 am
Posts: 18
sorry I submitted this by accident.


Last edited by Omar Ampyx on Thu Aug 30, 2018 12:09 pm, edited 1 time in total.

Top
   
PostPosted: Thu Aug 30, 2018 11:31 am 
Offline

Joined: Thu Aug 23, 2018 8:42 am
Posts: 18
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


Top
   
PostPosted: Mon Sep 03, 2018 1:32 pm 
Offline

Joined: Thu Aug 23, 2018 8:42 am
Posts: 18
Hi there,

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

Regards,
Omar


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 5 posts ] 

All times are UTC+01:00


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Limited