From 74dd68ad611d15abf16a65c36a7cfd3f4492930a Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Fri, 9 Nov 2012 16:25:22 +0000 Subject: Made the repo less libbu++-centric. --- c++-libbu++/src/integer.h | 86 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 c++-libbu++/src/integer.h (limited to 'c++-libbu++/src/integer.h') 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 @@ +#ifndef GATS_INTEGER_H +#define GATS_INTEGER_H + +#include "gats/object.h" + +#include + +#include + +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 + 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 + 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 -- cgit v1.2.3