Inheritance Runtime type info for downcasting

5 posts / 0 new
Last post
Offline
Last seen: 5 years 10 months ago
Joined: 02/24/2015
Posts: 7
Inheritance Runtime type info for downcasting

We have an IDL that uses inheritance.

 

Struct derivedclass1 : baseclass

{

//xxx

}

Struct derivedclass2 : baseclass

{

//xxx

}  

struct message

{    

   Baseclass field

}  

 

Using the ModernAPI, when we receive a message, how can we find out what the runtime type is for the field?  Before we cast to derivedclass1 or derivedclass2, we need to know which one it is.

 

I would assume that this runtime type info is available via xtypes, typecode, etc.

 

Thanks,

                -Ryan

Organization:
Offline
Last seen: 5 years 10 months ago
Joined: 01/17/2013
Posts: 23

Hi Ryan

Let me see if I understand your use case correctly.
You have 2 publishers, one of the publishing DerivedClass1 and the other DerivedClass2, and then a Subscriber of BaseClass.
For example:
struct baseclass{
long m1;
}

struct DerivedClass1 : baseclass{
long m2;
};

struct DerivedClass2 : baseclass{
string m3;
};

If that's the case, what it is going to happen in the subscriber side is that when it receives a sample either from
DerivedClass1 or DerivedClass2, it is going to receive only the baseclass part, i.e, the sample would only
include the information of m1, but it will discard the rest of the information (m2 or m3 depending who sent it).

You could cast the sample after that to DerivedClass1 or DerivedClass2 but it won't contain the information of m2 or m3,
it will have the default values for that fields.

Is it that your use case?
Best Regards,
Aida

Offline
Last seen: 3 days 16 hours ago
Joined: 04/02/2013
Posts: 195

As Aida explained, when you subscribe to a base type, you can communicate with publishers of derived types—but what you actually receive is a sample of the base type. Any fields that don't exist in the subscriber type are stripped out. Any fields that the subscriber type has, but the publisher type doesn't, get a default value (zero, empty string...).

 

 

Offline
Last seen: 5 years 10 months ago
Joined: 02/24/2015
Posts: 7

Aida & Alejandro,

Thanks for your replies.

No, we don't necessarily have 2 publishers.  We have an XSD (flowed down to us)  that we've converted to IDL.  The XSD uses polymophism.  one of the XSD complex types has a field of type AbstractBaseClass.

e.g.

struct message

{

     int x

     int y

     AbstractBaseClass x

}

Although Xtypes supports inheritance, were a derive class would inherit the base class fields, it does NOT support polymophic fields.   What is the typical workaround for this?  Some type of XSD choice or IDL uninon?  Maybe an additional enum/string to identifiy which type is being used?

Does RTI and/or OMG plan to enhance the current interitance/xtypes capabilities to add polymorphic support?

 

Thanks,

-Ryan

 

Offline
Last seen: 3 days 16 hours ago
Joined: 04/02/2013
Posts: 195

You can use optional members:

struct TypeWithOptionalMembers {

     string required_member;

     string optional_member; //@Optional

};

struct message {

    long x;

    long y;

    TypeWithOptionalMembers x;

};