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 "Config.hh"
00023 #include <stdint.h>
00024 #include <string>
00025 #include <vector>
00026 
00027 #include "ValidSchema.hh"
00028 #include "Stream.hh"
00029 
00030 #include <boost/shared_ptr.hpp>
00031 
00045 
00046 namespace avro {
00047 
00053 class AVRO_DECL Encoder {
00054 public:
00055     virtual ~Encoder() { };
00059     virtual void init(OutputStream& os) = 0;
00060 
00062     virtual void flush() = 0;
00063 
00065     virtual void encodeNull() = 0;
00066 
00068     virtual void encodeBool(bool b) = 0;
00069 
00071     virtual void encodeInt(int32_t i) = 0;
00072 
00074     virtual void encodeLong(int64_t l) = 0;
00075 
00077     virtual void encodeFloat(float f) = 0;
00078 
00080     virtual void encodeDouble(double d) = 0;
00081 
00083     virtual void encodeString(const std::string& s) = 0;
00084 
00091     virtual void encodeBytes(const uint8_t *bytes, size_t len) = 0;
00092 
00098     void encodeBytes(const std::vector<uint8_t>& bytes) {
00099         uint8_t b = 0; 
00100         encodeBytes(bytes.empty() ? &b : &bytes[0], bytes.size());
00101     }
00102 
00104     virtual void encodeFixed(const uint8_t *bytes, size_t len) = 0;
00105 
00111     void encodeFixed(const std::vector<uint8_t>& bytes) {
00112         encodeFixed(&bytes[0], bytes.size());
00113     }
00114 
00116     virtual void encodeEnum(size_t e) = 0;
00117 
00119     virtual void arrayStart() = 0;
00120 
00122     virtual void arrayEnd() = 0;
00123 
00125     virtual void mapStart() = 0;
00126 
00128     virtual void mapEnd() = 0;
00129 
00132     virtual void setItemCount(size_t count) = 0;
00133 
00135     virtual void startItem() = 0;
00136 
00138     virtual void encodeUnionIndex(size_t e) = 0;
00139 };
00140 
00144 typedef boost::shared_ptr<Encoder> EncoderPtr;
00145 
00149 AVRO_DECL EncoderPtr binaryEncoder();
00150 
00155 AVRO_DECL EncoderPtr validatingEncoder(const ValidSchema& schema,
00156     const EncoderPtr& base);
00157 
00161 AVRO_DECL EncoderPtr jsonEncoder(const ValidSchema& schema);
00162 
00163 }   // namespace avro
00164 
00165 #endif