Avro C++
Generic.hh
00001 /*
00002  * Licensed to the Apache Software Foundation (ASF) under one
00003  * or more contributor license agreements.  See the NOTICE file
00004  * distributed with this work for additional information
00005  * regarding copyright ownership.  The ASF licenses this file
00006  * to you under the Apache License, Version 2.0 (the
00007  * "License"); you may not use this file except in compliance
00008  * with the License.  You may obtain a copy of the License at
00009  *
00010  *     http://www.apache.org/licenses/LICENSE-2.0
00011  *
00012  * Unless required by applicable law or agreed to in writing, software
00013  * distributed under the License is distributed on an "AS IS" BASIS,
00014  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015  * See the License for the specific language governing permissions and
00016  * limitations under the License.
00017  */
00018 
00019 #ifndef avro_Generic_hh__
00020 #define avro_Generic_hh__
00021 
00022 #include <boost/utility.hpp>
00023 
00024 #include "Config.hh"
00025 #include "Types.hh"
00026 #include "Encoder.hh"
00027 #include "Decoder.hh"
00028 #include "GenericDatum.hh"
00029 
00030 namespace avro {
00034 class AVRO_DECL GenericReader : boost::noncopyable {
00035     const ValidSchema schema_;
00036     const bool isResolving_;
00037     const DecoderPtr decoder_;
00038 
00039     static void read(GenericDatum& datum, Decoder& d, bool isResolving);
00040 public:
00044     GenericReader(const ValidSchema& s, const DecoderPtr& decoder);
00045 
00051     GenericReader(const ValidSchema& writerSchema,
00052         const ValidSchema& readerSchema, const DecoderPtr& decoder);
00053 
00057     void read(GenericDatum& datum) const;
00058 
00062     static void read(Decoder& d, GenericDatum& g);
00063 
00067     static void read(Decoder& d, GenericDatum& g, const ValidSchema& s);
00068 };
00069 
00070 
00074 class AVRO_DECL GenericWriter : boost::noncopyable {
00075     const ValidSchema schema_;
00076     const EncoderPtr encoder_;
00077 
00078     static void write(const GenericDatum& datum, Encoder& e);
00079 public:
00083     GenericWriter(const ValidSchema& s, const EncoderPtr& encoder);
00084 
00088     void write(const GenericDatum& datum) const;
00089 
00093     static void write(Encoder& e, const GenericDatum& g);
00094 
00099     static void write(Encoder& e, const GenericDatum& g, const ValidSchema&) {
00100         write(e, g);
00101     }
00102 };
00103 
00104 template <typename T> struct codec_traits;
00105 
00111 template <> struct codec_traits<std::pair<ValidSchema, GenericDatum> > {
00113     static void encode(Encoder& e,
00114         const std::pair<ValidSchema, GenericDatum>& p) {
00115         GenericWriter::write(e, p.second, p.first);
00116     }
00117 
00119     static void decode(Decoder& d, std::pair<ValidSchema, GenericDatum>& p) {
00120         GenericReader::read(d, p.second, p.first);
00121     }
00122 };
00123 
00127 template <> struct codec_traits<GenericDatum> {
00129     static void encode(Encoder& e, const GenericDatum& g) {
00130         GenericWriter::write(e, g);
00131     }
00132 
00134     static void decode(Decoder& d, GenericDatum& g) {
00135         GenericReader::read(d, g);
00136     }
00137 };
00138     
00139 }   // namespace avro
00140 #endif
00141