Avro C++
Parser.hh
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * https://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #ifndef avro_Parser_hh__
20 #define avro_Parser_hh__
21 
22 #include "Config.hh"
23 #include "Reader.hh"
24 
25 #include <array>
26 
27 namespace avro {
28 
33 
34 template<class Reader>
35 class Parser : private boost::noncopyable
36 {
37 
38  public:
39 
40  // Constructor only works with Writer
41  explicit Parser(const InputBuffer &in) :
42  reader_(in)
43  {}
44 
46  Parser(const ValidSchema &schema, const InputBuffer &in) :
47  reader_(schema, in)
48  {}
49 
50  void readNull() {
51  Null null;
52  reader_.readValue(null);
53  }
54 
55  bool readBool() {
56  bool val;
57  reader_.readValue(val);
58  return val;
59  }
60 
61  int32_t readInt() {
62  int32_t val;
63  reader_.readValue(val);
64  return val;
65  }
66 
67  int64_t readLong() {
68  int64_t val;
69  reader_.readValue(val);
70  return val;
71  }
72 
73  float readFloat() {
74  float val;
75  reader_.readValue(val);
76  return val;
77  }
78 
79  double readDouble() {
80  double val;
81  reader_.readValue(val);
82  return val;
83  }
84 
85  void readString(std::string &val) {
86  reader_.readValue(val);
87  }
88 
89  void readBytes(std::vector<uint8_t> &val) {
90  reader_.readBytes(val);
91  }
92 
93  template <size_t N>
94  void readFixed(uint8_t (&val)[N]) {
95  reader_.readFixed(val);
96  }
97 
98  template<size_t N>
99  void readFixed(std::array<uint8_t, N> &val) {
100  reader_.readFixed(val);
101  }
102 
103  void readRecord() {
104  reader_.readRecord();
105  }
106 
107  void readRecordEnd() {
108  reader_.readRecordEnd();
109  }
110 
111  int64_t readArrayBlockSize() {
112  return reader_.readArrayBlockSize();
113  }
114 
115  int64_t readUnion() {
116  return reader_.readUnion();
117  }
118 
119  int64_t readEnum() {
120  return reader_.readEnum();
121  }
122 
123  int64_t readMapBlockSize() {
124  return reader_.readMapBlockSize();
125  }
126 
127  private:
128 
129  friend Type nextType(Parser<ValidatingReader> &p);
130  friend bool currentRecordName(Parser<ValidatingReader> &p, std::string &name);
131  friend bool nextFieldName(Parser<ValidatingReader> &p, std::string &name);
132 
133  Reader reader_;
134 
135 };
136 
137 inline Type nextType(Parser<ValidatingReader> &p) {
138  return p.reader_.nextType();
139 }
140 
141 inline bool currentRecordName(Parser<ValidatingReader> &p, std::string &name) {
142  return p.reader_.currentRecordName(name);
143 }
144 
145 inline bool nextFieldName(Parser<ValidatingReader> &p, std::string &name) {
146  return p.reader_.nextFieldName(name);
147 }
148 
149 } // namespace avro
150 
151 #endif
Type
The "type" for the schema.
Definition: Types.hh:31
A bunch of templates and specializations for encoding and decoding specific types.
Definition: AvroParse.hh:30
Class that wraps a reader or ValidatingReade with an interface that uses explicit get* names instead ...
Definition: Parser.hh:35
define a type to identify Null in template functions
Definition: Types.hh:102
A ValidSchema is basically a non-mutable Schema that has passed some minumum of sanity checks...
Definition: ValidSchema.hh:40
Parser(const ValidSchema &schema, const InputBuffer &in)
Constructor only works with ValidatingWriter.
Definition: Parser.hh:46