How do I use keys when using a union?

In general, rtiddsgen only recognizes keys in the outermost structure. To consider the keys in embedded structures, the outer member needs to be a key as well.

In the example below, rtiddsgen will not recognize mykey as a key for union Astruct B, or struct C.

struct D { 
    long mykey; //@key
    long other;  
}; 

struct B { 
    D d; 
}; 

struct C { 
    D d; 
}; 

union A switch (long) { 
    case 0: 
        struct B b; 
    case 1: 
        struct C c; 
};

You can choose to leave B and C as unkeyed types or mark their member d as a key. In that case, mykey will be the key.

However, as per the DDS-XTypes standard, unions don't support keys. You can define a new struct that contains A and move mykey there. For example:

struct MyType { 
    long mykey; //@key 
    A a; 
}; 


If you must also keep mykey in D, then your application will have to assign MyType.mykey to a.b.d.mykey or  a.c.d.mykey (depending on the key discriminator of a).