-
Notifications
You must be signed in to change notification settings - Fork 849
Comparable: Enable comparison operators based on a ternary comparison #4391
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
Closed
+402
−0
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,196 @@ | ||
| /** @file | ||
|
|
||
| Given a class T with a compare function that returns ordering information, create the standard | ||
| comparison operators. | ||
|
|
||
| @section license License | ||
|
|
||
| Licensed to the Apache Software Foundation (ASF) under one or more contributor license | ||
| agreements. See the NOTICE file distributed with this work for additional information regarding | ||
| copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 | ||
| (the "License"); you may not use this file except in compliance with the License. You may obtain | ||
| a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software distributed under the License | ||
| is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
| or implied. See the License for the specific language governing permissions and limitations under | ||
| the License. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <type_traits> | ||
| #include <utility> | ||
| #include "tscpp/util/ts_meta.h" | ||
|
|
||
| namespace ts | ||
| { | ||
| /** This is a manual override for comparison. | ||
| * | ||
| * @tparam T Left hand side operand type. | ||
| * @tparam U Right hand side operand type. | ||
| * | ||
| * Specialize this for @a T and @a U if the normal mechanisms yield bad results. This has the highest | ||
| * priority for selecting a compare function. | ||
| */ | ||
| template <typename T, typename U = T> struct ComparablePolicy { | ||
| }; | ||
|
|
||
| namespace detail | ||
| { | ||
| // This is an ordered set of ways to call the compare function for two types, @a T and @a U. | ||
| // One of these is called only if the operator overload detects at least one operand that | ||
| // is a @c Comparable. Therefore no checks for appropriate types are needed, and this sequence | ||
| // only probes the target types for supported free functions and methods. | ||
|
|
||
| /// Use @c ComparePolicy if it has been specialized to @a T and @a U. | ||
| template <typename T, typename U> | ||
| auto | ||
| ComparableFunction(T const &lhs, U const &rhs, meta::CaseTag<4> const &) -> decltype(ComparablePolicy<T, U>()(lhs, rhs), int()) | ||
| { | ||
| return static_cast<int>(ComparablePolicy<T, U>()(lhs, rhs)); | ||
| } | ||
|
|
||
| /// Use the free function @c cmp(T,U). | ||
| template <typename T, typename U> | ||
| auto | ||
| ComparableFunction(T const &lhs, U const &rhs, meta::CaseTag<3> const &) -> decltype(cmp(lhs, rhs), int()) | ||
| { | ||
| return static_cast<int>(cmp(lhs, rhs)); | ||
| } | ||
|
|
||
| /// Use the free function @c cmp(U,T) and flip the result. | ||
| template <typename T, typename U> | ||
| auto | ||
| ComparableFunction(T const &lhs, U const &rhs, meta::CaseTag<2> const &) -> decltype(cmp(rhs, lhs), int()) | ||
| { | ||
| return static_cast<int>(cmp(rhs, lhs)) * -1; | ||
| } | ||
|
|
||
| /// Use the @c T::cmp method. | ||
| template <typename T, typename U> | ||
| auto | ||
| ComparableFunction(T const &lhs, U const &rhs, meta::CaseTag<1> const &) -> decltype(lhs.cmp(rhs), int()) | ||
| { | ||
| return static_cast<int>(lhs.cmp(rhs)); | ||
| } | ||
|
|
||
| /// Use @c U::cmp and flip the result. | ||
| template <typename T, typename U> | ||
| auto | ||
| ComparableFunction(T const &lhs, U const &rhs, meta::CaseTag<0> const &) -> decltype(rhs.cmp(lhs), int()) | ||
| { | ||
| return static_cast<int>(rhs.cmp(lhs)) * -1; | ||
| } | ||
|
|
||
| } // namespace detail | ||
|
|
||
| /** Create standard comparison operators given a compare function. | ||
| * | ||
| * The operators supported are @c == @c != @c \< @c \> @c \<= @c \>= | ||
| * | ||
| * To successfully use this mixin, there are two requirements. | ||
| * - There must be a ternary comparison that returns an int in the standard ternary compare style. | ||
| * - The class must inherit from this mixin. | ||
| * | ||
| * The standard ternary compare must return an @c int which is | ||
| * - negative if @a lhs is smaller than @a rhs | ||
| * - 0 if @a lhs is equal to @a rhs | ||
| * - positive if @a lhs is greater than @a rhs | ||
| * | ||
| * If a comparison operator is used and at least one of the operands inherits from @c Comparable | ||
| * then the types are probed for ternary comparisons. The order is | ||
| * | ||
| * - @c ts::ComparablePolicy<lhs,rhs> specialization | ||
| * - function @c cmp(lhs,rhs) | ||
| * - function @c cmp(rhs,lhs) | ||
| * - method @c lhs::cmp(rhs) | ||
| * - method @c rhs::cmp(lhs) | ||
| * | ||
| * Once a class inherits from this mixin, then comparisons are supported against any type for | ||
| * which a ternary compare can be found. | ||
| * | ||
| * @code | ||
| * class T : public ts::Comparable | ||
| * @endcode | ||
| * | ||
| * To provide self comparison operators, it would suffice to have | ||
| * | ||
| * @code | ||
| * int cmp(T const& that) const; | ||
| * @endcode | ||
| * | ||
| * If comparison operators against @c std::string_view should supported, this could be done by adding | ||
| * | ||
| * @code | ||
| * int cmp(std::string_view that) { return strcmp(text, that); } | ||
| * @endcode | ||
| * | ||
| * If both of these are present, then the following are now valid | ||
| * @code | ||
| * T t, t1, t2; | ||
| * if ("walt"sv == t) {...} | ||
| * if ("walt" == t) {...} // because string literals convert to string_view | ||
| * if (t1 < t2) {...} | ||
| * @endcode | ||
| */ | ||
| struct Comparable { | ||
| }; | ||
|
|
||
| // --- | ||
| // For each comparison operator, a template overload is provided. These overloads are enabled for | ||
| // overload resolution iff at least one operand inherits from @c ts::Comparable. In that case the | ||
| // @c ComparableFunction machinery is engaged to probe for a ternary comparison. All of this should | ||
| // optimize away after compilation. | ||
|
|
||
| template <typename T, typename U> | ||
| auto | ||
| operator==(T const &lhs, U const &rhs) -> | ||
| typename std::enable_if<std::is_base_of<Comparable, T>::value || std::is_base_of<Comparable, U>::value, bool>::type | ||
| { | ||
| return 0 == detail::ComparableFunction(lhs, rhs, meta::CaseArg); | ||
| } | ||
|
|
||
| template <typename T, typename U> | ||
| auto | ||
| operator!=(T const &lhs, U const &rhs) -> | ||
| typename std::enable_if<std::is_base_of<Comparable, T>::value || std::is_base_of<Comparable, U>::value, bool>::type | ||
| { | ||
| return 0 != detail::ComparableFunction(lhs, rhs, meta::CaseArg); | ||
| } | ||
|
|
||
| template <typename T, typename U> | ||
| auto | ||
| operator<(T const &lhs, U const &rhs) -> | ||
| typename std::enable_if<std::is_base_of<Comparable, T>::value || std::is_base_of<Comparable, U>::value, bool>::type | ||
| { | ||
| return detail::ComparableFunction(lhs, rhs, meta::CaseArg) < 0; | ||
| } | ||
|
|
||
| template <typename T, typename U> | ||
| auto | ||
| operator<=(T const &lhs, U const &rhs) -> | ||
| typename std::enable_if<std::is_base_of<Comparable, T>::value || std::is_base_of<Comparable, U>::value, bool>::type | ||
| { | ||
| return detail::ComparableFunction(lhs, rhs, meta::CaseArg) <= 0; | ||
| } | ||
|
|
||
| template <typename T, typename U> | ||
| auto | ||
| operator>(T const &lhs, U const &rhs) -> | ||
| typename std::enable_if<std::is_base_of<Comparable, T>::value || std::is_base_of<Comparable, U>::value, bool>::type | ||
| { | ||
| return detail::ComparableFunction(lhs, rhs, meta::CaseArg) > 0; | ||
| } | ||
|
|
||
| template <typename T, typename U> | ||
| auto | ||
| operator>=(T const &lhs, U const &rhs) -> | ||
| typename std::enable_if<std::is_base_of<Comparable, T>::value || std::is_base_of<Comparable, U>::value, bool>::type | ||
| { | ||
| return detail::ComparableFunction(lhs, rhs, meta::CaseArg) >= 0; | ||
| } | ||
|
|
||
| } // end namespace ts | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider using std::is_convertable in place of std::is_base_of . This allows the alternative of tagging a class for comparison using a dummy conversion operator:
operator Comparable ();
This avoids the ambiguous base class error if both a base class and a class derived from it need to be tagged as comparable. It also allows a derived class to suppress inheritance of the comparison operators of a base class with:
operator Comparable () = delete;
https://godbolt.org/z/ToweoI
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I considered that but think it's a bad idea because it could easily cause the Comparable machinery to fire up in places it shouldn't. "if both a base class and a class derived from it need to be tagged as comparable" - IMHO that is never the case. If the base class is tagged as comparable, there is never a need to tag a class derived from it. Just don't do it. That seems a simpler solution.