Avro C++
Generic.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_Generic_hh__
20 #define avro_Generic_hh__
21 
22 #include <boost/utility.hpp>
23 
24 #include "Config.hh"
25 #include "Types.hh"
26 #include "Encoder.hh"
27 #include "Decoder.hh"
28 #include "GenericDatum.hh"
29 
30 namespace avro {
34 class AVRO_DECL GenericReader : boost::noncopyable {
35  const ValidSchema schema_;
36  const bool isResolving_;
37  const DecoderPtr decoder_;
38 
39  static void read(GenericDatum& datum, Decoder& d, bool isResolving);
40 public:
44  GenericReader(const ValidSchema& s, const DecoderPtr& decoder);
45 
51  GenericReader(const ValidSchema& writerSchema,
52  const ValidSchema& readerSchema, const DecoderPtr& decoder);
53 
57  void read(GenericDatum& datum) const;
58 
64  void drain() {
65  decoder_->drain();
66  }
70  static void read(Decoder& d, GenericDatum& g);
71 
75  static void read(Decoder& d, GenericDatum& g, const ValidSchema& s);
76 };
77 
78 
82 class AVRO_DECL GenericWriter : boost::noncopyable {
83  const ValidSchema schema_;
84  const EncoderPtr encoder_;
85 
86  static void write(const GenericDatum& datum, Encoder& e);
87 public:
91  GenericWriter(const ValidSchema& s, const EncoderPtr& encoder);
92 
96  void write(const GenericDatum& datum) const;
97 
101  static void write(Encoder& e, const GenericDatum& g);
102 
107  static void write(Encoder& e, const GenericDatum& g, const ValidSchema&) {
108  write(e, g);
109  }
110 };
111 
112 template <typename T> struct codec_traits;
113 
119 template <> struct codec_traits<std::pair<ValidSchema, GenericDatum> > {
121  static void encode(Encoder& e,
122  const std::pair<ValidSchema, GenericDatum>& p) {
123  GenericWriter::write(e, p.second, p.first);
124  }
125 
127  static void decode(Decoder& d, std::pair<ValidSchema, GenericDatum>& p) {
128  GenericReader::read(d, p.second, p.first);
129  }
130 };
131 
135 template <> struct codec_traits<GenericDatum> {
137  static void encode(Encoder& e, const GenericDatum& g) {
138  GenericWriter::write(e, g);
139  }
140 
142  static void decode(Decoder& d, GenericDatum& g) {
143  GenericReader::read(d, g);
144  }
145 };
146 
147 } // namespace avro
148 #endif
149 
Low level support for encoding avro values.
A bunch of templates and specializations for encoding and decoding specific types.
Definition: AvroParse.hh:30
Codec_traits tells avro how to encode and decode an object of given type.
Definition: Generic.hh:112
Definition: Node.hh:202
static void decode(Decoder &d, GenericDatum &g)
Decodes.
Definition: Generic.hh:142
static void write(Encoder &e, const GenericDatum &g, const ValidSchema &)
Writes a generic datum on to the stream, using the given schema.
Definition: Generic.hh:107
std::shared_ptr< Encoder > EncoderPtr
Shared pointer to Encoder.
Definition: Encoder.hh:147
std::shared_ptr< Decoder > DecoderPtr
Shared pointer to Decoder.
Definition: Decoder.hh:177
static void encode(Encoder &e, const GenericDatum &g)
Encodes.
Definition: Generic.hh:137
static void encode(Encoder &e, const std::pair< ValidSchema, GenericDatum > &p)
Encodes.
Definition: Generic.hh:121
A utility class to read generic datum from decoders.
Definition: Generic.hh:34
A ValidSchema is basically a non-mutable Schema that has passed some minumum of sanity checks...
Definition: ValidSchema.hh:40
static void decode(Decoder &d, std::pair< ValidSchema, GenericDatum > &p)
Decodes.
Definition: Generic.hh:127
A utility class to write generic datum to encoders.
Definition: Generic.hh:82
The abstract base class for all Avro encoders.
Definition: Encoder.hh:52
Decoder is an interface implemented by every decoder capable of decoding Avro data.
Definition: Decoder.hh:48
Low level support for decoding avro values.
void drain()
Drains any residual bytes in the input stream (e.g.
Definition: Generic.hh:64
Generic datum which can hold any Avro type.
Definition: GenericDatum.hh:61