Skip to content

Commit

Permalink
Better detection of comparability in BoxUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
gershnik committed Jan 9, 2024
1 parent a7f81cc commit d34aa28
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
6 changes: 3 additions & 3 deletions include/objc-helpers/BoxUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ template<class T>
class BoxMaker {
private:
static auto detectBoxedType() {
if constexpr (std::three_way_comparable<T, std::strong_ordering>) {
if constexpr (std::totally_ordered<T>) {
if constexpr (std::is_copy_constructible_v<T>) {
return (NSObject<BoxedValue, BoxedComparable, NSCopying> *)nullptr;
} else {
Expand Down Expand Up @@ -172,7 +172,7 @@ class BoxMaker {
if (!class_addMethod(cls, objcData.hashSel, IMP(hash), (@encode(NSUInteger) + "@:"s).c_str()))
@throw [NSException exceptionWithName:NSGenericException reason:@"class_addMethod(hash) failed" userInfo:nullptr];

if constexpr (std::three_way_comparable<T, std::strong_ordering>) {
if constexpr (std::totally_ordered<T>) {
if (!class_addMethod(cls, objcData.compareSel, IMP(compare), (@encode(NSComparisonResult) + "@:@"s).c_str()))
@throw [NSException exceptionWithName:NSGenericException reason:@"class_addMethod(compare) failed" userInfo:nullptr];
}
Expand Down Expand Up @@ -274,7 +274,7 @@ class BoxMaker {
@throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"comparison operand is of invalid type" userInfo:nullptr];
auto * val = (T *)classData.addrOfValue(self);
auto * otherVal = (T *)classData.addrOfValue(other);
const auto res = *val <=> *otherVal;
const auto res = std::compare_strong_order_fallback(*val, *otherVal);
return NSComparisonResult(-(res < 0) + (res > 0));
}
public:
Expand Down
31 changes: 31 additions & 0 deletions test/BoxUtilTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,37 @@
CHECK([obj.description isEqualToString:@"Boxed object of type \"qwerty\""]);
}

struct no_spaceship {
int i;

bool operator==(const no_spaceship &) const = default;
bool operator!=(const no_spaceship &) const = default;
bool operator<(const no_spaceship & rhs) const { return i < rhs.i; }
bool operator<=(const no_spaceship & rhs) const { return i <= rhs.i; }
bool operator>(const no_spaceship & rhs) const { return i > rhs.i; }
bool operator>=(const no_spaceship & rhs) const { return i >= rhs.i; }
};

TEST_CASE( "no-spaceship" ) {
auto obj1 = box(no_spaceship{3});
auto obj2 = box(no_spaceship{4});
CHECK([obj1 compare:obj2] == NSOrderedAscending);
}

struct spaceship_only {
int i;

std::strong_ordering operator<=>(const spaceship_only &) const = default;
};

TEST_CASE( "no-spaceship" ) {
auto obj1 = box(spaceship_only{3});
auto obj2 = box(spaceship_only{4});
CHECK([obj1 compare:obj2] == NSOrderedAscending);

auto obj3 = box(spaceship_only{3});
CHECK([obj3 isEqual:obj1]);
}


TEST_SUITE_END();

0 comments on commit d34aa28

Please sign in to comment.