eclipse-cyclonedds / cyclonedds-cxx

Other
95 stars 75 forks source link

Equality operator returns false for an equal empty union #457

Open jordanLatta155 opened 1 year ago

jordanLatta155 commented 1 year ago

I have an IDL union with an enum discriminator with one of the enum cases being empty. The generated C++ code's == operator will return false when comparing two "empty" unions , even if their discriminator is the same.

example: IDL:

enum AllocationState {
    Free,
    Allocated,
    Offline
};

union AllocationStateUnion switch (AllocationState) {
    case Allocated:
    case Offline:
        string reason;
};

C++:

bool operator==(const AllocationStateUnion& _other) const
{
  if(_d() != _other._d()) return false;
    switch(_d()) {
      case AllocationState::Allocated:
      case AllocationState::Offline:
        return reason() == _other.reason();
  }
  return false;
}

The last return false in the generated C++ is the branched reached when both _d() are equal to AllocationState::Free.

eboasson commented 1 year ago

Interesting ... makes me wonder if it shouldn't simply end in return true; instead. @reicheratwork am I barking up the right tree?

reicheratwork commented 1 year ago

Let me have a look at this

reicheratwork commented 1 year ago

Also found some other issues that were exposed by creating a unittest that used unions with default cases, fixing those as well :)

@jordanLatta155 , can I count on you to verify my fix and review my PR when I create it?