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 "Zigzag.hh"
00027 #include "Types.hh"
00028 #include "Validator.hh"
00029 #include "buffer/BufferReader.hh"
00030 
00031 namespace avro {
00032 
00037 
00038 template<class ValidatorType>
00039 class ReaderImpl : private boost::noncopyable
00040 {
00041 
00042   public:
00043 
00044     explicit ReaderImpl(const InputBuffer &buffer) :
00045         reader_(buffer)
00046     {}
00047 
00048     ReaderImpl(const ValidSchema &schema, const InputBuffer &buffer) :
00049         validator_(schema),
00050         reader_(buffer)
00051     {}
00052 
00053     void readValue(Null &) {
00054         validator_.checkTypeExpected(AVRO_NULL);
00055     }
00056 
00057     void readValue(bool &val) {
00058         validator_.checkTypeExpected(AVRO_BOOL);
00059         uint8_t ival = 0;
00060         reader_.read(ival);
00061         val = (ival != 0);
00062     }
00063 
00064     void readValue(int32_t &val) {
00065         validator_.checkTypeExpected(AVRO_INT);
00066         uint32_t encoded = readVarInt();
00067         val = decodeZigzag32(encoded);
00068     }
00069 
00070     void readValue(int64_t &val) {
00071         validator_.checkTypeExpected(AVRO_LONG);
00072         uint64_t encoded = readVarInt();
00073         val = decodeZigzag64(encoded);
00074     }
00075 
00076     void readValue(float &val) {
00077         validator_.checkTypeExpected(AVRO_FLOAT);
00078         union { 
00079             float f;
00080             uint32_t i;
00081         } v;
00082         reader_.read(v.i);
00083         val = v.f;
00084     }
00085 
00086     void readValue(double &val) {
00087         validator_.checkTypeExpected(AVRO_DOUBLE);
00088         union { 
00089             double d;
00090             uint64_t i;
00091         } v;
00092         reader_.read(v.i);
00093         val = v.d;
00094     }
00095 
00096     void readValue(std::string &val) {
00097         validator_.checkTypeExpected(AVRO_STRING);
00098         int64_t size = readSize();
00099         reader_.read(val, size);
00100     }
00101 
00102     void readBytes(std::vector<uint8_t> &val) {
00103         validator_.checkTypeExpected(AVRO_BYTES);
00104         int64_t size = readSize();
00105         val.resize(size);
00106         reader_.read(reinterpret_cast<char *>(&val[0]), size);
00107     }
00108 
00109     void readFixed(uint8_t *val, size_t size) {
00110         validator_.checkFixedSizeExpected(size);
00111         reader_.read(reinterpret_cast<char *>(val), size);
00112     }
00113 
00114     template <size_t N>
00115     void readFixed(uint8_t (&val)[N]) {
00116         this->readFixed(val, N);
00117     }
00118   
00119     template <size_t N>
00120     void readFixed(boost::array<uint8_t, N> &val) {
00121         this->readFixed(val.c_array(), N);
00122     }
00123   
00124     void readRecord() { 
00125         validator_.checkTypeExpected(AVRO_RECORD);
00126         validator_.checkTypeExpected(AVRO_LONG);
00127         validator_.setCount(1);
00128     }
00129 
00130     void readRecordEnd() { 
00131         validator_.checkTypeExpected(AVRO_RECORD);
00132         validator_.checkTypeExpected(AVRO_LONG);
00133         validator_.setCount(0);
00134     }
00135 
00136     int64_t readArrayBlockSize() {
00137         validator_.checkTypeExpected(AVRO_ARRAY);
00138         return readCount();
00139     }
00140 
00141     int64_t readUnion() { 
00142         validator_.checkTypeExpected(AVRO_UNION);
00143         return readCount();
00144     }
00145 
00146     int64_t readEnum() {
00147         validator_.checkTypeExpected(AVRO_ENUM);
00148         return readCount();
00149     }
00150 
00151     int64_t readMapBlockSize() {
00152         validator_.checkTypeExpected(AVRO_MAP);
00153         return readCount();
00154     }
00155 
00156     Type nextType() const {
00157         return validator_.nextTypeExpected();
00158     }
00159 
00160     bool currentRecordName(std::string &name) const {
00161         return validator_.getCurrentRecordName(name);
00162     }
00163 
00164     bool nextFieldName(std::string &name) const {
00165         return validator_.getNextFieldName(name);
00166     }
00167 
00168   private:
00169 
00170     uint64_t readVarInt() {
00171         uint64_t encoded = 0;
00172         uint8_t val = 0;
00173         int shift = 0;
00174         do {
00175             reader_.read(val);
00176             uint64_t newbits = static_cast<uint64_t>(val & 0x7f) << shift;
00177             encoded |= newbits;
00178             shift += 7;
00179         } while (val & 0x80);
00180 
00181         return encoded;
00182     }
00183 
00184     int64_t readSize() {
00185         uint64_t encoded = readVarInt();
00186         int64_t size = decodeZigzag64(encoded);
00187         return size;
00188     }
00189 
00190     int64_t readCount() {
00191         validator_.checkTypeExpected(AVRO_LONG);
00192         int64_t count = readSize();
00193         validator_.setCount(count);
00194         return count;
00195     }
00196 
00197     ValidatorType validator_;
00198     BufferReader  reader_;
00199 
00200 };
00201 
00202 typedef ReaderImpl<NullValidator> Reader;
00203 typedef ReaderImpl<Validator> ValidatingReader;
00204 
00205 } // namespace avro
00206 
00207 #endif
Generated on Fri Oct 8 14:36:18 2010 for Avro C++ by  doxygen 1.6.3