ValidatingReader.hh

00001 
00019 #ifndef avro_ValidatingReader_hh__
00020 #define avro_ValidatingReader_hh__
00021 
00022 #include <stdint.h>
00023 #include <vector>
00024 #include <boost/noncopyable.hpp>
00025 
00026 #include "Reader.hh"
00027 #include "Validator.hh"
00028 #include "AvroTraits.hh"
00029 
00030 namespace avro {
00031 
00032 class ValidSchema;
00033 class InputStreamer;
00034 
00044 
00045 class ValidatingReader : private boost::noncopyable
00046 {
00047 
00048   public:
00049 
00050     ValidatingReader(const ValidSchema &schema, InputStreamer &in);
00051 
00052     template<typename T>
00053     void readValue(T &val) {
00054         checkSafeToGet(type_to_avro<T>::type);
00055         reader_.readValue(val);
00056         validator_.advance();
00057     }
00058 
00059     void readBytes(std::vector<uint8_t> &val) {
00060         checkSafeToGet(AVRO_BYTES);
00061         validator_.advance();
00062         reader_.readBytes(val);
00063     }
00064 
00065     void readFixed(uint8_t *val, size_t size) {
00066         checkSafeToGet(AVRO_FIXED);
00067         checkSizeExpected(size);
00068         validator_.advance();
00069         reader_.readFixed(val, size);
00070     }
00071 
00072     template <size_t N>
00073     void readFixed(uint8_t (&val)[N]) {
00074         readFixed(val, N);
00075     }
00076 
00077     template<size_t N>
00078     void readFixed(boost::array<uint8_t, N> &val) {
00079         readFixed(val.c_array(), N);
00080     }
00081 
00082     void readRecord();
00083 
00084     int64_t readArrayBlockSize();
00085 
00086     int64_t readUnion();
00087 
00088     int64_t readEnum();
00089 
00090     int64_t readMapBlockSize();
00091 
00092     Type nextType() const{
00093         return validator_.nextTypeExpected();
00094     }
00095 
00096     bool currentRecordName(std::string &name) const {
00097         return validator_.getCurrentRecordName(name);
00098     }
00099 
00100     bool nextFieldName(std::string &name) const {
00101         return validator_.getNextFieldName(name);
00102     }
00103 
00104   private:
00105 
00106     int64_t readCount();
00107 
00108     void checkSafeToGet(Type type) const {
00109         if(validator_.nextTypeExpected() != type) {
00110             throw Exception("Type does not match");
00111         }
00112     }
00113 
00114     void checkSizeExpected(int size) const {
00115         if(validator_.nextSizeExpected() != size) {
00116             throw Exception("Wrong size of for fixed");
00117         }
00118     }
00119 
00120     Validator validator_;
00121     Reader reader_;
00122 };
00123 
00124 } // namespace avro
00125 
00126 #endif

Generated on Tue Feb 23 15:31:29 2010 for Avro C++ by  doxygen 1.6.1