Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r60752 - in trunk/boost/spirit/home/qi/numeric: . detail
From: joel_at_[hidden]
Date: 2010-03-21 13:15:32


Author: djowel
Date: 2010-03-21 13:15:32 EDT (Sun, 21 Mar 2010)
New Revision: 60752
URL: http://svn.boost.org/trac/boost/changeset/60752

Log:
force casting of signed integer attributes to unsigned integers when possible.
Text files modified:
   trunk/boost/spirit/home/qi/numeric/detail/numeric_utils.hpp | 40 ++++++++++++++++++++++++++++++++++++----
   trunk/boost/spirit/home/qi/numeric/numeric_utils.hpp | 7 +++++--
   2 files changed, 41 insertions(+), 6 deletions(-)

Modified: trunk/boost/spirit/home/qi/numeric/detail/numeric_utils.hpp
==============================================================================
--- trunk/boost/spirit/home/qi/numeric/detail/numeric_utils.hpp (original)
+++ trunk/boost/spirit/home/qi/numeric/detail/numeric_utils.hpp 2010-03-21 13:15:32 EDT (Sun, 21 Mar 2010)
@@ -19,7 +19,10 @@
 #include <boost/spirit/home/support/char_encoding/ascii.hpp>
 #include <boost/preprocessor/repetition/repeat.hpp>
 #include <boost/utility/enable_if.hpp>
+#include <boost/type_traits/is_integral.hpp>
+#include <boost/type_traits/is_signed.hpp>
 #include <boost/mpl/bool.hpp>
+#include <boost/mpl/and.hpp>
 
 #include <limits>
 #include <boost/limits.hpp>
@@ -350,8 +353,8 @@
                 }
             }
 
- typedef typename
- traits::attribute_type<Attribute>::type
+ typedef typename
+ traits::attribute_type<Attribute>::type
             attribute_type;
 
             attribute_type val = Accumulate ? attr : attribute_type(0);
@@ -455,8 +458,8 @@
                 }
             }
 
- typedef typename
- traits::attribute_type<Attribute>::type
+ typedef typename
+ traits::attribute_type<Attribute>::type
             attribute_type;
 
             attribute_type val = Accumulate ? attr : attribute_type(0);
@@ -512,6 +515,35 @@
 
 #undef SPIRIT_NUMERIC_INNER_LOOP
 
+ ///////////////////////////////////////////////////////////////////////////
+ // Cast an signed integer to an unsigned integer
+ ///////////////////////////////////////////////////////////////////////////
+ template <typename T,
+ bool force_unsigned
+ = mpl::and_<is_integral<T>, is_signed<T> >::value>
+ struct cast_unsigned;
+
+ template <typename T>
+ struct cast_unsigned<T, true>
+ {
+ typedef typename make_unsigned<T>::type unsigned_type;
+ typedef typename make_unsigned<T>::type& unsigned_type_ref;
+
+ static unsigned_type_ref call(T& n)
+ {
+ return unsigned_type_ref(n);
+ }
+ };
+
+ template <typename T>
+ struct cast_unsigned<T, false>
+ {
+ static T& call(T& n)
+ {
+ return n;
+ }
+ };
+
 }}}}
 
 #endif

Modified: trunk/boost/spirit/home/qi/numeric/numeric_utils.hpp
==============================================================================
--- trunk/boost/spirit/home/qi/numeric/numeric_utils.hpp (original)
+++ trunk/boost/spirit/home/qi/numeric/numeric_utils.hpp 2010-03-21 13:15:32 EDT (Sun, 21 Mar 2010)
@@ -52,7 +52,9 @@
             not_supported_radix, ());
 
         template <typename Iterator, typename Attribute>
- static bool call(Iterator& first, Iterator const& last, Attribute& attr)
+ static bool call(
+ Iterator& first, Iterator const& last,
+ Attribute& attr)
         {
             if (first == last)
                 return false;
@@ -67,7 +69,8 @@
             extract_type;
 
             Iterator save = first;
- if (!extract_type::parse(first, last, attr))
+ if (!extract_type::parse(first, last,
+ detail::cast_unsigned<Attribute>::call(attr)))
             {
                 first = save;
                 return false;


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