Reader.hh
00001
00019 #ifndef avro_Reader_hh__
00020 #define avro_Reader_hh__
00021
00022 #include <stdint.h>
00023 #include <vector>
00024 #include <boost/noncopyable.hpp>
00025
00026 #include "InputStreamer.hh"
00027 #include "Zigzag.hh"
00028 #include "Types.hh"
00029
00030 namespace avro {
00031
00036
00037 class Reader : private boost::noncopyable
00038 {
00039
00040 public:
00041
00042 explicit Reader(InputStreamer &in) :
00043 in_(in)
00044 {}
00045
00046 void readValue(Null &) {}
00047
00048 void readValue(bool &val) {
00049 uint8_t ival;
00050 in_.readByte(ival);
00051 val = (ival != 0);
00052 }
00053
00054 void readValue(int32_t &val) {
00055 uint32_t encoded = readVarInt();
00056 val = decodeZigzag32(encoded);
00057 }
00058
00059 void readValue(int64_t &val) {
00060 uint64_t encoded = readVarInt();
00061 val = decodeZigzag64(encoded);
00062 }
00063
00064 void readValue(float &val) {
00065 union {
00066 float f;
00067 uint32_t i;
00068 } v;
00069 in_.readWord(v.i);
00070 val = v.f;
00071 }
00072
00073 void readValue(double &val) {
00074 union {
00075 double d;
00076 uint64_t i;
00077 } v;
00078 in_.readLongWord(v.i);
00079 val = v.d;
00080 }
00081
00082 void readValue(std::string &val) {
00083 int64_t size = readSize();
00084 val.clear();
00085 val.reserve(size);
00086 uint8_t bval;
00087 for(size_t bytes = 0; bytes < static_cast<size_t>(size); bytes++) {
00088 in_.readByte(bval);
00089 val.push_back(bval);
00090 }
00091 }
00092
00093 void readBytes(std::vector<uint8_t> &val) {
00094 int64_t size = readSize();
00095
00096 val.reserve(size);
00097 uint8_t bval;
00098 for(size_t bytes = 0; bytes < static_cast<size_t>(size); bytes++) {
00099 in_.readByte(bval);
00100 val.push_back(bval);
00101 }
00102 }
00103
00104 void readFixed(uint8_t *val, size_t size) {
00105 for(size_t bytes = 0; bytes < size; bytes++) {
00106 in_.readByte(val[bytes]);
00107 }
00108 }
00109
00110 template <size_t N>
00111 void readFixed(uint8_t (&val)[N]) {
00112 readFixed(val, N);
00113 }
00114
00115 template <size_t N>
00116 void readFixed(boost::array<uint8_t, N> &val) {
00117 readFixed(val.c_array(), N);
00118 }
00119
00120 void readRecord() { }
00121
00122 int64_t readArrayBlockSize() {
00123 return readSize();
00124 }
00125
00126 int64_t readUnion() {
00127 return readSize();
00128 }
00129
00130 int64_t readEnum() {
00131 return readSize();
00132 }
00133
00134 int64_t readMapBlockSize() {
00135 return readSize();
00136 }
00137
00138 private:
00139
00140 int64_t readSize() {
00141 int64_t size(0);
00142 readValue(size);
00143 return size;
00144 }
00145
00146 uint64_t readVarInt() {
00147 uint64_t encoded = 0;
00148 uint8_t val = 0;
00149 int shift = 0;
00150 do {
00151 in_.readByte(val);
00152 uint64_t newbits = static_cast<uint64_t>(val & 0x7f) << shift;
00153 encoded |= newbits;
00154 shift += 7;
00155 } while (val & 0x80);
00156
00157 return encoded;
00158 }
00159
00160 InputStreamer &in_;
00161
00162 };
00163
00164
00165 }
00166
00167 #endif