Skip to content

Commit

Permalink
Unions with Boolean or Enumeration discriminator might not be initial…
Browse files Browse the repository at this point in the history
…ized correctly (#156)

* Refs #21594. Fix

Signed-off-by: Ricardo González Moreno <[email protected]>

* Refs #21594. Apply suggestion

Signed-off-by: Ricardo González Moreno <[email protected]>

* Refs #21594. Documentation

Signed-off-by: Ricardo González Moreno <[email protected]>

* Apply suggestions from code review

Co-authored-by: Miguel Company <[email protected]>

---------

Signed-off-by: Ricardo González Moreno <[email protected]>
Co-authored-by: Miguel Company <[email protected]>
  • Loading branch information
richiware and MiguelCompany authored Sep 10, 2024
1 parent 1e39d94 commit 8191ad2
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/main/java/com/eprosima/idl/parser/typecode/UnionTypeCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,31 @@ public Member getDefaultAnnotatedMember()
return null;
}

/*!
* @ingroup api_for_stg
* @brief This function returns the union's default member (when there is no implicit default).
* A member is selected as the default one according to one of these cases:
* - It is under the `default:` case label.
* - It is under the case label of the default discriminator.
* - All possible values of the discriminator's type are covered so the first member should be considered the default.
*
* @return The union's default member.
*/
public Member getDefaultMember()
{
// Union's default member is explicitly selected (either under the `default:` case label or under the default discriminator value case label).
if (m_defaultindex != -1)
{
return getMembers().get(m_defaultindex);
}
// All possible values of a Boolean discriminator or Enumeration discriminator are covered.
else if((Kind.KIND_BOOLEAN == discriminator_.getTypecode().getKind() && 2 == getTotallabels().size()) ||
(Kind.KIND_ENUM == discriminator_.getTypecode().getKind() &&
((EnumTypeCode)discriminator_.getTypecode()).getMembers().size() == getTotallabels().size()))
{
// First member is selected ad the default one.
return getMembers().get(0);
}

return null;
}
Expand Down

0 comments on commit 8191ad2

Please sign in to comment.