Encoder.hh

Go to the documentation of this file.
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_Encoder_hh__
00020 #define avro_Encoder_hh__
00021 
00022 #include <stdint.h>
00023 #include <string>
00024 #include <vector>
00025 
00026 #include "ValidSchema.hh"
00027 #include "Stream.hh"
00028 
00029 #include <boost/shared_ptr.hpp>
00030 
00044 
00045 namespace avro {
00046 
00052 class Encoder {
00053 public:
00054     virtual ~Encoder() { };
00058     virtual void init(OutputStream& os) = 0;
00059 
00061     virtual void flush() = 0;
00062 
00064     virtual void encodeNull() = 0;
00065 
00067     virtual void encodeBool(bool b) = 0;
00068 
00070     virtual void encodeInt(int32_t i) = 0;
00071 
00073     virtual void encodeLong(int64_t l) = 0;
00074 
00076     virtual void encodeFloat(float f) = 0;
00077 
00079     virtual void encodeDouble(double d) = 0;
00080 
00082     virtual void encodeString(const std::string& s) = 0;
00083 
00090     virtual void encodeBytes(const uint8_t *bytes, size_t len) = 0;
00091 
00097     void encodeBytes(const std::vector<uint8_t>& bytes) {
00098         encodeBytes(&bytes[0], bytes.size());
00099     }
00100 
00102     virtual void encodeFixed(const uint8_t *bytes, size_t len) = 0;
00103 
00109     void encodeFixed(const std::vector<uint8_t>& bytes) {
00110         encodeFixed(&bytes[0], bytes.size());
00111     }
00112 
00114     virtual void encodeEnum(size_t e) = 0;
00115 
00117     virtual void arrayStart() = 0;
00118 
00120     virtual void arrayEnd() = 0;
00121 
00123     virtual void mapStart() = 0;
00124 
00126     virtual void mapEnd() = 0;
00127 
00130     virtual void setItemCount(size_t count) = 0;
00131 
00133     virtual void startItem() = 0;
00134 
00136     virtual void encodeUnionIndex(size_t e) = 0;
00137 };
00138 
00142 typedef boost::shared_ptr<Encoder> EncoderPtr;
00143 
00147 EncoderPtr binaryEncoder();
00148 
00153 EncoderPtr validatingEncoder(const ValidSchema& schema,
00154     const EncoderPtr& base);
00155 
00159 EncoderPtr jsonEncoder(const ValidSchema& schema);
00160 
00161 }   // namespace avro
00162 
00163 #endif