Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable relational operators for classes and exceptions C++ #2862

Merged
merged 1 commit into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 24 additions & 12 deletions cpp/include/Ice/Comparable.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,84 +117,96 @@ namespace Ice
namespace Tuple
{
/**
* Relational operator for generated structs and classes.
* Relational operator for generated structs.
* @param lhs The left-hand side.
* @param rhs The right-hand side.
* @return True if the left-hand side compares less than the right-hand side, false otherwise.
*/
template<
class C,
std::enable_if_t<std::is_member_function_pointer<decltype(&C::ice_tuple)>::value, bool> = true>
std::enable_if_t<
std::is_member_function_pointer<decltype(&C::ice_tuple)>::value && !std::is_polymorphic_v<C>,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

classes and exceptions are polymorphic, while structs aren't.

bool> = true>
bool operator<(const C& lhs, const C& rhs)
{
return lhs.ice_tuple() < rhs.ice_tuple();
}

/**
* Relational operator for generated structs and classes.
* Relational operator for generated structs.
* @param lhs The left-hand side.
* @param rhs The right-hand side.
* @return True if the left-hand side compares less than or equal to the right-hand side, false otherwise.
*/
template<
class C,
std::enable_if_t<std::is_member_function_pointer<decltype(&C::ice_tuple)>::value, bool> = true>
std::enable_if_t<
std::is_member_function_pointer<decltype(&C::ice_tuple)>::value && !std::is_polymorphic_v<C>,
bool> = true>
bool operator<=(const C& lhs, const C& rhs)
{
return lhs.ice_tuple() <= rhs.ice_tuple();
}

/**
* Relational operator for generated structs and classes.
* Relational operator for generated structs.
* @param lhs The left-hand side.
* @param rhs The right-hand side.
* @return True if the left-hand side compares greater than the right-hand side, false otherwise.
*/
template<
class C,
std::enable_if_t<std::is_member_function_pointer<decltype(&C::ice_tuple)>::value, bool> = true>
std::enable_if_t<
std::is_member_function_pointer<decltype(&C::ice_tuple)>::value && !std::is_polymorphic_v<C>,
bool> = true>
bool operator>(const C& lhs, const C& rhs)
{
return lhs.ice_tuple() > rhs.ice_tuple();
}

/**
* Relational operator for generated structs and classes.
* Relational operator for generated structs.
* @param lhs The left-hand side.
* @param rhs The right-hand side.
* @return True if the left-hand side compares greater than or equal to the right-hand side, false otherwise.
*/
template<
class C,
std::enable_if_t<std::is_member_function_pointer<decltype(&C::ice_tuple)>::value, bool> = true>
std::enable_if_t<
std::is_member_function_pointer<decltype(&C::ice_tuple)>::value && !std::is_polymorphic_v<C>,
bool> = true>
bool operator>=(const C& lhs, const C& rhs)
{
return lhs.ice_tuple() >= rhs.ice_tuple();
}

/**
* Relational operator for generated structs and classes.
* Relational operator for generated structs.
* @param lhs The left-hand side.
* @param rhs The right-hand side.
* @return True if the left-hand side compares equal to the right-hand side, false otherwise.
*/
template<
class C,
std::enable_if_t<std::is_member_function_pointer<decltype(&C::ice_tuple)>::value, bool> = true>
std::enable_if_t<
std::is_member_function_pointer<decltype(&C::ice_tuple)>::value && !std::is_polymorphic_v<C>,
bool> = true>
bool operator==(const C& lhs, const C& rhs)
{
return lhs.ice_tuple() == rhs.ice_tuple();
}

/**
* Relational operator for generated structs and classes.
* Relational operator for generated structs.
* @param lhs The left-hand side.
* @param rhs The right-hand side.
* @return True if the left-hand side is not equal to the right-hand side, false otherwise.
*/
template<
class C,
std::enable_if_t<std::is_member_function_pointer<decltype(&C::ice_tuple)>::value, bool> = true>
std::enable_if_t<
std::is_member_function_pointer<decltype(&C::ice_tuple)>::value && !std::is_polymorphic_v<C>,
bool> = true>
bool operator!=(const C& lhs, const C& rhs)
{
return lhs.ice_tuple() != rhs.ice_tuple();
Expand Down
22 changes: 13 additions & 9 deletions cpp/src/slice2cpp/Gen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2000,16 +2000,20 @@ Slice::Gen::DataDefVisitor::visitModuleStart(const ModulePtr& p)
}

void
Slice::Gen::DataDefVisitor::visitModuleEnd(const ModulePtr&)
Slice::Gen::DataDefVisitor::visitModuleEnd(const ModulePtr& p)
{
// Always generated when this module contains a struct, class, or exception.
H << sp;
H << nl << "using Ice::Tuple::operator<;";
H << nl << "using Ice::Tuple::operator<=;";
H << nl << "using Ice::Tuple::operator>;";
H << nl << "using Ice::Tuple::operator>=;";
H << nl << "using Ice::Tuple::operator==;";
H << nl << "using Ice::Tuple::operator!=;";
if (p->contains<Struct>())
{
// Bring in relational operators for structs.
H << sp;
H << nl << "using Ice::Tuple::operator<;";
H << nl << "using Ice::Tuple::operator<=;";
H << nl << "using Ice::Tuple::operator>;";
H << nl << "using Ice::Tuple::operator>=;";
H << nl << "using Ice::Tuple::operator==;";
H << nl << "using Ice::Tuple::operator!=;";
}

H << sp << nl << '}';
_useWstring = resetUseWstring(_useWstringHist);
}
Expand Down
10 changes: 3 additions & 7 deletions cpp/test/Ice/objects/AllTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,14 @@ allTests(Test::TestHelper* helper)
test(ba2->theS.str == "hello");
test(ba2->str == "hi");

test(*ba1 < *ba2);
test(*ba2 > *ba1);
test(*ba1 != *ba2);
auto [s2, str2] = ba2->ice_tuple();
test(s2 == s);
test(str2 == "hi");

ba1 = ba2->ice_clone();
test(ba1->theS.str == "hello");
test(ba1->str == "hi");

test(*ba1 == *ba2);
test(*ba1 >= *ba2);
test(*ba1 <= *ba2);

BasePtr bp1 = make_shared<Base>();
bp1 = ba2->ice_clone();
test(bp1->theS.str == "hello");
Expand Down
Loading
Loading