Avro C++
Parser.hh
00001 /*
00002  * Licensed to the Apache Software Foundation (ASF) under one
00003  * or more contributor license agreements.  See the NOTICE file
00004  * distributed with this work for additional information
00005  * regarding copyright ownership.  The ASF licenses this file
00006  * to you under the Apache License, Version 2.0 (the
00007  * "License"); you may not use this file except in compliance
00008  * with the License.  You may obtain a copy of the License at
00009  *
00010  *     http://www.apache.org/licenses/LICENSE-2.0
00011  *
00012  * Unless required by applicable law or agreed to in writing, software
00013  * distributed under the License is distributed on an "AS IS" BASIS,
00014  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015  * See the License for the specific language governing permissions and
00016  * limitations under the License.
00017  */
00018 
00019 #ifndef avro_Parser_hh__
00020 #define avro_Parser_hh__
00021 
00022 #include "Config.hh"
00023 #include "Reader.hh"
00024 
00025 namespace avro {
00026 
00031 
00032 template<class Reader>
00033 class Parser : private boost::noncopyable
00034 {
00035 
00036   public:
00037 
00038     // Constructor only works with Writer
00039     explicit Parser(const InputBuffer &in) :
00040         reader_(in)
00041     {}
00042 
00044     Parser(const ValidSchema &schema, const InputBuffer &in) :
00045         reader_(schema, in)
00046     {}
00047 
00048     void readNull() {
00049         Null null;
00050         reader_.readValue(null);
00051     }
00052 
00053     bool readBool() {
00054         bool val;
00055         reader_.readValue(val);
00056         return val;
00057     }
00058 
00059     int32_t readInt() {
00060         int32_t val;
00061         reader_.readValue(val);
00062         return val;
00063     }
00064 
00065     int64_t readLong() {
00066         int64_t val;
00067         reader_.readValue(val);
00068         return val;
00069     }
00070 
00071     float readFloat() {
00072         float val;
00073         reader_.readValue(val);
00074         return val;
00075     }
00076 
00077     double readDouble() {
00078         double val;
00079         reader_.readValue(val);
00080         return val;
00081     }
00082 
00083     void readString(std::string &val) {
00084         reader_.readValue(val);
00085     }
00086 
00087     void readBytes(std::vector<uint8_t> &val) {
00088         reader_.readBytes(val);
00089     }
00090 
00091     template <size_t N>
00092     void readFixed(uint8_t (&val)[N]) {
00093         reader_.readFixed(val);
00094     }
00095 
00096     template<size_t N>
00097     void readFixed(boost::array<uint8_t, N> &val) {
00098         reader_.readFixed(val);
00099     }
00100 
00101     void readRecord() { 
00102         reader_.readRecord();
00103     }
00104 
00105     void readRecordEnd() { 
00106         reader_.readRecordEnd();
00107     }
00108 
00109     int64_t readArrayBlockSize() {
00110         return reader_.readArrayBlockSize();
00111     }
00112 
00113     int64_t readUnion() { 
00114         return reader_.readUnion();
00115     }
00116 
00117     int64_t readEnum() {
00118         return reader_.readEnum();
00119     }
00120 
00121     int64_t readMapBlockSize() {
00122         return reader_.readMapBlockSize();
00123     }
00124 
00125   private:
00126 
00127     friend Type nextType(Parser<ValidatingReader> &p);
00128     friend bool currentRecordName(Parser<ValidatingReader> &p, std::string &name);
00129     friend bool nextFieldName(Parser<ValidatingReader> &p, std::string &name);
00130 
00131     Reader reader_;
00132 
00133 };
00134 
00135 inline Type nextType(Parser<ValidatingReader> &p) {
00136     return p.reader_.nextType();
00137 }
00138 
00139 inline bool currentRecordName(Parser<ValidatingReader> &p, std::string &name) {
00140     return p.reader_.currentRecordName(name);
00141 }
00142 
00143 inline bool nextFieldName(Parser<ValidatingReader> &p, std::string &name) {
00144     return p.reader_.nextFieldName(name);
00145 }
00146 
00147 } // namespace avro
00148 
00149 #endif