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

Replace boost::totally_ordered with explicit operator overloads for TfEnum #2251

Merged
merged 1 commit into from
Jul 3, 2023
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
27 changes: 25 additions & 2 deletions pxr/base/tf/enum.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
#include "pxr/base/tf/safeTypeCompare.h"
#include "pxr/base/tf/api.h"

#include <boost/operators.hpp>
#include <boost/preprocessor/punctuation/comma_if.hpp>

#include <iosfwd>
Expand Down Expand Up @@ -136,7 +135,7 @@ PXR_NAMESPACE_OPEN_SCOPE
/// // Returns 5, sets found to \c true
/// \endcode
///
class TfEnum : boost::totally_ordered<TfEnum>
class TfEnum
{
public:
/// Default constructor assigns integer value zero.
Expand Down Expand Up @@ -169,6 +168,12 @@ class TfEnum : boost::totally_ordered<TfEnum>
TfSafeTypeCompare(*t._typeInfo, *_typeInfo);
}

/// Inequality operator
/// \sa TfEnum::operator==(const TfEnum&)
bool operator!=(const TfEnum& t) const {
return !(*this == t);
}

/// Less than comparison. Enum values belonging to the same type are
/// ordered according to their numeric value. Enum values belonging to
/// different types are ordered in a consistent but arbitrary way which
Expand All @@ -178,6 +183,24 @@ class TfEnum : boost::totally_ordered<TfEnum>
(!t._typeInfo->before(*_typeInfo) && _value < t._value);
}

/// Less than or equal operator
/// \sa TfEnum::operator<(const TfEnum&)
bool operator<=(const TfEnum& t) const {
return !(t < *this);
}

/// Greater than operator
/// \sa TfEnum::operator<(const TfEnum&)
bool operator>(const TfEnum& t) const {
return t < *this;
}

/// Greater than or equal operator
/// \sa TfEnum::operator<(const TfEnum&)
bool operator>=(const TfEnum& t) const {
return !(*this < t);
}

/// True if \c *this has been assigned with \c value.
template <class T>
std::enable_if_t<std::is_enum<T>::value, bool>
Expand Down
10 changes: 10 additions & 0 deletions pxr/base/tf/testenv/enum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,16 @@ Test_TfEnum()
TF_AXIOM(e.GetValueAsInt() == 3);
TF_AXIOM(e.GetValue<Season>() == SUMMER);

// Test coverage for operators
TF_AXIOM(TfEnum(SUMMER) == TfEnum(SUMMER));
TF_AXIOM(TfEnum(SUMMER) <= TfEnum(SUMMER));
TF_AXIOM(TfEnum(SUMMER) >= TfEnum(SUMMER));
TF_AXIOM(TfEnum(SUMMER) != TfEnum(SPRING));
TF_AXIOM(TfEnum(SUMMER) > TfEnum(SPRING));
TF_AXIOM(TfEnum(SUMMER) >= TfEnum(SPRING));
TF_AXIOM(TfEnum(SPRING) <= TfEnum(SUMMER));
TF_AXIOM(TfEnum(SPRING) < TfEnum(SUMMER));

return true;
}

Expand Down