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 public:
38  // Constructor only works with Writer
39  explicit Parser(const InputBuffer &in) : reader_(in) {}
40 
42  Parser(const ValidSchema &schema, const InputBuffer &in) : reader_(schema, in) {}
43 
44  void readNull() {
45  Null null;
46  reader_.readValue(null);
47  }
48 
49  bool readBool() {
50  bool val;
51  reader_.readValue(val);
52  return val;
53  }
54 
55  int32_t readInt() {
56  int32_t val;
57  reader_.readValue(val);
58  return val;
59  }
60 
61  int64_t readLong() {
62  int64_t val;
63  reader_.readValue(val);
64  return val;
65  }
66 
67  float readFloat() {
68  float val;
69  reader_.readValue(val);
70  return val;
71  }
72 
73  double readDouble() {
74  double val;
75  reader_.readValue(val);
76  return val;
77  }
78 
79  void readString(std::string &val) {
80  reader_.readValue(val);
81  }
82 
83  void readBytes(std::vector<uint8_t> &val) {
84  reader_.readBytes(val);
85  }
86 
87  template<size_t N>
88  void readFixed(uint8_t (&val)[N]) {
89  reader_.readFixed(val);
90  }
91 
92  template<size_t N>
93  void readFixed(std::array<uint8_t, N> &val) {
94  reader_.readFixed(val);
95  }
96 
97  void readRecord() {
98  reader_.readRecord();
99  }
100 
101  void readRecordEnd() {
102  reader_.readRecordEnd();
103  }
104 
105  int64_t readArrayBlockSize() {
106  return reader_.readArrayBlockSize();
107  }
108 
109  int64_t readUnion() {
110  return reader_.readUnion();
111  }
112 
113  int64_t readEnum() {
114  return reader_.readEnum();
115  }
116 
117  int64_t readMapBlockSize() {
118  return reader_.readMapBlockSize();
119  }
120 
121 private:
122  friend Type nextType(Parser<ValidatingReader> &p);
123  friend bool currentRecordName(Parser<ValidatingReader> &p, std::string &name);
124  friend bool nextFieldName(Parser<ValidatingReader> &p, std::string &name);
125 
126  Reader reader_;
127 };
128 
129 inline Type nextType(Parser<ValidatingReader> &p) {
130  return p.reader_.nextType();
131 }
132 
133 inline bool currentRecordName(Parser<ValidatingReader> &p, std::string &name) {
134  return p.reader_.currentRecordName(name);
135 }
136 
137 inline bool nextFieldName(Parser<ValidatingReader> &p, std::string &name) {
138  return p.reader_.nextFieldName(name);
139 }
140 
141 } // namespace avro
142 
143 #endif
avro
A bunch of templates and specializations for encoding and decoding specific types.
Definition: AvroParse.hh:30
avro::ValidSchema
A ValidSchema is basically a non-mutable Schema that has passed some minimum of sanity checks.
Definition: ValidSchema.hh:40
avro::Null
define a type to represent Avro Null in template functions
Definition: Types.hh:101
avro::Parser::Parser
Parser(const ValidSchema &schema, const InputBuffer &in)
Constructor only works with ValidatingWriter.
Definition: Parser.hh:42
avro::Parser
Class that wraps a reader or ValidatingReade with an interface that uses explicit get* names instead ...
Definition: Parser.hh:35
avro::Type
Type
The "type" for the schema.
Definition: Types.hh:31