Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r62997 - in sandbox/xint: boost/xint libs/xint/test
From: chad.thecomfychair_at_[hidden]
Date: 2010-06-15 16:30:30


Author: chadnelson
Date: 2010-06-15 16:30:29 EDT (Tue, 15 Jun 2010)
New Revision: 62997
URL: http://svn.boost.org/trac/boost/changeset/62997

Log:
Implemented safe bool idiom for integer_t class, by request.
Text files modified:
   sandbox/xint/boost/xint/integer.hpp | 4 ++++
   sandbox/xint/libs/xint/test/test_convert.cpp | 18 ++++++++++++++++++
   2 files changed, 22 insertions(+), 0 deletions(-)

Modified: sandbox/xint/boost/xint/integer.hpp
==============================================================================
--- sandbox/xint/boost/xint/integer.hpp (original)
+++ sandbox/xint/boost/xint/integer.hpp 2010-06-15 16:30:29 EDT (Tue, 15 Jun 2010)
@@ -121,6 +121,8 @@
     typedef BOOST_XINT_INTEGER_TYPE type;
     typedef detail::raw_integer_t<BitsType::value, SecureType::value, Alloc>
         datatype;
+ typedef void (BOOST_XINT_INTEGER_TYPE::*safe_bool_type)() const;
+ void this_is_a_safe_bool_type() const { }
     #endif // BOOST_XINT_DOXYGEN_IGNORE
 
     //! \name Constructors & Destructors
@@ -164,6 +166,8 @@
     template <typename Type> BOOST_XINT_INTEGER_TYPE& operator=(const Type n) {
         BOOST_XINT_INTEGER_TYPE nn(n); _swap(nn); return *this; }
 
+ operator safe_bool_type() const { return (data.is_zero() ? 0 :
+ &BOOST_XINT_INTEGER_TYPE::this_is_a_safe_bool_type); }
     bool operator!() const { return data.is_zero(); }
     BOOST_XINT_INTEGER_TYPE operator-() const;
     BOOST_XINT_INTEGER_TYPE& operator+() { return *this; }

Modified: sandbox/xint/libs/xint/test/test_convert.cpp
==============================================================================
--- sandbox/xint/libs/xint/test/test_convert.cpp (original)
+++ sandbox/xint/libs/xint/test/test_convert.cpp 2010-06-15 16:30:29 EDT (Tue, 15 Jun 2010)
@@ -154,6 +154,24 @@
         digits + 1), exceptions::invalid_argument);
 }
 
+BOOST_AUTO_TEST_CASE(test_bool_conversions) {
+ integer n(-1);
+ BOOST_CHECK(bool(n) == true);
+ BOOST_CHECK(!n == false);
+
+ ++n; // Now zero
+ BOOST_CHECK(bool(n) == false);
+ BOOST_CHECK(!n == true);
+
+ ++n; // Now one
+ BOOST_CHECK(bool(n) == true);
+ BOOST_CHECK(!n == false);
+
+ ++n; // Now two
+ BOOST_CHECK(bool(n) == true);
+ BOOST_CHECK(!n == false);
+}
+
 BOOST_AUTO_TEST_CASE(test_to) {
     {
         typedef int T;


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk