aboutsummaryrefslogtreecommitdiff
path: root/c++-libbu++/src/integer.h
blob: dc4ae4c3d06cb974102ff470fa09bfe27d7a3f2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
 * Copyright (C) 2007-2012 Xagasoft, All rights reserved.
 *
 * This file is part of the libgats library and is released under the
 * terms of the license contained in the file LICENSE.
 */

#ifndef GATS_INTEGER_H
#define GATS_INTEGER_H

#include "gats/object.h"

#include <stdint.h>

#include <bu/stream.h>

namespace Gats
{
    class Integer : public Gats::Object
    {
    public:
        Integer();
        Integer( int64_t iVal );
        virtual ~Integer();

        virtual Object *clone() const;

        virtual Type getType() const { return typeInteger; }
        int64_t getValue() const { return iVal; }

        virtual void write( Bu::Stream &rOut ) const;
        virtual void read( Bu::Stream &rIn, char cType );

        template<typename itype>
        static void readPackedInt( Bu::Stream &rStream, itype &rOut )
        {
            int8_t b;
            rOut = 0;
            bool bNeg;

            rStream.read( &b, 1 );
            bNeg = ( b&0x40 );
            rOut |= (itype(b&0x3F));
            int c = 0;
            while( (b&0x80) )
            {
                rStream.read( &b, 1 );
                rOut |= (itype(b&0x7F)) << (6+7*(c++));
            }
            if( bNeg ) rOut = -rOut;
        }

        template<typename itype>
        static void writePackedInt( Bu::Stream &rStream, itype iIn )
        {
            uint8_t b;

            if( iIn < 0 )
            {
                iIn = -iIn;
                b = (iIn&0x3F);
                if( iIn > b )
                    b |= 0x80 | 0x40;
                else
                    b |= 0x40;
            }
            else
            {
                b = (iIn&0x3F);
                if( iIn > b )
                    b |= 0x80;
            }
            rStream.write( &b, 1 );
            iIn = iIn >> 6;

            while( iIn )
            {
                b = (iIn&0x7F);
                if( iIn > b )
                    b |= 0x80;
                rStream.write( &b, 1 );
                iIn = iIn >> 7;
            }
        }

    private:
        int64_t iVal;
    };
};

Bu::Formatter &operator<<( Bu::Formatter &f, const Gats::Integer &i );

#endif