catch2/include/internal/catch_evaluate.hpp

110 lines
3.4 KiB
C++
Raw Normal View History

/*
* Created by Phil on 04/03/2011.
* Copyright 2011 Two Blue Cubes Ltd. All rights reserved.
*
* Distributed under the Boost Software License, Version 1.0. (See accompanying
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*/
2011-03-09 10:38:33 +01:00
#ifndef TWOBLUECUBES_CATCH_EVALUATE_HPP_INCLUDED
#define TWOBLUECUBES_CATCH_EVALUATE_HPP_INCLUDED
2013-03-25 09:46:48 +01:00
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable:4389) // '==' : signed/unsigned mismatch
#pragma warning(disable:4018) // more "signed/unsigned mismatch"
#pragma warning(disable:4312) // Converting int to T* using reinterpret_cast (issue on x64 platform)
2013-03-25 09:46:48 +01:00
#endif
#include <cstddef>
2012-05-15 08:42:26 +02:00
namespace Catch {
namespace Internal {
enum Operator {
2011-03-09 10:33:33 +01:00
IsEqualTo,
IsNotEqualTo,
IsLessThan,
IsGreaterThan,
IsLessThanOrEqualTo,
IsGreaterThanOrEqualTo
};
const char* operatorName(Operator op);
template<typename T>
T& removeConst(T const &t) { return const_cast<T&>(t); }
2013-03-25 09:46:48 +01:00
// 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.
template<Operator Op>
struct Evaluator{};
template<>
struct Evaluator<IsEqualTo> {
template<typename T1, typename T2>
static bool evaluate( T1 const& lhs, T2 const& rhs) {
return bool(removeConst(lhs) == removeConst(rhs) );
2011-03-09 10:33:33 +01:00
}
template<typename T>
static bool evaluate( int lhs, T* rhs) {
return reinterpret_cast<void const*>( lhs ) == rhs;
}
template<typename T>
static bool evaluate( T* lhs, int rhs) {
return lhs == reinterpret_cast<void const*>( rhs );
}
};
template<>
struct Evaluator<IsNotEqualTo> {
template<typename T1, typename T2>
static bool evaluate( T1 const& lhs, T2 const& rhs ) {
return bool(removeConst(lhs) != removeConst(rhs) );
2011-03-09 10:33:33 +01:00
}
template<typename T>
static bool evaluate( int lhs, T* rhs) {
return reinterpret_cast<void const*>( lhs ) != rhs;
}
template<typename T>
static bool evaluate( T* lhs, int 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) );
}
2011-03-09 10:33:33 +01:00
};
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) );
}
2011-03-09 10:33:33 +01:00
};
template<>
struct Evaluator<IsLessThanOrEqualTo> {
template<typename T1, typename T2>
static bool evaluate( T1 const& lhs, T2 const& rhs ) {
return bool(removeConst(lhs) <= removeConst(rhs) );
}
};
2012-11-06 20:13:25 +01:00
} // end of namespace Internal
} // end of namespace Catch
2013-03-25 09:46:48 +01:00
#ifdef _MSC_VER
#pragma warning(pop)
#endif
2011-03-09 10:38:33 +01:00
#endif // TWOBLUECUBES_CATCH_EVALUATE_HPP_INCLUDED