I am having a problem to read a union from DynamicData. The problem is to read the value of the discriminator so that the member
with the valid data can be identified. I can read all the values of the member fields and of course all but one have invalid data. How does one through
the TypeCode and DynamicData API get the value of the discriminator. The closest I can get to the discriminator is to get only the TypeCode with
discriminator_type(). Can someone point me to the relevant API functions to accomplish this or have a (Java) example to share ?
Thank you
Hello Nico,
I just checked and the use of the dynamic data API to navigate Union types is not clearly documented, so it is no wonder that you are having problems...
The basic idea is that at any point in time a Union type can hold only a single member. The member_id for this member is equal to the discriminator value. Therefore to get the value of the discriminator you must the operation get_member_info_by_index() on the DynamicData using the index value of 0.
The operation get_member_info_by_index () fills in a DynamicDataMemberInfo and accessing the member_id field within that DynamicDataMemberInfo gives you the value of the discriminator you are looking for.
Once you know the discriminator value you can use the proper-type version if the get_xxx() operation to access the member value.
Here is an example of the basic calls:
I also posted a Java working example on the File Exchange. You can find it at: http://community.rti.com/filedepot?fid=6
Gerardo
EDIT: Never mind, I've found the soultion now.
Hello,
We have a slight variation of the problem:
We need to use the case values to look up the correct member.
Imagine a variation of the IDL from the snippet:
The problem is that
SHORT_MEMBER_DISCRIMINATOR
andLONG_MEMBER_DISCRIMINATOR
are both mapped to the memberint_member
, and similarlyFLOAT_MEMBER_DISCRIMINATOR
andDOUBLE_MEMBER_DISCRIMINATOR
are both mapped to the memberflt_member
.Now, we have the union values in a text file with a syntax similar to JSON:
and we want to parse this file into DynamicData instances representing the union.
How should we go about this?
Thanks,
Oliver
Nevermind, we found a solution.
It involves the function
DDS_UnsignedLong find_member_by_label(DDS_Long label, DDS_ExceptionCode_t &)
Using the traditional C++ API, the steps (omitting the
DDS_ExceptionCode_t
argument at calls) are:evstr
) and given the TypeCode of the discriminant enum type (etc
), loopndx
from0
toetc->member_count() - 1
and compare theetc->member_name(ndx)
withevstr
; returnndx
on match.DDS_Long label = etc->member_ordinal(ndx)
.utc
) , set a member indexDDS_UnsignedLong mIndex = utc->find_member_by_label(label)
.DDS_TypeCode *branchTc = utc->member_type(mIndex)
.branchTc
TypeCode kind and decide which set function to callin the union's corresponding DynamicData object.
- Oliver