Validator.hh

00001 
00019 #ifndef avro_Validating_hh__
00020 #define avro_Validating_hh__
00021 
00022 #include <boost/noncopyable.hpp>
00023 #include <vector>
00024 #include <stdint.h>
00025 
00026 #include "Types.hh"
00027 #include "ValidSchema.hh"
00028 
00029 namespace avro {
00030 
00031 class NullValidator : private boost::noncopyable
00032 {
00033   public:
00034 
00035     explicit NullValidator(const ValidSchema &schema) {}
00036     NullValidator() {}
00037 
00038     void setCount(int64_t val) {}
00039 
00040     bool typeIsExpected(Type type) const {
00041         return true;
00042     }
00043 
00044     Type nextTypeExpected() const {
00045         return AVRO_UNKNOWN;
00046     }
00047 
00048     int nextSizeExpected() const {
00049         return 0;
00050     }
00051 
00052     bool getCurrentRecordName(std::string &name) const {
00053         return true;
00054     }
00055 
00056     bool getNextFieldName(std::string &name) const {
00057         return true;
00058     }
00059 
00060     void checkTypeExpected(Type type) { }
00061     void checkFixedSizeExpected(int size) { }
00062 
00063 
00064 };
00065 
00071 
00072 class Validator : private boost::noncopyable
00073 {
00074   public:
00075 
00076     explicit Validator(const ValidSchema &schema);
00077 
00078     void setCount(int64_t val);
00079 
00080     bool typeIsExpected(Type type) const {
00081         return (expectedTypesFlag_ & typeToFlag(type));
00082     }
00083 
00084     Type nextTypeExpected() const {
00085         return nextType_;
00086     }
00087 
00088     int nextSizeExpected() const;
00089 
00090     bool getCurrentRecordName(std::string &name) const;
00091     bool getNextFieldName(std::string &name) const;
00092 
00093     void checkTypeExpected(Type type) {
00094         if(! typeIsExpected(type)) {
00095             throw Exception(
00096                 boost::format("Type %1% does not match schema %2%") 
00097                     % type % nextType_
00098             );
00099         }
00100         advance();
00101     }
00102 
00103     void checkFixedSizeExpected(int size) { 
00104         if( nextSizeExpected() != size) {
00105             throw Exception(
00106                 boost::format("Wrong size for fixed, got %1%, expected %2%") 
00107                     % size % nextSizeExpected()
00108             );
00109         }
00110         checkTypeExpected(AVRO_FIXED);
00111     }
00112 
00113   private:
00114 
00115     typedef uint32_t flag_t;
00116 
00117     flag_t typeToFlag(Type type) const {
00118         flag_t flag = (1L << type);
00119         return flag;
00120     }
00121 
00122     void setupOperation(const NodePtr &node);
00123 
00124     void setWaitingForCount();
00125 
00126     void advance();
00127     void doAdvance();
00128 
00129     void enumAdvance();
00130     bool countingSetup();
00131     void countingAdvance();
00132     void unionAdvance();
00133     void fixedAdvance();
00134 
00135     void setupFlag(Type type);
00136 
00137     const ValidSchema schema_;
00138 
00139     Type nextType_; 
00140     flag_t expectedTypesFlag_;
00141     bool compoundStarted_;
00142     bool waitingForCount_;
00143     int64_t count_;
00144 
00145     struct CompoundType {
00146         explicit CompoundType(const NodePtr &n) :
00147             node(n), pos(0)
00148         {}
00149         NodePtr node;  
00150         size_t  pos;   
00151     };
00152 
00153     std::vector<CompoundType> compoundStack_;
00154     std::vector<size_t> counters_;
00155 
00156 };
00157 
00158 } // namespace avro
00159 
00160 #endif
Generated on Thu Sep 2 18:40:54 2010 for Avro C++ by  doxygen 1.6.3