Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r67463 - in trunk/boost/range: . adaptor detail
From: neil_at_[hidden]
Date: 2010-12-26 18:14:09


Author: neilgroves
Date: 2010-12-26 18:14:08 EST (Sun, 26 Dec 2010)
New Revision: 67463
URL: http://svn.boost.org/trac/boost/changeset/67463

Log:
[boost][range] - Trac item 4226 - Implemented a safe_bool utility class that is intended to be refactored into a core area. Integrated this into iterator_range.
Added:
   trunk/boost/range/detail/safe_bool.hpp (contents, props changed)
Text files modified:
   trunk/boost/range/adaptor/strided.hpp | 4 ----
   trunk/boost/range/iterator_range_core.hpp | 18 ++++++++----------
   2 files changed, 8 insertions(+), 14 deletions(-)

Modified: trunk/boost/range/adaptor/strided.hpp
==============================================================================
--- trunk/boost/range/adaptor/strided.hpp (original)
+++ trunk/boost/range/adaptor/strided.hpp 2010-12-26 18:14:08 EST (Sun, 26 Dec 2010)
@@ -15,7 +15,6 @@
 #include <boost/range/iterator_range.hpp>
 #include <boost/iterator/iterator_adaptor.hpp>
 #include <iterator>
-#include <iostream>
 
 namespace boost
 {
@@ -206,14 +205,11 @@
         private:
             void increment()
             {
- std::cout << "increment() - before = " << (this->base_reference() - m_first);
                 base_iterator& it = this->base_reference();
                 if ((m_last - it) > m_stride)
                     it += m_stride;
                 else
                     it = m_last;
-
- std::cout << " after = " << (this->base_reference() - m_first) << std::endl;
             }
 
             void decrement()

Added: trunk/boost/range/detail/safe_bool.hpp
==============================================================================
--- (empty file)
+++ trunk/boost/range/detail/safe_bool.hpp 2010-12-26 18:14:08 EST (Sun, 26 Dec 2010)
@@ -0,0 +1,72 @@
+// This header intentionally has no include guards.
+//
+// Copyright (c) 2010 Neil Groves
+// 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
+//
+// This code utilises the experience gained during the evolution of
+// <boost/smart_ptr/operator_bool.hpp>
+#ifndef BOOST_RANGE_SAFE_BOOL_INCLUDED_HPP
+#define BOOST_RANGE_SAFE_BOOL_INCLUDED_HPP
+
+#include <boost/config.hpp>
+#include <boost/range/config.hpp>
+
+namespace boost
+{
+ namespace range_detail
+ {
+
+template<class DataMemberPtr>
+class safe_bool
+{
+public:
+ typedef safe_bool this_type;
+
+#if (defined(__SUNPRO_CC) && BOOST_WORKAROUND(__SUNPRO_CC, < 0x570)) || defined(__CINT_)
+ typedef bool unspecified_bool_type;
+ static unspecified_bool_type to_unspecified_bool(const bool x, DataMemberPtr)
+ {
+ return x;
+ }
+#elif defined(_MANAGED)
+ static void unspecified_bool(this_type***)
+ {
+ }
+ typedef void(*unspecified_bool_type)(this_type***);
+ static unspecified_bool_type to_unspecified_bool(const bool x, DataMemberPtr)
+ {
+ return x ? unspecified_bool : 0;
+ }
+#elif \
+ ( defined(__MWERKS__) && BOOST_WORKAROUND(__MWERKS__, < 0x3200) ) || \
+ ( defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ < 304) ) || \
+ ( defined(__SUNPRO_CC) && BOOST_WORKAROUND(__SUNPRO_CC, <= 0x590) )
+
+ typedef bool (this_type::*unspecified_bool_type)() const;
+
+ static unspecified_bool_type to_unspecified_bool(const bool x, DataMemberPtr)
+ {
+ return x ? &this_type::detail_safe_bool_member_fn : 0;
+ }
+private:
+ bool detail_safe_bool_member_fn() const { return false; }
+#else
+ typedef DataMemberPtr unspecified_bool_type;
+ static unspecified_bool_type to_unspecified_bool(const bool x, DataMemberPtr p)
+ {
+ return x ? p : 0;
+ }
+#endif
+private:
+ safe_bool();
+ safe_bool(const safe_bool&);
+ void operator=(const safe_bool&);
+ ~safe_bool();
+};
+
+ } // namespace range_detail
+} // namespace boost
+
+#endif // include guard

Modified: trunk/boost/range/iterator_range_core.hpp
==============================================================================
--- trunk/boost/range/iterator_range_core.hpp (original)
+++ trunk/boost/range/iterator_range_core.hpp 2010-12-26 18:14:08 EST (Sun, 26 Dec 2010)
@@ -27,6 +27,7 @@
 #include <boost/range/iterator.hpp>
 #include <boost/range/difference_type.hpp>
 #include <boost/range/algorithm/equal.hpp>
+#include <boost/range/detail/safe_bool.hpp>
 #include <boost/utility/enable_if.hpp>
 #include <iterator>
 #include <algorithm>
@@ -81,7 +82,6 @@
 
         struct range_tag { };
         struct const_range_tag { };
-
     }
 
 // iterator range template class -----------------------------------------//
@@ -106,13 +106,14 @@
         template<class IteratorT>
         class iterator_range
         {
+ typedef range_detail::safe_bool< IteratorT iterator_range<IteratorT>::* > safe_bool_t;
         protected: // Used by sub_range
             //! implementation class
             typedef iterator_range_detail::iterator_range_impl<IteratorT> impl;
         public:
-
             //! this type
             typedef iterator_range<IteratorT> type;
+ typedef BOOST_DEDUCED_TYPENAME safe_bool_t::unspecified_bool_type unspecified_bool_type;
             //BOOST_BROKEN_COMPILER_TYPE_TRAITS_SPECIALIZATION(value_type);
 
             //! Encapsulated value type
@@ -238,18 +239,15 @@
                 return m_Begin == m_End;
             }
 
-#if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
- operator bool() const
+ operator unspecified_bool_type() const
             {
- return !empty();
+ return safe_bool_t::to_unspecified_bool(m_Begin != m_End, &iterator_range::m_Begin);
             }
-#else
- typedef iterator (iterator_range::*unspecified_bool_type) () const;
- operator unspecified_bool_type() const
+
+ bool operator!() const
             {
- return empty() ? 0: &iterator_range::end;
+ return empty();
             }
-#endif
 
             bool equal( const iterator_range& r ) const
             {


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