Refactored Evaluator templates to only be specialised on Op, with Lhs/Rhs types templated on method instead

This commit is contained in:
Phil Nash 2017-08-06 00:29:37 +01:00
parent 0a2ce87d32
commit 63392e095e
2 changed files with 43 additions and 47 deletions

View File

@ -36,71 +36,67 @@ namespace Internal {
// So the compare overloads can be operator agnostic we convey the operator as a template // So the compare overloads can be operator agnostic we convey the operator as a template
// enum, which is used to specialise an Evaluator for doing the comparison. // enum, which is used to specialise an Evaluator for doing the comparison.
template<typename T1, typename T2, Operator Op> template<Operator Op>
struct Evaluator{}; struct Evaluator{};
template<typename T1, typename T2> template<>
struct Evaluator<T1, T2, IsEqualTo> { struct Evaluator<IsEqualTo> {
template<typename T1, typename T2>
static bool evaluate( T1 const& lhs, T2 const& rhs) { static bool evaluate( T1 const& lhs, T2 const& rhs) {
return bool(removeConst(lhs) == removeConst(rhs) ); return bool(removeConst(lhs) == removeConst(rhs) );
} }
}; template<typename T>
template<typename T1, typename T2>
struct Evaluator<T1, T2, IsNotEqualTo> {
static bool evaluate( T1 const& lhs, T2 const& rhs ) {
return bool(removeConst(lhs) != removeConst(rhs) );
}
};
template<typename T1, typename T2>
struct Evaluator<T1, T2, IsLessThan> {
static bool evaluate( T1 const& lhs, T2 const& rhs ) {
return bool(removeConst(lhs) < removeConst(rhs) );
}
};
template<typename T1, typename T2>
struct Evaluator<T1, T2, IsGreaterThan> {
static bool evaluate( T1 const& lhs, T2 const& rhs ) {
return bool(removeConst(lhs) > removeConst(rhs) );
}
};
template<typename T1, typename T2>
struct Evaluator<T1, T2, IsGreaterThanOrEqualTo> {
static bool evaluate( T1 const& lhs, T2 const& rhs ) {
return bool(removeConst(lhs) >= removeConst(rhs) );
}
};
template<typename T1, typename T2>
struct Evaluator<T1, T2, IsLessThanOrEqualTo> {
static bool evaluate( T1 const& lhs, T2 const& rhs ) {
return bool(removeConst(lhs) <= removeConst(rhs) );
}
};
// Special case for comparing a pointer to an int (deduced for p==0)
template<typename T>
struct Evaluator<int const&, T* const&, IsEqualTo> {
static bool evaluate( int lhs, T* rhs) { static bool evaluate( int lhs, T* rhs) {
return reinterpret_cast<void const*>( lhs ) == rhs; return reinterpret_cast<void const*>( lhs ) == rhs;
} }
}; template<typename T>
template<typename T>
struct Evaluator<T* const&, int const&, IsEqualTo> {
static bool evaluate( T* lhs, int rhs) { static bool evaluate( T* lhs, int rhs) {
return lhs == reinterpret_cast<void const*>( rhs ); return lhs == reinterpret_cast<void const*>( rhs );
} }
}; };
template<typename T> template<>
struct Evaluator<int const&, T* const&, IsNotEqualTo> { struct Evaluator<IsNotEqualTo> {
template<typename T1, typename T2>
static bool evaluate( T1 const& lhs, T2 const& rhs ) {
return bool(removeConst(lhs) != removeConst(rhs) );
}
template<typename T>
static bool evaluate( int lhs, T* rhs) { static bool evaluate( int lhs, T* rhs) {
return reinterpret_cast<void const*>( lhs ) != rhs; return reinterpret_cast<void const*>( lhs ) != rhs;
} }
}; template<typename T>
template<typename T>
struct Evaluator<T* const&, int const&, IsNotEqualTo> {
static bool evaluate( T* lhs, int rhs) { static bool evaluate( T* lhs, int rhs) {
return lhs != reinterpret_cast<void const*>( rhs ); return lhs != reinterpret_cast<void const*>( rhs );
} }
}; };
template<>
struct Evaluator<IsLessThan> {
template<typename T1, typename T2>
static bool evaluate( T1 const& lhs, T2 const& rhs ) {
return bool(removeConst(lhs) < removeConst(rhs) );
}
};
template<>
struct Evaluator<IsGreaterThan> {
template<typename T1, typename T2>
static bool evaluate( T1 const& lhs, T2 const& rhs ) {
return bool(removeConst(lhs) > removeConst(rhs) );
}
};
template<>
struct Evaluator<IsGreaterThanOrEqualTo> {
template<typename T1, typename T2>
static bool evaluate( T1 const& lhs, T2 const& rhs ) {
return bool(removeConst(lhs) >= removeConst(rhs) );
}
};
template<>
struct Evaluator<IsLessThanOrEqualTo> {
template<typename T1, typename T2>
static bool evaluate( T1 const& lhs, T2 const& rhs ) {
return bool(removeConst(lhs) <= removeConst(rhs) );
}
};
} // end of namespace Internal } // end of namespace Internal
} // end of namespace Catch } // end of namespace Catch

View File

@ -113,7 +113,7 @@ public:
void endExpression() const { void endExpression() const {
m_rb m_rb
.setResultType( Internal::Evaluator<LhsT, RhsT, Op>::evaluate( m_lhs, m_rhs ) ) .setResultType( Internal::Evaluator<Op>::evaluate( m_lhs, m_rhs ) )
.endExpression( *this ); .endExpression( *this );
} }