Avro C++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator
Specific.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_Codec_hh__
20 #define avro_Codec_hh__
21 
22 #include <string>
23 #include <vector>
24 #include <map>
25 #include <algorithm>
26 
27 #include "boost/array.hpp"
28 
29 #include "Config.hh"
30 #include "Encoder.hh"
31 #include "Decoder.hh"
32 
47 namespace avro {
48 
49 template <typename T> void encode(Encoder& e, const T& t);
50 template <typename T> void decode(Decoder& d, T& t);
51 
60 template <typename T>
61 struct codec_traits {
62 };
63 
67 template <> struct codec_traits<bool> {
71  static void encode(Encoder& e, bool b) {
72  e.encodeBool(b);
73  }
74 
78  static void decode(Decoder& d, bool& b) {
79  b = d.decodeBool();
80  }
81 };
82 
86 template <> struct codec_traits<int32_t> {
90  static void encode(Encoder& e, int32_t i) {
91  e.encodeInt(i);
92  }
93 
97  static void decode(Decoder& d, int32_t& i) {
98  i = d.decodeInt();
99  }
100 };
101 
105 template <> struct codec_traits<int64_t> {
109  static void encode(Encoder& e, int64_t l) {
110  e.encodeLong(l);
111  }
112 
116  static void decode(Decoder& d, int64_t& l) {
117  l = d.decodeLong();
118  }
119 };
120 
124 template <> struct codec_traits<float> {
128  static void encode(Encoder& e, float f) {
129  e.encodeFloat(f);
130  }
131 
135  static void decode(Decoder& d, float& f) {
136  f = d.decodeFloat();
137  }
138 };
139 
143 template <> struct codec_traits<double> {
147  static void encode(Encoder& e, double d) {
148  e.encodeDouble(d);
149  }
150 
154  static void decode(Decoder& d, double& dbl) {
155  dbl = d.decodeDouble();
156  }
157 };
158 
162 template <> struct codec_traits<std::string> {
166  static void encode(Encoder& e, const std::string& s) {
167  e.encodeString(s);
168  }
169 
173  static void decode(Decoder& d, std::string& s) {
174  s = d.decodeString();
175  }
176 };
177 
181 template <> struct codec_traits<std::vector<uint8_t> > {
185  static void encode(Encoder& e, const std::vector<uint8_t>& b) {
186  e.encodeBytes(b);
187  }
188 
192  static void decode(Decoder& d, std::vector<uint8_t>& s) {
193  d.decodeBytes(s);
194  }
195 };
196 
200 template <size_t N> struct codec_traits<boost::array<uint8_t, N> > {
204  static void encode(Encoder& e, const boost::array<uint8_t, N>& b) {
205  e.encodeFixed(&b[0], N);
206  }
207 
211  static void decode(Decoder& d, boost::array<uint8_t, N>& s) {
212  std::vector<uint8_t> v(N);
213  d.decodeFixed(N, v);
214  std::copy(&v[0], &v[0] + N, &s[0]);
215  }
216 };
217 
221 template <typename T> struct codec_traits<std::vector<T> > {
225  static void encode(Encoder& e, const std::vector<T>& b) {
226  e.arrayStart();
227  if (! b.empty()) {
228  e.setItemCount(b.size());
229  for (typename std::vector<T>::const_iterator it = b.begin();
230  it != b.end(); ++it) {
231  e.startItem();
232  avro::encode(e, *it);
233  }
234  }
235  e.arrayEnd();
236  }
237 
241  static void decode(Decoder& d, std::vector<T>& s) {
242  s.clear();
243  for (size_t n = d.arrayStart(); n != 0; n = d.arrayNext()) {
244  for (size_t i = 0; i < n; ++i) {
245  T t;
246  avro::decode(d, t);
247  s.push_back(t);
248  }
249  }
250  }
251 };
252 
256 template <typename T> struct codec_traits<std::map<std::string, T> > {
260  static void encode(Encoder& e, const std::map<std::string, T>& b) {
261  e.mapStart();
262  if (! b.empty()) {
263  e.setItemCount(b.size());
264  for (typename std::map<std::string, T>::const_iterator
265  it = b.begin();
266  it != b.end(); ++it) {
267  e.startItem();
268  avro::encode(e, it->first);
269  avro::encode(e, it->second);
270  }
271  }
272  e.mapEnd();
273  }
274 
278  static void decode(Decoder& d, std::map<std::string, T>& s) {
279  s.clear();
280  for (size_t n = d.mapStart(); n != 0; n = d.mapNext()) {
281  for (size_t i = 0; i < n; ++i) {
282  std::string k;
283  avro::decode(d, k);
284  T t;
285  avro::decode(d, t);
286  s[k] = t;
287  }
288  }
289  }
290 };
291 
295 template <typename T>
296 void encode(Encoder& e, const T& t) {
298 }
299 
303 template <typename T>
304 void decode(Decoder& d, T& t) {
306 }
307 
308 } // namespace avro
309 #endif // avro_Codec_hh__
310 
311 
312 
Low level support for encoding avro values.
std::vector< uint8_t > decodeBytes()
Decodes arbitray binary data from the current stream.
Definition: Decoder.hh:91
static void decode(Decoder &d, std::map< std::string, T > &s)
Decodes into a given value.
Definition: Specific.hh:278
virtual void encodeLong(int64_t l)=0
Encodes a 64-bit signed int to the current stream.
virtual void encodeBytes(const uint8_t *bytes, size_t len)=0
Encodes aribtray binary data into tthe current stream as Avro "bytes" data type.
virtual size_t mapStart()=0
Start decoding a map. Returns the number of entries in first chunk.
static void encode(Encoder &e, const std::string &s)
Encodes a given value.
Definition: Specific.hh:166
Definition: Boost.hh:48
void decode(Decoder &d, T &t)
Generic decoder function that makes use of the codec_traits.
Definition: Specific.hh:304
virtual void arrayEnd()=0
Indicates that the current array of items have ended.
A bunch of templates and specializations for encoding and decoding specific types.
Definition: AvroParse.hh:31
static void decode(Decoder &d, boost::array< uint8_t, N > &s)
Decodes into a given value.
Definition: Specific.hh:211
static void encode(Encoder &e, float f)
Encodes a given value.
Definition: Specific.hh:128
Codec_traits tells avro how to encode and decode an object of given type.
Definition: Generic.hh:104
Definition: Node.hh:180
virtual bool decodeBool()=0
Decodes a bool from the current stream.
static void decode(Decoder &d, int64_t &l)
Decodes into a given value.
Definition: Specific.hh:116
virtual void mapStart()=0
Indicates that a map of items is being encoded.
virtual void encodeBool(bool b)=0
Encodes a bool to the current stream.
static void encode(Encoder &e, int64_t l)
Encodes a given value.
Definition: Specific.hh:109
virtual int32_t decodeInt()=0
Decodes a 32-bit int from the current stream.
virtual void mapEnd()=0
Indicates that the current map of items have ended.
virtual size_t arrayNext()=0
Returns the number of entries in next chunk. 0 if last.
virtual void encodeFixed(const uint8_t *bytes, size_t len)=0
Encodes fixed length binary to the current stream.
static void encode(Encoder &e, int32_t i)
Encodes a given value.
Definition: Specific.hh:90
static void encode(Encoder &e, const std::vector< T > &b)
Encodes a given value.
Definition: Specific.hh:225
static void decode(Decoder &d, std::string &s)
Decodes into a given value.
Definition: Specific.hh:173
virtual double decodeDouble()=0
Decodes a double-precision floating point number from current stream.
virtual void startItem()=0
Marks a beginning of an item in the current array or map.
static void decode(Decoder &d, int32_t &i)
Decodes into a given value.
Definition: Specific.hh:97
static void encode(Encoder &e, const boost::array< uint8_t, N > &b)
Encodes a given value.
Definition: Specific.hh:204
virtual void setItemCount(size_t count)=0
Indicates that count number of items are to follow in the current array or map.
virtual float decodeFloat()=0
Decodes a single-precision floating point number from current stream.
std::vector< uint8_t > decodeFixed(size_t n)
Decodes fixed length binary from the current stream.
Definition: Decoder.hh:110
virtual size_t arrayStart()=0
Start decoding an array. Returns the number of entries in first chunk.
virtual void encodeString(const std::string &s)=0
Encodes a UTF-8 string to the current stream.
void encode(Encoder &e, const T &t)
Generic encoder function that makes use of the codec_traits.
Definition: Specific.hh:296
static void decode(Decoder &d, std::vector< uint8_t > &s)
Decodes into a given value.
Definition: Specific.hh:192
virtual size_t mapNext()=0
Returns the number of entries in next chunk. 0 if last.
static void encode(Encoder &e, const std::vector< uint8_t > &b)
Encodes a given value.
Definition: Specific.hh:185
virtual void encodeInt(int32_t i)=0
Encodes a 32-bit int to the current stream.
virtual void arrayStart()=0
Indicates that an array of items is being encoded.
virtual void encodeFloat(float f)=0
Encodes a single-precision floating point number to the current stream.
static void decode(Decoder &d, double &dbl)
Decodes into a given value.
Definition: Specific.hh:154
static void encode(Encoder &e, const std::map< std::string, T > &b)
Encodes a given value.
Definition: Specific.hh:260
static void encode(Encoder &e, double d)
Encodes a given value.
Definition: Specific.hh:147
static void decode(Decoder &d, bool &b)
Decodes into a given value.
Definition: Specific.hh:78
virtual void encodeDouble(double d)=0
Encodes a double-precision floating point number to the current stream.
virtual int64_t decodeLong()=0
Decodes a 64-bit signed int from the current stream.
std::string decodeString()
Decodes a UTF-8 string from the current stream.
Definition: Decoder.hh:76
static void encode(Encoder &e, bool b)
Encodes a given value.
Definition: Specific.hh:71
The abstract base class for all Avro encoders.
Definition: Encoder.hh:53
static void decode(Decoder &d, std::vector< T > &s)
Decodes into a given value.
Definition: Specific.hh:241
Decoder is an interface implemented by every decoder capable of decoding Avro data.
Definition: Decoder.hh:49
Low level support for decoding avro values.
static void decode(Decoder &d, float &f)
Decodes into a given value.
Definition: Specific.hh:135