Deriving from structs with key fields

5 posts / 0 new
Last post
Offline
Last seen: 2 years 9 months ago
Joined: 02/15/2013
Posts: 20
Deriving from structs with key fields

Hi there,

I have a basetype

struct Base {

     ObjectId sourceID; //@key

     ...

}; //@top-level false //@Extensibility FINAL_EXTENSIBILITY

and derive an application type

struct Error : Base {

    ErrorCode code; //@key

    ...

}; //@Extensibility FINAL_EXTENSIBILITY  

From rtiddsgen I get the warning

"struct/valuetype derived from a struct/valuetype should not contain @key fields. This is not compliant with the XTypes specification."

I understand that and checked the XTypes spec, it is explicitly stated there.

But what is the recommended fix? Copy the fields from the base? Make the base a member of the derived? Or is there a more elegant way?

Best regards

Josef

Howard's picture
Offline
Last seen: 16 hours 11 min ago
Joined: 11/29/2012
Posts: 565

Instead of deriving, you can certainly include it as a member.

struct Error {       

    Base base;
    ErrorCode code; //@key    

    ... }; //@Extensibility FINAL_EXTENSIBILITY 

This will only use the "Error code" member as the key for the datatype Error.

If you want to also use as a key member what was defined in the Base class, the you should mark the Base element as a key.

NOTE:  "//@key" is the RTI proprietary method of annotating a key.  So are "//@top-level false //@Extensibility FINAL_EXTENSIBILITY".

Those were used before the X-Types standard came out to define OMG standard annotations for DDS.  Thus you should be using

@final
@nested
struct Base {

@key ObjectId sourceID;
     ...

};


@final
struct Error {       

@key Base base;

@key ErrorCode code;
    ...

}; 

 

Offline
Last seen: 2 years 9 months ago
Joined: 02/15/2013
Posts: 20

Thanks for the update on X-Types.

Just to make sure I have understood this right. When I include a structure which has key fields as a member then those key fields are no keys!

This means I have to mark every member explicitly as key for all structs used as topic types. Right?

But what if I only want certain members of Base as key fields and not all of them?

My Base contains a sourceID and a timestamp where only the sourceID is key, I definitely don't want a timestamp as key field!

So what does '@key Base base;' in Error mean?

a) All fields in Base are keys (and any @key in Base are ignored)

b) Only fields marked with @key in Base are keys

If it is a) then I would end up copying all fields from Base into Error...

In my opinion this is a step back in usability as the 'old' model of inheritance and inheriting key fields allowed nice data architectures. At least for a C++ developer :-) But maybe it was too easy to get it wrong or other languages had problems.

Howard's picture
Offline
Last seen: 16 hours 11 min ago
Joined: 11/29/2012
Posts: 565

So, we're just following the X-Types specification as issued by OMG.

When I include a structure which has key fields as a member then those key fields are no keys!

If you just include the structure as a member without annnotation, yes.

This means I have to mark every member explicitly as key for all structs used as topic types. Right?

 

Yes, for a datatype, if that datatype is keyed, you HAVE to define the datatype as keyed by annotating one or members as a key (@key).  It will NOT inherit the keys of an embedded member...unless you explicitly define it to.

It is option "B" as you wrote...if there is an @key in the definition of the embedded structure, then only the key members of the embedded structure are the keys for the higher level.  If the embedded structure doesn't have any key members defined, then using it as a key in the parent structure would make each member in the embedded structure a key.

Please see Table 3.11 in the Users Manual:

https://community.rti.com/static/documentation/connext-dds/6.1.0/doc/manuals/connext_dds_professional/users_manual/index.htm#users_manual/Using_BuiltinAnnotations.htm#3.3.9.1_The_@key_Annotation%3FTocPath%3DPart%25202%253A%2520Core%2520Concepts%7C3.%2520Data%2520Types%2520and%2520DDS%2520Data%2520Samples%7C3.3%2520Creating%2520User%2520Data%2520Types%2520with%2520IDL%7C3.3.9%2520Using%2520Builtin%2520Annotations%7C_____1

Offline
Last seen: 2 years 9 months ago
Joined: 02/15/2013
Posts: 20

Howard, thank you for the clarifications.

Josef