Avro C++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator
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  * http://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 
62  static void read(Decoder& d, GenericDatum& g);
63 
67  static void read(Decoder& d, GenericDatum& g, const ValidSchema& s);
68 };
69 
70 
74 class AVRO_DECL GenericWriter : boost::noncopyable {
75  const ValidSchema schema_;
76  const EncoderPtr encoder_;
77 
78  static void write(const GenericDatum& datum, Encoder& e);
79 public:
83  GenericWriter(const ValidSchema& s, const EncoderPtr& encoder);
84 
88  void write(const GenericDatum& datum) const;
89 
93  static void write(Encoder& e, const GenericDatum& g);
94 
99  static void write(Encoder& e, const GenericDatum& g, const ValidSchema&) {
100  write(e, g);
101  }
102 };
103 
104 template <typename T> struct codec_traits;
105 
111 template <> struct codec_traits<std::pair<ValidSchema, GenericDatum> > {
113  static void encode(Encoder& e,
114  const std::pair<ValidSchema, GenericDatum>& p) {
115  GenericWriter::write(e, p.second, p.first);
116  }
117 
119  static void decode(Decoder& d, std::pair<ValidSchema, GenericDatum>& p) {
120  GenericReader::read(d, p.second, p.first);
121  }
122 };
123 
127 template <> struct codec_traits<GenericDatum> {
129  static void encode(Encoder& e, const GenericDatum& g) {
130  GenericWriter::write(e, g);
131  }
132 
134  static void decode(Decoder& d, GenericDatum& g) {
135  GenericReader::read(d, g);
136  }
137 };
138 
139 } // namespace avro
140 #endif
141 
Low level support for encoding avro values.
boost::shared_ptr< Decoder > DecoderPtr
Shared pointer to Decoder.
Definition: Decoder.hh:161
A bunch of templates and specializations for encoding and decoding specific types.
Definition: AvroParse.hh:31
Codec_traits tells avro how to encode and decode an object of given type.
Definition: Generic.hh:104
Definition: Node.hh:180
static void decode(Decoder &d, GenericDatum &g)
Decodes.
Definition: Generic.hh:134
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:99
static void encode(Encoder &e, const GenericDatum &g)
Encodes.
Definition: Generic.hh:129
static void encode(Encoder &e, const std::pair< ValidSchema, GenericDatum > &p)
Encodes.
Definition: Generic.hh:113
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
boost::shared_ptr< Encoder > EncoderPtr
Shared pointer to Encoder.
Definition: Encoder.hh:144
static void decode(Decoder &d, std::pair< ValidSchema, GenericDatum > &p)
Decodes.
Definition: Generic.hh:119
A utility class to write generic datum to encoders.
Definition: Generic.hh:74
The abstract base class for all Avro encoders.
Definition: Encoder.hh:53
Decoder is an interface implemented by every decoder capable of decoding Avro data.
Definition: Decoder.hh:49
Low level support for decoding avro values.
Generic datum which can hold any Avro type.
Definition: GenericDatum.hh:55