+ inline void writeInt(uint8_t i) {
+ os.put(BININT1);
+ os.put(i);
+ }
+
+ inline void writeInt(uint16_t i) {
+ if (i < 0x100) {
+ writeInt((uint8_t)i);
+ } else {
+ os.put(BININT2);
+ putInt16(i);
+ }
+ }
+
+ inline void writeInt(int32_t i) {
+ if (0 <= i && i < 0x10000) {
+ writeInt((uint16_t)i);
+ } else {
+ os.put(BININT);
+ putInt32(i);
+ }
+ }
+
+ inline void writeInt(uint32_t i) {
+ if (i < 0x8000000) {
+ writeInt((int32_t)i);
+ } else {
+ writeLong(i);
+ }
+ }
+
+ inline void writeInt(long long i) {
+ if (-0x8000000 <= i && i < 0x8000000) {
+ writeInt((int32_t)i);
+ } else {
+ writeLong(i);
+ }
+ }
+
+ inline void writeInt(unsigned long long i) {
+ if (i < 0x8000000) {
+ writeInt((int32_t)i);
+ } else {
+ writeLong(i);
+ }
+ }
+
+ inline void writeFloat(double f) {
+ union {
+ double f;
+ char c[8];
+ } u;
+
+ assert(sizeof u.f == sizeof u.c);
+ u.f = f;
+
+ os.put(BINFLOAT);
+ os.put(u.c[7]);
+ os.put(u.c[6]);
+ os.put(u.c[5]);
+ os.put(u.c[4]);
+ os.put(u.c[3]);
+ os.put(u.c[2]);
+ os.put(u.c[1]);
+ os.put(u.c[0]);
+ }
+
+ inline void writeByteArray(const void *buf, size_t length) {
+ os.put(GLOBAL);
+ os << "__builtin__\nbytearray\n";
+ os.put(BINPUT);
+ os.put(1);
+ writeString(static_cast<const char *>(buf), length);
+ os.put(TUPLE1);
+ os.put(REDUCE);
+ }
+
+protected:
+ inline void putInt16(uint16_t i) {
+ os.put( i & 0xff);
+ os.put( i >> 8 );
+ }
+
+ inline void putInt32(uint32_t i) {
+ os.put( i & 0xff);
+ os.put((i >> 8) & 0xff);
+ os.put((i >> 16) & 0xff);
+ os.put( i >> 24 );