diff options
Diffstat (limited to '')
-rw-r--r-- | src/integer.h | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/src/integer.h b/src/integer.h new file mode 100644 index 0000000..6ed7c49 --- /dev/null +++ b/src/integer.h | |||
@@ -0,0 +1,78 @@ | |||
1 | #ifndef GATS_INTEGER_H | ||
2 | #define GATS_INTEGER_H | ||
3 | |||
4 | #include "gats/object.h" | ||
5 | |||
6 | #include <stdint.h> | ||
7 | |||
8 | #include <bu/stream.h> | ||
9 | |||
10 | namespace Gats | ||
11 | { | ||
12 | class Integer : public Gats::Object | ||
13 | { | ||
14 | public: | ||
15 | Integer(); | ||
16 | Integer( int64_t iVal ); | ||
17 | virtual ~Integer(); | ||
18 | |||
19 | virtual Type getType() const { return typeInteger; } | ||
20 | int64_t getValue() const { return iVal; } | ||
21 | |||
22 | virtual void write( Bu::Stream &rOut ) const; | ||
23 | virtual void read( Bu::Stream &rIn, char cType ); | ||
24 | |||
25 | template<typename itype> | ||
26 | static void readPackedInt( Bu::Stream &rStream, itype &rOut ) | ||
27 | { | ||
28 | int8_t b; | ||
29 | rOut = 0; | ||
30 | bool bNeg; | ||
31 | |||
32 | rStream.read( &b, 1 ); | ||
33 | bNeg = ( b&0x40 ); | ||
34 | rOut |= (itype(b&0x3F)); | ||
35 | int c = 0; | ||
36 | while( (b&0x80) ) | ||
37 | { | ||
38 | rStream.read( &b, 1 ); | ||
39 | rOut |= (itype(b&0x7F)) << (6+7*(c++)); | ||
40 | } | ||
41 | if( bNeg ) rOut = -rOut; | ||
42 | } | ||
43 | |||
44 | template<typename itype> | ||
45 | static void writePackedInt( Bu::Stream &rStream, itype iIn ) | ||
46 | { | ||
47 | uint8_t b; | ||
48 | |||
49 | if( iIn < 0 ) | ||
50 | { | ||
51 | iIn = -iIn; | ||
52 | b = (iIn&0x3F) | 0x40; | ||
53 | } | ||
54 | else | ||
55 | { | ||
56 | b = (iIn&0x3F); | ||
57 | } | ||
58 | if( iIn > b ) | ||
59 | b |= 0x80; | ||
60 | rStream.write( &b, 1 ); | ||
61 | iIn = iIn >> 6; | ||
62 | |||
63 | while( iIn ) | ||
64 | { | ||
65 | b = (iIn&0x7F); | ||
66 | if( iIn > b ) | ||
67 | b |= 0x80; | ||
68 | rStream.write( &b, 1 ); | ||
69 | iIn = iIn >> 7; | ||
70 | } | ||
71 | } | ||
72 | |||
73 | private: | ||
74 | int64_t iVal; | ||
75 | }; | ||
76 | }; | ||
77 | |||
78 | #endif | ||