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         val.resize(size);
00096         in_.readBytes(&val[0], size);
00097     }
00098 
00099     void readFixed(uint8_t *val, size_t size) {
00100         in_.readBytes(val, size);
00101     }
00102 
00103     template <size_t N>
00104     void readFixed(uint8_t (&val)[N]) {
00105         readFixed(val, N);
00106     }
00107   
00108     template <size_t N>
00109     void readFixed(boost::array<uint8_t, N> &val) {
00110         readFixed(val.c_array(), N);
00111     }
00112   
00113     void readRecord() { }
00114 
00115     int64_t readArrayBlockSize() {
00116         return readSize();
00117     }
00118 
00119     int64_t readUnion() { 
00120         return readSize();
00121     }
00122 
00123     int64_t readEnum() {
00124         return readSize();
00125     }
00126 
00127     int64_t readMapBlockSize() {
00128         return readSize();
00129     }
00130 
00131   private:
00132 
00133     int64_t readSize() {
00134         int64_t size(0);
00135         readValue(size);
00136         return size;
00137     }
00138 
00139     uint64_t readVarInt() {
00140         uint64_t encoded = 0;
00141         uint8_t val = 0;
00142         int shift = 0;
00143         do {
00144             in_.readByte(val);
00145             uint64_t newbits = static_cast<uint64_t>(val & 0x7f) << shift;
00146             encoded |= newbits;
00147             shift += 7;
00148         } while (val & 0x80);
00149 
00150         return encoded;
00151     }
00152 
00153     InputStreamer &in_;
00154 
00155 };
00156 
00157 
00158 } // namespace avro
00159 
00160 #endif
Generated on Mon Jun 7 02:59:20 2010 for Avro C++ by  doxygen 1.6.3