PageRenderTime 53ms CodeModel.GetById 35ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/Src/Dependencies/Boost/boost/spirit/home/qi/numeric/numeric_utils.hpp

http://hadesmem.googlecode.com/
C++ Header | 149 lines | 111 code | 17 blank | 21 comment | 22 complexity | de5fc65b33631325897c7d57fa843a6e MD5 | raw file
  1/*=============================================================================
  2    Copyright (c) 2001-2011 Joel de Guzman
  3    Copyright (c) 2011 Jan Frederick Eick
  4
  5    Distributed under the Boost Software License, Version 1.0. (See accompanying
  6    file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7==============================================================================*/
  8#if !defined(BOOST_SPIRIT_NUMERIC_UTILS_APRIL_17_2006_0830AM)
  9#define BOOST_SPIRIT_NUMERIC_UTILS_APRIL_17_2006_0830AM
 10
 11#if defined(_MSC_VER)
 12#pragma once
 13#endif
 14
 15#include <boost/spirit/home/support/assert_msg.hpp>
 16#include <boost/spirit/home/qi/detail/assign_to.hpp>
 17#include <boost/spirit/home/qi/numeric/detail/numeric_utils.hpp>
 18#include <boost/assert.hpp>
 19#include <boost/mpl/assert.hpp>
 20
 21namespace boost { namespace spirit { namespace qi
 22{
 23    ///////////////////////////////////////////////////////////////////////////
 24    //  Extract the prefix sign (- or +), return true if a '-' was found
 25    ///////////////////////////////////////////////////////////////////////////
 26    template <typename Iterator>
 27    inline bool
 28    extract_sign(Iterator& first, Iterator const& last)
 29    {
 30        (void)last;                  // silence unused warnings
 31        BOOST_ASSERT(first != last); // precondition
 32
 33        // Extract the sign
 34        bool neg = *first == '-';
 35        if (neg || (*first == '+'))
 36        {
 37            ++first;
 38            return neg;
 39        }
 40        return false;
 41    }
 42
 43    ///////////////////////////////////////////////////////////////////////////
 44    // Low level unsigned integer parser
 45    ///////////////////////////////////////////////////////////////////////////
 46    template <typename T, unsigned Radix, unsigned MinDigits, int MaxDigits
 47      , bool Accumulate = false>
 48    struct extract_uint
 49    {
 50        // check template parameter 'Radix' for validity
 51        BOOST_SPIRIT_ASSERT_MSG(
 52            Radix >= 2 && Radix <= 36,
 53            not_supported_radix, ());
 54
 55        template <typename Iterator>
 56        inline static bool call(Iterator& first, Iterator const& last, T& attr)
 57        {
 58            if (first == last)
 59                return false;
 60
 61            typedef detail::extract_int<
 62                T
 63              , Radix
 64              , MinDigits
 65              , MaxDigits
 66              , detail::positive_accumulator<Radix>
 67              , Accumulate>
 68            extract_type;
 69
 70            Iterator save = first;
 71            if (!extract_type::parse(first, last,
 72                detail::cast_unsigned<T>::call(attr)))
 73            {
 74                first = save;
 75                return false;
 76            }
 77            return true;
 78        }
 79
 80        template <typename Iterator, typename Attribute>
 81        inline static bool call(Iterator& first, Iterator const& last, Attribute& attr_)
 82        {
 83            // this case is called when Attribute is not T
 84            T attr;
 85            if (call(first, last, attr))
 86            {
 87                traits::assign_to(attr, attr_);
 88                return true;
 89            }
 90            return false;
 91        }
 92    };
 93
 94    ///////////////////////////////////////////////////////////////////////////
 95    // Low level signed integer parser
 96    ///////////////////////////////////////////////////////////////////////////
 97    template <typename T, unsigned Radix, unsigned MinDigits, int MaxDigits>
 98    struct extract_int
 99    {
100        // check template parameter 'Radix' for validity
101        BOOST_SPIRIT_ASSERT_MSG(
102            Radix == 2 || Radix == 8 || Radix == 10 || Radix == 16,
103            not_supported_radix, ());
104
105        template <typename Iterator>
106        inline static bool call(Iterator& first, Iterator const& last, T& attr)
107        {
108            if (first == last)
109                return false;
110
111            typedef detail::extract_int<
112                T, Radix, MinDigits, MaxDigits>
113            extract_pos_type;
114
115            typedef detail::extract_int<
116                T, Radix, MinDigits, MaxDigits, detail::negative_accumulator<Radix> >
117            extract_neg_type;
118
119            Iterator save = first;
120            bool hit = extract_sign(first, last);
121            if (hit)
122                hit = extract_neg_type::parse(first, last, attr);
123            else
124                hit = extract_pos_type::parse(first, last, attr);
125
126            if (!hit)
127            {
128                first = save;
129                return false;
130            }
131            return true;
132        }
133
134        template <typename Iterator, typename Attribute>
135        inline static bool call(Iterator& first, Iterator const& last, Attribute& attr_)
136        {
137            // this case is called when Attribute is not T
138            T attr;
139            if (call(first, last, attr))
140            {
141                traits::assign_to(attr, attr_);
142                return true;
143            }
144            return false;
145        }
146    };
147}}}
148
149#endif