Skip to content
Snippets Groups Projects
ParseInt.ih 7.7 KiB
Newer Older
sbund's avatar
sbund committed
// $Id$
//
// Copyright (C) 2006
sbund's avatar
sbund committed
// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
// Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
//     Stefan Bund <stefan.bund@fokus.fraunhofer.de>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the
// Free Software Foundation, Inc.,
// 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

#ifndef IH_ParseInt_
#define IH_ParseInt_ 1

// Custom includes

///////////////////////////////ih.p////////////////////////////////////////

sbund's avatar
sbund committed
namespace impl {
sbund's avatar
sbund committed
    ///////////////////////////////////////////////////////////////////////////
    // Integer operators

    template <class Derived, class Value>
    class ParseIntOps
    {
    public:
        typedef Value value_type;

        operator Value () const { return derived().value(); }

#       define unary(op) \
            Value operator op () const { return op derived().value(); }
sbund's avatar
sbund committed
#       define mutator(op) \
            template <class Other> Derived const & operator op ## =  (Other other) \
                { derived().value( derived().value() op other ); return derived(); }

        unary(~)
        unary(!)
        unary(-)

        mutator(+)
        mutator(-)
        mutator(*)
        mutator(/)
        mutator(%)
        mutator(<<)
        mutator(>>)
        mutator(&)
        mutator(|)
        mutator(^)

#       undef unary
#       undef mutator
sbund's avatar
sbund committed
        Derived const & operator ++ ()
            { derived().value( derived.value()+1 ); return derived(); }
        Derived const & operator -- ()
            { derived().value( derived.value()-1 ); return derived(); }

        Derived const & operator ++ (int)
            { Value v (derived.value()); derived().value( v+1 ); return v; }
        Derived const & operator -- (int)
            { Value v (derived.value()); derived().value( v-1 ); return v; }

sbund's avatar
sbund committed
    private:
        Derived & derived() { return *static_cast<Derived *>(this); }
        Derived const & derived() const { return *static_cast<Derived const *>(this); };
    };
sbund's avatar
sbund committed
    ///////////////////////////////////////////////////////////////////////////
    // Network byte order integer extraction
sbund's avatar
sbund committed
    template <class Iterator>
    boost::uint16_t parse_uint16(Iterator const & i)
    {
        return i[1] | i[0]<<8;
    }

    template <class Iterator>
    void write_uint16(Iterator const & i, boost::uint16_t v)
    {
        i[0] = ( v >>  8 ) & 0xff;
        i[1] = ( v       ) & 0xff;
    }

    template <class Iterator>
    boost::uint32_t parse_uint24(Iterator const & i)
    {
        return i[2] | i[1]<<8 | i[0]<<16;
    }

    template <class Iterator>
    void write_uint24(Iterator const & i, boost::uint32_t v)
    {
        i[0] = ( v >> 16 ) & 0xff;
        i[1] = ( v >>  8 ) & 0xff;
        i[2] = ( v       ) & 0xff;
    }

    template <class Iterator>
    boost::uint32_t parse_uint32(Iterator const & i)
    {
        return i[3] | i[2]<<8 | i[1]<<16 | i[0]<<24;
    }

    template <class Iterator>
    void write_uint32(Iterator const & i, boost::uint32_t v)
    {
        i[0] = ( v >> 24 ) & 0xff;
        i[1] = ( v >> 16 ) & 0xff;
        i[2] = ( v >>  8 ) & 0xff;
        i[3] = ( v       ) & 0xff;
    }

    ///////////////////////////////////////////////////////////////////////////
    // bitfield extraction

    template <class Iterator, unsigned offset, unsigned endb, unsigned start, unsigned end>
    struct parse_bitfield_i
    {
        static boost::uint32_t parse(Iterator const & i) {
            return ( ( ( parse_uint32(i+offset+1)>>(40-end) ) // Beware of sign extension !!
                       & boost::low_bits_mask_t<32-(40-end)>::sig_bits )
sbund's avatar
sbund committed
                     | (i[offset]<<(32-(40-end))) )
                & boost::low_bits_mask_t<end-start>::sig_bits;
        }

        static void write(Iterator const & i, boost::uint32_t v) {
            write_uint32(i+offset+1,
sbund's avatar
sbund committed
                         (parse_uint32(i+offset+1) & ~(boost::low_bits_mask_t<end-8>::sig_bits<<(40-end)))
                         | ((v & boost::low_bits_mask_t<end-8>::sig_bits) << (40-end)));
            i[offset] = (i[offset] & ~(boost::low_bits_mask_t<8-start>::sig_bits))
                | ((v>>(end-8)) & boost::low_bits_mask_t<8-start>::sig_bits);
        }
    };

    template <class Iterator, unsigned offset, unsigned start, unsigned end>
    struct parse_bitfield_i<Iterator, offset, 3, start, end>
    {
        static boost::uint32_t parse(Iterator const & i) {
            return ( parse_uint32(i+offset)>>(32-end) )
                & boost::low_bits_mask_t<end-start>::sig_bits;
        }

        static void write(Iterator const & i, boost::uint32_t v) {
            write_uint32(i+offset,
sbund's avatar
sbund committed
                         (parse_uint32(i+offset) & ~(boost::low_bits_mask_t<end-start>::sig_bits<<(32-end)))
                         | ((v & boost::low_bits_mask_t<end-start>::sig_bits) << (32-end)));
        }
    };

    template <class Iterator, unsigned offset, unsigned start, unsigned end>
    struct parse_bitfield_i<Iterator, offset, 2, start, end>
    {
        static boost::uint32_t parse(Iterator const & i) {
            return ( parse_uint24(i+offset)>>(24-end) )
                & boost::low_bits_mask_t<end-start>::sig_bits;
        }

        static void write(Iterator const & i, boost::uint32_t v) {
            write_uint24(i+offset,
sbund's avatar
sbund committed
                         (parse_uint24(i+offset) & ~(boost::low_bits_mask_t<end-start>::sig_bits<<(24-end)))
                         | ((v & boost::low_bits_mask_t<end-start>::sig_bits) << (24-end)));
        }
    };

    template <class Iterator, unsigned offset, unsigned start, unsigned end>
    struct parse_bitfield_i<Iterator, offset, 1, start, end>
    {
        static boost::uint32_t parse(Iterator const & i) {
            return ( parse_uint16(i+offset)>>(16-end) )
                & boost::low_bits_mask_t<end-start>::sig_bits;
        }

        static void write(Iterator const & i, boost::uint32_t v) {
            write_uint16(i+offset,
sbund's avatar
sbund committed
                         (parse_uint16(i+offset) & ~(boost::low_bits_mask_t<end-start>::sig_bits<<(16-end)))
                         | ((v & boost::low_bits_mask_t<end-start>::sig_bits) << (16-end)));
        }
    };

    template <class Iterator, unsigned offset, unsigned start, unsigned end>
    struct parse_bitfield_i<Iterator, offset, 0, start, end>
    {
        static boost::uint32_t parse(Iterator const & i) {
            return ( i[offset]>>(8-end) )
sbund's avatar
sbund committed
                & boost::low_bits_mask_t<end-start>::sig_bits;
        }

        static void write(Iterator const & i, boost::uint32_t v) {
            i[offset] = (i[offset] & ~(boost::low_bits_mask_t<end-start>::sig_bits<<(8-end)))
                | ((v & boost::low_bits_mask_t<end-start>::sig_bits) << (8-end));
        }
    };

    template <class Iterator, unsigned start, unsigned end>
    struct parse_bitfield
sbund's avatar
sbund committed
        : public parse_bitfield_i<Iterator,start/8,(end-1)/8-start/8,start%8,end-8*(start/8)>
    {};

sbund's avatar
sbund committed

///////////////////////////////ih.e////////////////////////////////////////
#endif


// Local Variables:
// mode: c++
// fill-column: 100
// c-file-style: "senf"
// indent-tabs-mode: nil
// ispell-local-dictionary: "american"
// compile-command: "scons -u test"
// comment-column: 40
sbund's avatar
sbund committed
// End: