From cf185b00fa929c09a13294b899bef834c0d23377 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sat, 15 Oct 2016 19:15:53 +0200 Subject: [PATCH] Math: add explicit boolean conversion to BoolVector. --- src/Magnum/Math/BoolVector.h | 26 ++++++++++++++++++++++--- src/Magnum/Math/Test/BoolVectorTest.cpp | 11 +++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/Magnum/Math/BoolVector.h b/src/Magnum/Math/BoolVector.h index c2dab37a6..578e625a0 100644 --- a/src/Magnum/Math/BoolVector.h +++ b/src/Magnum/Math/BoolVector.h @@ -134,13 +134,33 @@ template class BoolVector { return !operator==(other); } - /** @brief Whether all bits are set */ + /** + * @brief Boolean conversion + * + * Equivalent to @ref all(). + * @see @ref any(), @ref none() + */ + explicit operator bool() const { return all(); } + + /** + * @brief Whether all bits are set + * + * @see @ref none(), @ref any(), @ref operator bool() + */ bool all() const; - /** @brief Whether no bits are set */ + /** + * @brief Whether no bits are set + * + * @see @ref all(), @ref any(), @ref operator bool() + */ bool none() const; - /** @brief Whether any bit is set */ + /** + * @brief Whether any bit is set + * + * @see @ref all(), @ref none(), @ref operator bool() + */ bool any() const { return !none(); } /** @brief Bitwise inversion */ diff --git a/src/Magnum/Math/Test/BoolVectorTest.cpp b/src/Magnum/Math/Test/BoolVectorTest.cpp index a5777762f..0325a644d 100644 --- a/src/Magnum/Math/Test/BoolVectorTest.cpp +++ b/src/Magnum/Math/Test/BoolVectorTest.cpp @@ -43,6 +43,7 @@ struct BoolVectorTest: Corrade::TestSuite::Tester { void compare(); void compareUndefined(); + void convertBool(); void all(); void none(); void any(); @@ -70,6 +71,7 @@ BoolVectorTest::BoolVectorTest() { &BoolVectorTest::compare, &BoolVectorTest::compareUndefined, + &BoolVectorTest::convertBool, &BoolVectorTest::all, &BoolVectorTest::none, &BoolVectorTest::any, @@ -193,6 +195,15 @@ void BoolVectorTest::compareUndefined() { CORRADE_VERIFY(a != c); } +void BoolVectorTest::convertBool() { + /* The ! operation should *just work* using the bool conversion operator */ + CORRADE_VERIFY(BoolVector19(0xff, 0xff, 0x07)); + CORRADE_VERIFY(!BoolVector19(0xff, 0xff, 0x04)); + + /* Implicit conversion is not allowed */ + CORRADE_VERIFY(!(std::is_convertible::value)); +} + void BoolVectorTest::all() { CORRADE_VERIFY(BoolVector19(0xff, 0xff, 0x07).all());