Specific.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_Codec_hh__
00020 #define avro_Codec_hh__
00021 
00022 #include <string>
00023 #include <vector>
00024 #include <map>
00025 #include <algorithm>
00026 
00027 #include "boost/array.hpp"
00028 
00029 #include "Encoder.hh"
00030 #include "Decoder.hh"
00031 
00046 namespace avro {
00047 
00048 template <typename T> void encode(Encoder& e, const T& t);
00049 template <typename T> void decode(Decoder& d, T& t);
00050 
00059 template <typename T>
00060 struct codec_traits {
00061 };
00062 
00066 template <> struct codec_traits<bool> {
00070     static void encode(Encoder& e, bool b) {
00071         e.encodeBool(b);
00072     }
00073 
00077     static void decode(Decoder& d, bool& b) {
00078         b = d.decodeBool();
00079     }
00080 };
00081 
00085 template <> struct codec_traits<int32_t> {
00089     static void encode(Encoder& e, int32_t i) {
00090         e.encodeInt(i);
00091     }
00092 
00096     static void decode(Decoder& d, int32_t& i) {
00097         i = d.decodeInt();
00098     }
00099 };
00100 
00104 template <> struct codec_traits<int64_t> {
00108     static void encode(Encoder& e, int64_t l) {
00109         e.encodeLong(l);
00110     }
00111 
00115     static void decode(Decoder& d, int64_t& l) {
00116         l = d.decodeLong();
00117     }
00118 };
00119 
00123 template <> struct codec_traits<float> {
00127     static void encode(Encoder& e, float f) {
00128         e.encodeFloat(f);
00129     }
00130 
00134     static void decode(Decoder& d, float& f) {
00135         f = d.decodeFloat();
00136     }
00137 };
00138 
00142 template <> struct codec_traits<double> {
00146     static void encode(Encoder& e, double d) {
00147         e.encodeDouble(d);
00148     }
00149 
00153     static void decode(Decoder& d, double& dbl) {
00154         dbl = d.decodeDouble();
00155     }
00156 };
00157 
00161 template <> struct codec_traits<std::string> {
00165     static void encode(Encoder& e, const std::string& s) {
00166         e.encodeString(s);
00167     }
00168 
00172     static void decode(Decoder& d, std::string& s) {
00173         s = d.decodeString();
00174     }
00175 };
00176 
00180 template <> struct codec_traits<std::vector<uint8_t> > {
00184     static void encode(Encoder& e, const std::vector<uint8_t>& b) {
00185         e.encodeBytes(b);
00186     }
00187 
00191     static void decode(Decoder& d, std::vector<uint8_t>& s) {
00192         d.decodeBytes(s);
00193     }
00194 };
00195 
00199 template <size_t N> struct codec_traits<boost::array<uint8_t, N> > {
00203     static void encode(Encoder& e, const boost::array<uint8_t, N>& b) {
00204         e.encodeFixed(&b[0], N);
00205     }
00206 
00210     static void decode(Decoder& d, boost::array<uint8_t, N>& s) {
00211         std::vector<uint8_t> v(N);
00212         d.decodeFixed(N, v);
00213         std::copy(&v[0], &v[0] + N, &s[0]);
00214     }
00215 };
00216 
00220 template <typename T> struct codec_traits<std::vector<T> > {
00224     static void encode(Encoder& e, const std::vector<T>& b) {
00225         e.arrayStart();
00226         if (! b.empty()) {
00227             e.setItemCount(b.size());
00228             for (typename std::vector<T>::const_iterator it = b.begin();
00229                 it != b.end(); ++it) {
00230                 e.startItem();
00231                 avro::encode(e, *it);
00232             }
00233         }
00234         e.arrayEnd();
00235     }
00236 
00240     static void decode(Decoder& d, std::vector<T>& s) {
00241         s.clear();
00242         for (size_t n = d.arrayStart(); n != 0; n = d.arrayNext()) {
00243             for (size_t i = 0; i < n; ++i) {
00244                 T t;
00245                 avro::decode(d, t);
00246                 s.push_back(t);
00247             }
00248         }
00249     }
00250 };
00251 
00255 template <typename T> struct codec_traits<std::map<std::string, T> > {
00259     static void encode(Encoder& e, const std::map<std::string, T>& b) {
00260         e.mapStart();
00261         if (! b.empty()) {
00262             e.setItemCount(b.size());
00263             for (typename std::map<std::string, T>::const_iterator
00264                 it = b.begin();
00265                 it != b.end(); ++it) {
00266                 e.startItem();
00267                 avro::encode(e, it->first);
00268                 avro::encode(e, it->second);
00269             }
00270         }
00271         e.mapEnd();
00272     }
00273 
00277     static void decode(Decoder& d, std::map<std::string, T>& s) {
00278         s.clear();
00279         for (size_t n = d.mapStart(); n != 0; n = d.mapNext()) {
00280             for (size_t i = 0; i < n; ++i) {
00281                 std::string k;
00282                 avro::decode(d, k);
00283                 T t;
00284                 avro::decode(d, t);
00285                 s[k] = t;
00286             }
00287         }
00288     }
00289 };
00290 
00294 template <typename T>
00295 void encode(Encoder& e, const T& t) {
00296     codec_traits<T>::encode(e, t);
00297 }
00298 
00302 template <typename T>
00303 void decode(Decoder& d, T& t) {
00304     codec_traits<T>::decode(d, t);
00305 }
00306 
00307 }   // namespace avro
00308 #endif // avro_Codec_hh__
00309 
00310 
00311