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 "Decoder.hh"
26 #include "Encoder.hh"
27 #include "GenericDatum.hh"
28 #include "Types.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 
41 public:
45  GenericReader(ValidSchema s, const DecoderPtr &decoder);
46 
52  GenericReader(const ValidSchema &writerSchema,
53  const ValidSchema &readerSchema, const DecoderPtr &decoder);
54 
58  void read(GenericDatum &datum) const;
59 
65  void drain() {
66  decoder_->drain();
67  }
71  static void read(Decoder &d, GenericDatum &g);
72 
76  static void read(Decoder &d, GenericDatum &g, const ValidSchema &s);
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 
88 public:
93 
97  void write(const GenericDatum &datum) const;
98 
102  static void write(Encoder &e, const GenericDatum &g);
103 
108  static void write(Encoder &e, const GenericDatum &g, const ValidSchema &) {
109  write(e, g);
110  }
111 };
112 
113 template<typename T>
115 
121 template<>
122 struct codec_traits<std::pair<ValidSchema, GenericDatum>> {
124  static void encode(Encoder &e,
125  const std::pair<ValidSchema, GenericDatum> &p) {
126  GenericWriter::write(e, p.second, p.first);
127  }
128 
130  static void decode(Decoder &d, std::pair<ValidSchema, GenericDatum> &p) {
131  GenericReader::read(d, p.second, p.first);
132  }
133 };
134 
138 template<>
141  static void encode(Encoder &e, const GenericDatum &g) {
142  GenericWriter::write(e, g);
143  }
144 
146  static void decode(Decoder &d, GenericDatum &g) {
147  GenericReader::read(d, g);
148  }
149 };
150 
151 } // namespace avro
152 #endif
avro::codec_traits< GenericDatum >::decode
static void decode(Decoder &d, GenericDatum &g)
Decodes.
Definition: Generic.hh:146
avro::codec_traits
Codec_traits tells avro how to encode and decode an object of given type.
Definition: Generic.hh:114
avro::Encoder
The abstract base class for all Avro encoders.
Definition: Encoder.hh:52
avro::GenericReader::drain
void drain()
Drains any residual bytes in the input stream (e.g.
Definition: Generic.hh:65
avro::codec_traits< GenericDatum >::encode
static void encode(Encoder &e, const GenericDatum &g)
Encodes.
Definition: Generic.hh:141
avro::Decoder
Decoder is an interface implemented by every decoder capable of decoding Avro data.
Definition: Decoder.hh:48
avro::DecoderPtr
std::shared_ptr< Decoder > DecoderPtr
Shared pointer to Decoder.
Definition: Decoder.hh:177
avro::GenericDatum
Generic datum which can hold any Avro type.
Definition: GenericDatum.hh:61
avro::GenericWriter
A utility class to write generic datum to encoders.
Definition: Generic.hh:82
avro::GenericReader
A utility class to read generic datum from decoders.
Definition: Generic.hh:34
avro::codec_traits< std::pair< ValidSchema, GenericDatum > >::encode
static void encode(Encoder &e, const std::pair< ValidSchema, GenericDatum > &p)
Encodes.
Definition: Generic.hh:124
avro
A bunch of templates and specializations for encoding and decoding specific types.
Definition: AvroParse.hh:30
avro::EncoderPtr
std::shared_ptr< Encoder > EncoderPtr
Shared pointer to Encoder.
Definition: Encoder.hh:147
avro::ValidSchema
A ValidSchema is basically a non-mutable Schema that has passed some minimum of sanity checks.
Definition: ValidSchema.hh:40
avro::codec_traits< std::pair< ValidSchema, GenericDatum > >::decode
static void decode(Decoder &d, std::pair< ValidSchema, GenericDatum > &p)
Decodes.
Definition: Generic.hh:130
Decoder.hh
Encoder.hh
avro::GenericWriter::write
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:108