aboutsummaryrefslogtreecommitdiff
path: root/c++-libbu++/src/integer.h
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2012-11-09 16:25:22 +0000
committerMike Buland <eichlan@xagasoft.com>2012-11-09 16:25:22 +0000
commit74dd68ad611d15abf16a65c36a7cfd3f4492930a (patch)
tree843fed9ba6bb03253a01314afc3b1dfbb2dfd26c /c++-libbu++/src/integer.h
parentd9b407475ae3ebe434b29d9eabdd7d4416e17881 (diff)
downloadlibgats-74dd68ad611d15abf16a65c36a7cfd3f4492930a.tar.gz
libgats-74dd68ad611d15abf16a65c36a7cfd3f4492930a.tar.bz2
libgats-74dd68ad611d15abf16a65c36a7cfd3f4492930a.tar.xz
libgats-74dd68ad611d15abf16a65c36a7cfd3f4492930a.zip
Made the repo less libbu++-centric.
Diffstat (limited to 'c++-libbu++/src/integer.h')
-rw-r--r--c++-libbu++/src/integer.h86
1 files changed, 86 insertions, 0 deletions
diff --git a/c++-libbu++/src/integer.h b/c++-libbu++/src/integer.h
new file mode 100644
index 0000000..a5e0d58
--- /dev/null
+++ b/c++-libbu++/src/integer.h
@@ -0,0 +1,86 @@
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
10namespace Gats
11{
12 class Integer : public Gats::Object
13 {
14 public:
15 Integer();
16 Integer( int64_t iVal );
17 virtual ~Integer();
18
19 virtual Object *clone() const;
20
21 virtual Type getType() const { return typeInteger; }
22 int64_t getValue() const { return iVal; }
23
24 virtual void write( Bu::Stream &rOut ) const;
25 virtual void read( Bu::Stream &rIn, char cType );
26
27 template<typename itype>
28 static void readPackedInt( Bu::Stream &rStream, itype &rOut )
29 {
30 int8_t b;
31 rOut = 0;
32 bool bNeg;
33
34 rStream.read( &b, 1 );
35 bNeg = ( b&0x40 );
36 rOut |= (itype(b&0x3F));
37 int c = 0;
38 while( (b&0x80) )
39 {
40 rStream.read( &b, 1 );
41 rOut |= (itype(b&0x7F)) << (6+7*(c++));
42 }
43 if( bNeg ) rOut = -rOut;
44 }
45
46 template<typename itype>
47 static void writePackedInt( Bu::Stream &rStream, itype iIn )
48 {
49 uint8_t b;
50
51 if( iIn < 0 )
52 {
53 iIn = -iIn;
54 b = (iIn&0x3F);
55 if( iIn > b )
56 b |= 0x80 | 0x40;
57 else
58 b |= 0x40;
59 }
60 else
61 {
62 b = (iIn&0x3F);
63 if( iIn > b )
64 b |= 0x80;
65 }
66 rStream.write( &b, 1 );
67 iIn = iIn >> 6;
68
69 while( iIn )
70 {
71 b = (iIn&0x7F);
72 if( iIn > b )
73 b |= 0x80;
74 rStream.write( &b, 1 );
75 iIn = iIn >> 7;
76 }
77 }
78
79 private:
80 int64_t iVal;
81 };
82};
83
84Bu::Formatter &operator<<( Bu::Formatter &f, const Gats::Integer &i );
85
86#endif