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 <string>
23 #include <vector>
24 #include <map>
25 #include <algorithm>
26 #include "array"
27 
28 #include "boost/blank.hpp"
29 
30 #include "AvroTraits.hh"
31 #include "Config.hh"
32 #include "Encoder.hh"
33 #include "Decoder.hh"
34 
49 namespace avro {
50 
51 typedef boost::blank null;
52 
53 template <typename T> void encode(Encoder& e, const T& t);
54 template <typename T> void decode(Decoder& d, T& t);
55 
64 template <typename T>
65 struct codec_traits;
66 
70 template <> struct codec_traits<bool> {
74  static void encode(Encoder& e, bool b) {
75  e.encodeBool(b);
76  }
77 
81  static void decode(Decoder& d, bool& b) {
82  b = d.decodeBool();
83  }
84 };
85 
89 template <> struct codec_traits<int32_t> {
93  static void encode(Encoder& e, int32_t i) {
94  e.encodeInt(i);
95  }
96 
100  static void decode(Decoder& d, int32_t& i) {
101  i = d.decodeInt();
102  }
103 };
104 
108 template <> struct codec_traits<int64_t> {
112  static void encode(Encoder& e, int64_t l) {
113  e.encodeLong(l);
114  }
115 
119  static void decode(Decoder& d, int64_t& l) {
120  l = d.decodeLong();
121  }
122 };
123 
127 template <> struct codec_traits<float> {
131  static void encode(Encoder& e, float f) {
132  e.encodeFloat(f);
133  }
134 
138  static void decode(Decoder& d, float& f) {
139  f = d.decodeFloat();
140  }
141 };
142 
146 template <> struct codec_traits<double> {
150  static void encode(Encoder& e, double d) {
151  e.encodeDouble(d);
152  }
153 
157  static void decode(Decoder& d, double& dbl) {
158  dbl = d.decodeDouble();
159  }
160 };
161 
165 template <> struct codec_traits<std::string> {
169  static void encode(Encoder& e, const std::string& s) {
170  e.encodeString(s);
171  }
172 
176  static void decode(Decoder& d, std::string& s) {
177  s = d.decodeString();
178  }
179 };
180 
184 template <> struct codec_traits<std::vector<uint8_t> > {
188  static void encode(Encoder& e, const std::vector<uint8_t>& b) {
189  e.encodeBytes(b);
190  }
191 
195  static void decode(Decoder& d, std::vector<uint8_t>& s) {
196  d.decodeBytes(s);
197  }
198 };
199 
203 template <size_t N> struct codec_traits<std::array<uint8_t, N> > {
207  static void encode(Encoder& e, const std::array<uint8_t, N>& b) {
208  e.encodeFixed(b.data(), N);
209  }
210 
214  static void decode(Decoder& d, std::array<uint8_t, N>& s) {
215  std::vector<uint8_t> v(N);
216  d.decodeFixed(N, v);
217  std::copy(v.data(), v.data() + N, s.data());
218  }
219 };
220 
224 template <typename T> struct codec_traits<std::vector<T> > {
228  static void encode(Encoder& e, const std::vector<T>& b) {
229  e.arrayStart();
230  if (! b.empty()) {
231  e.setItemCount(b.size());
232  for (typename std::vector<T>::const_iterator it = b.begin();
233  it != b.end(); ++it) {
234  e.startItem();
235  avro::encode(e, *it);
236  }
237  }
238  e.arrayEnd();
239  }
240 
244  static void decode(Decoder& d, std::vector<T>& s) {
245  s.clear();
246  for (size_t n = d.arrayStart(); n != 0; n = d.arrayNext()) {
247  for (size_t i = 0; i < n; ++i) {
248  T t;
249  avro::decode(d, t);
250  s.push_back(t);
251  }
252  }
253  }
254 };
255 
257 
258 template <> struct codec_traits<std::conditional<avro::is_not_defined<bool_codec_traits>::value,
259  std::vector<bool>::const_reference, void>::type> {
263  static void encode(Encoder& e, std::vector<bool>::const_reference b) {
264  e.encodeBool(b);
265  }
266 };
267 
271 template <typename T> struct codec_traits<std::map<std::string, T> > {
275  static void encode(Encoder& e, const std::map<std::string, T>& b) {
276  e.mapStart();
277  if (! b.empty()) {
278  e.setItemCount(b.size());
279  for (typename std::map<std::string, T>::const_iterator
280  it = b.begin();
281  it != b.end(); ++it) {
282  e.startItem();
283  avro::encode(e, it->first);
284  avro::encode(e, it->second);
285  }
286  }
287  e.mapEnd();
288  }
289 
293  static void decode(Decoder& d, std::map<std::string, T>& s) {
294  s.clear();
295  for (size_t n = d.mapStart(); n != 0; n = d.mapNext()) {
296  for (size_t i = 0; i < n; ++i) {
297  std::string k;
298  avro::decode(d, k);
299  T t;
300  avro::decode(d, t);
301  s[k] = t;
302  }
303  }
304  }
305 };
306 
310 template <> struct codec_traits<avro::null> {
314  static void encode(Encoder& e, const avro::null&) {
315  e.encodeNull();
316  }
317 
321  static void decode(Decoder& d, avro::null&) {
322  d.decodeNull();
323  }
324 };
325 
326 
327 
331 template <typename T>
332 void encode(Encoder& e, const T& t) {
334 }
335 
339 template <typename T>
340 void decode(Decoder& d, T& t) {
342 }
343 
344 } // namespace avro
345 
346 #endif // avro_Codec_hh__
347 
348 
349 
Low level support for encoding avro values.
std::vector< uint8_t > decodeBytes()
Decodes arbitray binary data from the current stream.
Definition: Decoder.hh:90
static void decode(Decoder &d, std::map< std::string, T > &s)
Decodes into a given value.
Definition: Specific.hh:293
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:169
virtual void encodeNull()=0
Encodes a null to the current stream.
void decode(Decoder &d, T &t)
Generic decoder function that makes use of the codec_traits.
Definition: Specific.hh:340
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:30
This header contains type traits and similar utilities used by the library.
static void encode(Encoder &e, float f)
Encodes a given value.
Definition: Specific.hh:131
Codec_traits tells avro how to encode and decode an object of given type.
Definition: Generic.hh:112
Definition: Node.hh:202
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:119
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:112
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 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:93
static void encode(Encoder &e, const std::vector< T > &b)
Encodes a given value.
Definition: Specific.hh:228
static void decode(Decoder &d, std::string &s)
Decodes into a given value.
Definition: Specific.hh:176
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 encode(Encoder &e, const avro::null &)
Encodes a given value.
Definition: Specific.hh:314
static void decode(Decoder &d, int32_t &i)
Decodes into a given value.
Definition: Specific.hh:100
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:109
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:332
static void decode(Decoder &d, std::vector< uint8_t > &s)
Decodes into a given value.
Definition: Specific.hh:195
virtual size_t mapNext()=0
Returns the number of entries in next chunk. 0 if last.
static void decode(Decoder &d, avro::null &)
Decodes into a given value.
Definition: Specific.hh:321
static void encode(Encoder &e, const std::vector< uint8_t > &b)
Encodes a given value.
Definition: Specific.hh:188
static void encode(Encoder &e, const std::array< uint8_t, N > &b)
Encodes a given value.
Definition: Specific.hh:207
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:157
static void decode(Decoder &d, std::array< uint8_t, N > &s)
Decodes into a given value.
Definition: Specific.hh:214
static void encode(Encoder &e, const std::map< std::string, T > &b)
Encodes a given value.
Definition: Specific.hh:275
static void encode(Encoder &e, double d)
Encodes a given value.
Definition: Specific.hh:150
static void decode(Decoder &d, bool &b)
Decodes into a given value.
Definition: Specific.hh:81
virtual void encodeDouble(double d)=0
Encodes a double-precision floating point number to the current stream.
static void encode(Encoder &e, std::vector< bool >::const_reference b)
Encodes a given value.
Definition: Specific.hh:263
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:75
static void encode(Encoder &e, bool b)
Encodes a given value.
Definition: Specific.hh:74
The abstract base class for all Avro encoders.
Definition: Encoder.hh:52
static void decode(Decoder &d, std::vector< T > &s)
Decodes into a given value.
Definition: Specific.hh:244
Decoder is an interface implemented by every decoder capable of decoding Avro data.
Definition: Decoder.hh:48
Low level support for decoding avro values.
static void decode(Decoder &d, float &f)
Decodes into a given value.
Definition: Specific.hh:138