Skip to content
Snippets Groups Projects
Commit 838bc724 authored by Rainer Kartmann's avatar Rainer Kartmann
Browse files

Fix check macros

parent 44f9bcfa
No related branches found
No related tags found
No related merge requests found
......@@ -17,20 +17,22 @@ namespace VirtualRobot
const std::string& hint,
const std::string& lhs, const std::string& rhs)
{
static const std::string newLine = "\n ";
std::stringstream ss;
ss << "Condition '" << condition << "' failed.";
if (!(lhs.empty() && rhs.empty()))
{
ss << "\n(lhs = " << lhs << ", rhs = " << rhs << ")";
ss << newLine << "(lhs = " << lhs << ", rhs = " << rhs << ")";
}
if (!hint.empty())
{
ss << "\n Hint: " << hint;
ss << newLine << "Hint: " << hint;
}
ss << "\n(line " << line << " in " << function << "() in " << file << ")" ;
ss << newLine << "(line " << line << " in " << function << "() in " << file << ")" ;
return ss.str();
}
......
......@@ -8,28 +8,34 @@
namespace VirtualRobot
{
/**
* @brief Exception class thrown by VR_CHECK_* macros ("soft" assertions).
*/
class VIRTUAL_ROBOT_IMPORT_EXPORT VirtualRobotCheckException :
public VirtualRobotException
{
public:
/// Construct with condition and meta information as well as optional hint.
VirtualRobotCheckException(
const std::string& condition,
const std::string& filename, int line, const std::string& function,
const std::string& file, int line, const std::string& function,
const std::string& hint = "");
/// Construct with left- and right-hand-side operators.
template <typename LhsT, typename RhsT>
VirtualRobotCheckException(
const std::string& condition, const LhsT& lhs, const RhsT& rhs,
const std::string& filename, int line, const std::string& function,
const std::string& file, int line, const std::string& function,
const std::string& hint = "") :
VirtualRobotException(
makeMsg(condition, filename, line, function, toString(lhs), toString(rhs), hint))
makeMsg(condition, file, line, function, hint, toString(lhs), toString(rhs)))
{}
private:
/// Convert `t` to a string using its << operator.
template <typename T>
static std::string toString(const T& t)
{
......@@ -38,6 +44,7 @@ namespace VirtualRobot
return ss.str();
}
/// Make the exception message.
static std::string makeMsg(
const std::string& condition,
const std::string& file, int line, const std::string& function,
......@@ -47,68 +54,75 @@ namespace VirtualRobot
};
/**
* Check the given condition.
* @throw VirtualRobotCheckException If `condition``evaluates to false.
*/
#define VR_CHECK_HINT(condition, hint) \
do { \
if( !(expression) ) { \
if( !(condition) ) { \
throw ::VirtualRobot::VirtualRobotCheckException( \
#condition, __FILE__, __LINE__, __FUNCTION__, hint); \
} \
} while(0);
/**
* Check the given condition.
* @throw VirtualRobotCheckException If `condition``evaluates to false.
*/
#define VR_CHECK(condition) \
VR_CHECK_HINT(condition, "")
#define VR_CHECK_COMPARISON_HINT(lhs, rhs, cmp, hint) \
do { \
if( !(lhs cmp rhs) ) {\
if( !((lhs) cmp (rhs)) ) { \
throw ::VirtualRobot::VirtualRobotCheckException( \
#lhs " " #cmp " " #rhs, lhs, rhs, __FILE__, __LINE__, __FUNCTION__, hint); \
} \
} while(0)
#define VR_CHECK_COMPARISON_HINT(lhs, rhs, cmp) \
VR_CHECK_HINT(lhs, rhs, cmp, "")
#define VR_CHECK_COMPARISON(lhs, rhs, cmp) \
VR_CHECK_COMPARISON_HINT(lhs, rhs, cmp, "")
#define VR_CHECK_EQUAL(lhs, rhs) VR_CHECK_COMPARISION(lhs, rhs, ==)
#define VR_CHECK_EQUAL_HINT(lhs, rhs, hint) VR_CHECK_COMPARISION(lhs, rhs, ==, hint)
#define VR_CHECK_EQUAL(lhs, rhs) VR_CHECK_COMPARISON(lhs, rhs, ==)
#define VR_CHECK_EQUAL_HINT(lhs, rhs, hint) VR_CHECK_COMPARISON_HINT(lhs, rhs, ==, hint)
#define VR_CHECK_NOT_EQUAL(lhs, rhs) VR_CHECK_COMPARISION(lhs, rhs, !=)
#define VR_CHECK_NOT_EQUAL_HINT(lhs, rhs, hint) VR_CHECK_COMPARISION(lhs, rhs, !=, hint)
#define VR_CHECK_NOT_EQUAL(lhs, rhs) VR_CHECK_COMPARISON(lhs, rhs, !=)
#define VR_CHECK_NOT_EQUAL_HINT(lhs, rhs, hint) VR_CHECK_COMPARISON_HINT(lhs, rhs, !=, hint)
#define VR_CHECK_LESS(lhs, rhs) VR_CHECK_COMPARISON(lhs, rhs, <)
#define VR_CHECK_LESS_HINT(lhs, rhs, hint) VR_CHECK_COMPARISON_HINT(lhs, rhs, <, hint)
#define VR_CHECK_LESS(lhs, rhs) VR_CHECK_COMPARISION(lhs, rhs, <)
#define VR_CHECK_LESS_HINT(lhs, rhs, hint) VR_CHECK_COMPARISION(lhs, rhs, <, hint)
#define VR_CHECK_LESS_EQUAL(lhs, rhs) VR_CHECK_COMPARISION(lhs, rhs, <=)
#define VR_CHECK_LESS_EQUAL_HINT(lhs, rhs, hint) VR_CHECK_COMPARISION(lhs, rhs, <=, hint)
#define VR_CHECK_LESS_EQUAL(lhs, rhs) VR_CHECK_COMPARISON(lhs, rhs, <=)
#define VR_CHECK_LESS_EQUAL_HINT(lhs, rhs, hint) VR_CHECK_COMPARISON_HINT(lhs, rhs, <=, hint)
#define VR_CHECK_GREATER(lhs, rhs) VR_CHECK_COMPARISION(lhs, rhs, >)
#define VR_CHECK_GREATER_HINT(lhs, rhs, hint) VR_CHECK_COMPARISION(lhs, rhs, >, hint)
#define VR_CHECK_GREATER(lhs, rhs) VR_CHECK_COMPARISON(lhs, rhs, >)
#define VR_CHECK_GREATER_HINT(lhs, rhs, hint) VR_CHECK_COMPARISON_HINT(lhs, rhs, >, hint)
#define VR_CHECK_GREATER_EQUAL(lhs, rhs) VR_CHECK_COMPARISION(lhs, rhs, >=)
#define VR_CHECK_GREATER_EQUAL_HINT(lhs, rhs, hint) VR_CHECK_COMPARISION(lhs, rhs, >=, hint)
#define VR_CHECK_GREATER_EQUAL(lhs, rhs) VR_CHECK_COMPARISON(lhs, rhs, >=)
#define VR_CHECK_GREATER_EQUAL_HINT(lhs, rhs, hint) VR_CHECK_COMPARISON_HINT(lhs, rhs, >=, hint)
#define VR_CHECK_NONNEGATIVE(value) VR_CHECK_GREATER_EQUAL(value, 0)
#define VR_CHECK_NONNEGATIVE_HINT(value, hint) VR_CHECK_GREATER_EQUAL(value, 0, hint)
#define VR_CHECK_NONNEGATIVE_HINT(value, hint) VR_CHECK_GREATER_EQUAL_HINT(value, 0, hint)
#define VR_CHECK_POSITIVE(value) VR_CHECK_GREATER(value, 0)
#define VR_CHECK_POSITIVE_HINT(value, hint) VR_CHECK_GREATER(value, 0, hint)
#define VR_CHECK_FITS_SIZE(value, size) \
#define VR_CHECK_POSITIVE_HINT(value, hint) VR_CHECK_GREATER_HINT(value, 0, hint)
#define VR_CHECK_FITS_SIZE(value, size) \
VR_CHECK_NONNEGATIVE(value); \
VR_CHECK_LESS(value, size)
#define VR_CHECK_FITS_SIZE_HINT(value, size, hint) \
VR_CHECK_NONNEGATIVE(value, hint); \
VR_CHECK_LESS(value, size, hint)
#define VR_CHECK_FITS_SIZE_HINT(value, size, hint) \
VR_CHECK_NONNEGATIVE_HINT(value, hint); \
VR_CHECK_LESS_HINT(value, size, hint)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment