Avro C++
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  * https://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 "array"
23 #include <algorithm>
24 #include <map>
25 #include <string>
26 #include <vector>
27 
28 #include "boost/blank.hpp"
29 
30 #include "AvroTraits.hh"
31 #include "Config.hh"
32 #include "Decoder.hh"
33 #include "Encoder.hh"
34 
49 namespace avro {
50 
51 typedef boost::blank null;
52 
53 template<typename T>
54 void encode(Encoder &e, const T &t);
55 template<typename T>
56 void decode(Decoder &d, T &t);
57 
66 template<typename T>
67 struct codec_traits;
68 
72 template<>
73 struct codec_traits<bool> {
77  static void encode(Encoder &e, bool b) {
78  e.encodeBool(b);
79  }
80 
84  static void decode(Decoder &d, bool &b) {
85  b = d.decodeBool();
86  }
87 };
88 
92 template<>
93 struct codec_traits<int32_t> {
97  static void encode(Encoder &e, int32_t i) {
98  e.encodeInt(i);
99  }
100 
104  static void decode(Decoder &d, int32_t &i) {
105  i = d.decodeInt();
106  }
107 };
108 
112 template<>
113 struct codec_traits<int64_t> {
117  static void encode(Encoder &e, int64_t l) {
118  e.encodeLong(l);
119  }
120 
124  static void decode(Decoder &d, int64_t &l) {
125  l = d.decodeLong();
126  }
127 };
128 
132 template<>
133 struct codec_traits<float> {
137  static void encode(Encoder &e, float f) {
138  e.encodeFloat(f);
139  }
140 
144  static void decode(Decoder &d, float &f) {
145  f = d.decodeFloat();
146  }
147 };
148 
152 template<>
153 struct codec_traits<double> {
157  static void encode(Encoder &e, double d) {
158  e.encodeDouble(d);
159  }
160 
164  static void decode(Decoder &d, double &dbl) {
165  dbl = d.decodeDouble();
166  }
167 };
168 
172 template<>
173 struct codec_traits<std::string> {
177  static void encode(Encoder &e, const std::string &s) {
178  e.encodeString(s);
179  }
180 
184  static void decode(Decoder &d, std::string &s) {
185  s = d.decodeString();
186  }
187 };
188 
192 template<>
193 struct codec_traits<std::vector<uint8_t>> {
197  static void encode(Encoder &e, const std::vector<uint8_t> &b) {
198  e.encodeBytes(b);
199  }
200 
204  static void decode(Decoder &d, std::vector<uint8_t> &s) {
205  d.decodeBytes(s);
206  }
207 };
208 
212 template<size_t N>
213 struct codec_traits<std::array<uint8_t, N>> {
217  static void encode(Encoder &e, const std::array<uint8_t, N> &b) {
218  e.encodeFixed(b.data(), N);
219  }
220 
224  static void decode(Decoder &d, std::array<uint8_t, N> &s) {
225  std::vector<uint8_t> v(N);
226  d.decodeFixed(N, v);
227  std::copy(v.data(), v.data() + N, s.data());
228  }
229 };
230 
234 template<typename T>
235 struct codec_traits<std::vector<T>> {
239  static void encode(Encoder &e, const std::vector<T> &b) {
240  e.arrayStart();
241  if (!b.empty()) {
242  e.setItemCount(b.size());
243  for (typename std::vector<T>::const_iterator it = b.begin();
244  it != b.end(); ++it) {
245  e.startItem();
246  avro::encode(e, *it);
247  }
248  }
249  e.arrayEnd();
250  }
251 
255  static void decode(Decoder &d, std::vector<T> &s) {
256  s.clear();
257  for (size_t n = d.arrayStart(); n != 0; n = d.arrayNext()) {
258  for (size_t i = 0; i < n; ++i) {
259  T t;
260  avro::decode(d, t);
261  s.push_back(std::move(t));
262  }
263  }
264  }
265 };
266 
267 typedef codec_traits<std::vector<bool>::const_reference> bool_codec_traits;
268 
269 template<>
270 struct codec_traits<std::conditional<avro::is_not_defined<bool_codec_traits>::value,
271  std::vector<bool>::const_reference, void>::type> {
275  static void encode(Encoder &e, std::vector<bool>::const_reference b) {
276  e.encodeBool(b);
277  }
278 };
279 
283 template<typename T>
284 struct codec_traits<std::map<std::string, T>> {
288  static void encode(Encoder &e, const std::map<std::string, T> &b) {
289  e.mapStart();
290  if (!b.empty()) {
291  e.setItemCount(b.size());
292  for (typename std::map<std::string, T>::const_iterator
293  it = b.begin();
294  it != b.end(); ++it) {
295  e.startItem();
296  avro::encode(e, it->first);
297  avro::encode(e, it->second);
298  }
299  }
300  e.mapEnd();
301  }
302 
306  static void decode(Decoder &d, std::map<std::string, T> &s) {
307  s.clear();
308  for (size_t n = d.mapStart(); n != 0; n = d.mapNext()) {
309  for (size_t i = 0; i < n; ++i) {
310  std::string k;
311  avro::decode(d, k);
312  T &t = s[std::move(k)];
313  avro::decode(d, t);
314  }
315  }
316  }
317 };
318 
322 template<>
323 struct codec_traits<avro::null> {
327  static void encode(Encoder &e, const avro::null &) {
328  e.encodeNull();
329  }
330 
334  static void decode(Decoder &d, avro::null &) {
335  d.decodeNull();
336  }
337 };
338 
342 template<typename T>
343 void encode(Encoder &e, const T &t) {
345 }
346 
350 template<typename T>
351 void decode(Decoder &d, T &t) {
353 }
354 
355 } // namespace avro
356 
357 #endif // avro_Codec_hh__
This header contains type traits and similar utilities used by the library.
Low level support for decoding avro values.
Low level support for encoding avro values.
Decoder is an interface implemented by every decoder capable of decoding Avro data.
Definition: Decoder.hh:48
virtual size_t mapNext()=0
Returns the number of entries in next chunk. 0 if last.
virtual void decodeNull()=0
Decodes a null from the current stream.
virtual int32_t decodeInt()=0
Decodes a 32-bit int from the current stream.
virtual size_t arrayNext()=0
Returns the number of entries in next chunk. 0 if last.
std::vector< uint8_t > decodeBytes()
Decodes arbitrary binary data from the current stream.
Definition: Decoder.hh:90
virtual size_t mapStart()=0
Start decoding a map. Returns the number of entries in first chunk.
virtual size_t arrayStart()=0
Start decoding an array. Returns the number of entries in first chunk.
virtual bool decodeBool()=0
Decodes a bool from the current stream.
virtual float decodeFloat()=0
Decodes a single-precision floating point number from current stream.
virtual int64_t decodeLong()=0
Decodes a 64-bit signed int from the current stream.
std::vector< uint8_t > decodeFixed(size_t n)
Decodes fixed length binary from the current stream.
Definition: Decoder.hh:109
virtual double decodeDouble()=0
Decodes a double-precision floating point number from current stream.
std::string decodeString()
Decodes a UTF-8 string from the current stream.
Definition: Decoder.hh:75
The abstract base class for all Avro encoders.
Definition: Encoder.hh:52
virtual void encodeInt(int32_t i)=0
Encodes a 32-bit int to the current stream.
virtual void encodeNull()=0
Encodes a null to the current stream.
virtual void arrayStart()=0
Indicates that an array of items is being encoded.
virtual void setItemCount(size_t count)=0
Indicates that count number of items are to follow in the current array or map.
virtual void arrayEnd()=0
Indicates that the current array of items have ended.
virtual void encodeString(const std::string &s)=0
Encodes a UTF-8 string to the current stream.
virtual void encodeFixed(const uint8_t *bytes, size_t len)=0
Encodes fixed length binary to the current stream.
virtual void startItem()=0
Marks a beginning of an item in the current array or map.
virtual void encodeBool(bool b)=0
Encodes a bool to the current stream.
virtual void mapStart()=0
Indicates that a map of items is being encoded.
virtual void mapEnd()=0
Indicates that the current map of items have ended.
virtual void encodeDouble(double d)=0
Encodes a double-precision floating point number to the current stream.
virtual void encodeBytes(const uint8_t *bytes, size_t len)=0
Encodes arbitrary binary data into the current stream as Avro "bytes" data type.
virtual void encodeLong(int64_t l)=0
Encodes a 64-bit signed int to the current stream.
virtual void encodeFloat(float f)=0
Encodes a single-precision floating point number to the current stream.
A bunch of templates and specializations for encoding and decoding specific types.
Definition: AvroParse.hh:30
void encode(Encoder &e, const T &t)
Generic encoder function that makes use of the codec_traits.
Definition: Specific.hh:343
void decode(Decoder &d, T &t)
Generic decoder function that makes use of the codec_traits.
Definition: Specific.hh:351
static void encode(Encoder &e, const avro::null &)
Encodes a given value.
Definition: Specific.hh:327
static void decode(Decoder &d, avro::null &)
Decodes into a given value.
Definition: Specific.hh:334
static void encode(Encoder &e, bool b)
Encodes a given value.
Definition: Specific.hh:77
static void decode(Decoder &d, bool &b)
Decodes into a given value.
Definition: Specific.hh:84
static void decode(Decoder &d, double &dbl)
Decodes into a given value.
Definition: Specific.hh:164
static void encode(Encoder &e, double d)
Encodes a given value.
Definition: Specific.hh:157
static void decode(Decoder &d, float &f)
Decodes into a given value.
Definition: Specific.hh:144
static void encode(Encoder &e, float f)
Encodes a given value.
Definition: Specific.hh:137
static void encode(Encoder &e, int32_t i)
Encodes a given value.
Definition: Specific.hh:97
static void decode(Decoder &d, int32_t &i)
Decodes into a given value.
Definition: Specific.hh:104
static void encode(Encoder &e, int64_t l)
Encodes a given value.
Definition: Specific.hh:117
static void decode(Decoder &d, int64_t &l)
Decodes into a given value.
Definition: Specific.hh:124
static void encode(Encoder &e, const std::array< uint8_t, N > &b)
Encodes a given value.
Definition: Specific.hh:217
static void decode(Decoder &d, std::array< uint8_t, N > &s)
Decodes into a given value.
Definition: Specific.hh:224
static void encode(Encoder &e, std::vector< bool >::const_reference b)
Encodes a given value.
Definition: Specific.hh:275
static void decode(Decoder &d, std::map< std::string, T > &s)
Decodes into a given value.
Definition: Specific.hh:306
static void encode(Encoder &e, const std::map< std::string, T > &b)
Encodes a given value.
Definition: Specific.hh:288
static void decode(Decoder &d, std::string &s)
Decodes into a given value.
Definition: Specific.hh:184
static void encode(Encoder &e, const std::string &s)
Encodes a given value.
Definition: Specific.hh:177
static void decode(Decoder &d, std::vector< T > &s)
Decodes into a given value.
Definition: Specific.hh:255
static void encode(Encoder &e, const std::vector< T > &b)
Encodes a given value.
Definition: Specific.hh:239
static void encode(Encoder &e, const std::vector< uint8_t > &b)
Encodes a given value.
Definition: Specific.hh:197
static void decode(Decoder &d, std::vector< uint8_t > &s)
Decodes into a given value.
Definition: Specific.hh:204
Codec_traits tells avro how to encode and decode an object of given type.
Definition: Generic.hh:114