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) {
344 codec_traits<T>::encode(e, t);
345 }
346
350 template<typename T>
351 void decode(Decoder &d, T &t) {
352 codec_traits<T>::decode(d, t);
353 }
354
355 } // namespace avro
356
357 #endif // avro_Codec_hh__
avro::codec_traits< std::vector< uint8_t > >::decode
static void decode(Decoder &d, std::vector< uint8_t > &s)
Decodes into a given value.
Definition: Specific.hh:204
avro::Encoder::encodeNull
virtual void encodeNull()=0
Encodes a null to the current stream.
avro::codec_traits< std::vector< T > >::encode
static void encode(Encoder &e, const std::vector< T > &b)
Encodes a given value.
Definition: Specific.hh:239
avro::Encoder::setItemCount
virtual void setItemCount(size_t count)=0
Indicates that count number of items are to follow in the current array or map.
avro::codec_traits< int32_t >::encode
static void encode(Encoder &e, int32_t i)
Encodes a given value.
Definition: Specific.hh:97
avro::Decoder::arrayNext
virtual size_t arrayNext()=0
Returns the number of entries in next chunk. 0 if last.
avro::Encoder::encodeLong
virtual void encodeLong(int64_t l)=0
Encodes a 64-bit signed int to the current stream.
avro::codec_traits
Codec_traits tells avro how to encode and decode an object of given type.
Definition: Generic.hh:114
avro::codec_traits< std::string >::decode
static void decode(Decoder &d, std::string &s)
Decodes into a given value.
Definition: Specific.hh:184
avro::codec_traits< float >::decode
static void decode(Decoder &d, float &f)
Decodes into a given value.
Definition: Specific.hh:144
avro::Encoder
The abstract base class for all Avro encoders.
Definition: Encoder.hh:52
avro::codec_traits< std::vector< T > >::decode
static void decode(Decoder &d, std::vector< T > &s)
Decodes into a given value.
Definition: Specific.hh:255
avro::Encoder::arrayStart
virtual void arrayStart()=0
Indicates that an array of items is being encoded.
avro::Encoder::arrayEnd
virtual void arrayEnd()=0
Indicates that the current array of items have ended.
avro::codec_traits< std::vector< uint8_t > >::encode
static void encode(Encoder &e, const std::vector< uint8_t > &b)
Encodes a given value.
Definition: Specific.hh:197
avro::codec_traits< std::array< uint8_t, N > >::decode
static void decode(Decoder &d, std::array< uint8_t, N > &s)
Decodes into a given value.
Definition: Specific.hh:224
AvroTraits.hh
avro::Decoder
Decoder is an interface implemented by every decoder capable of decoding Avro data.
Definition: Decoder.hh:48
avro::Decoder::arrayStart
virtual size_t arrayStart()=0
Start decoding an array. Returns the number of entries in first chunk.
avro::Decoder::decodeInt
virtual int32_t decodeInt()=0
Decodes a 32-bit int from the current stream.
avro::Encoder::mapEnd
virtual void mapEnd()=0
Indicates that the current map of items have ended.
avro::codec_traits< avro::null >::decode
static void decode(Decoder &d, avro::null &)
Decodes into a given value.
Definition: Specific.hh:334
avro::Decoder::decodeLong
virtual int64_t decodeLong()=0
Decodes a 64-bit signed int from the current stream.
avro::codec_traits< std::string >::encode
static void encode(Encoder &e, const std::string &s)
Encodes a given value.
Definition: Specific.hh:177
avro::Decoder::decodeBool
virtual bool decodeBool()=0
Decodes a bool from the current stream.
avro::codec_traits< std::map< std::string, T > >::encode
static void encode(Encoder &e, const std::map< std::string, T > &b)
Encodes a given value.
Definition: Specific.hh:288
avro::Encoder::startItem
virtual void startItem()=0
Marks a beginning of an item in the current array or map.
avro::Encoder::mapStart
virtual void mapStart()=0
Indicates that a map of items is being encoded.
avro::Decoder::decodeFloat
virtual float decodeFloat()=0
Decodes a single-precision floating point number from current stream.
avro::codec_traits< bool >::decode
static void decode(Decoder &d, bool &b)
Decodes into a given value.
Definition: Specific.hh:84
avro::Decoder::decodeFixed
std::vector< uint8_t > decodeFixed(size_t n)
Decodes fixed length binary from the current stream.
Definition: Decoder.hh:109
avro::Encoder::encodeString
virtual void encodeString(const std::string &s)=0
Encodes a UTF-8 string to the current stream.
avro
A bunch of templates and specializations for encoding and decoding specific types.
Definition: AvroParse.hh:30
avro::Decoder::mapNext
virtual size_t mapNext()=0
Returns the number of entries in next chunk. 0 if last.
avro::codec_traits< bool >::encode
static void encode(Encoder &e, bool b)
Encodes a given value.
Definition: Specific.hh:77
avro::codec_traits< int64_t >::encode
static void encode(Encoder &e, int64_t l)
Encodes a given value.
Definition: Specific.hh:117
avro::encode
void encode(Encoder &e, const T &t)
Generic encoder function that makes use of the codec_traits.
Definition: Specific.hh:343
avro::codec_traits< avro::null >::encode
static void encode(Encoder &e, const avro::null &)
Encodes a given value.
Definition: Specific.hh:327
avro::decode
void decode(Decoder &d, T &t)
Generic decoder function that makes use of the codec_traits.
Definition: Specific.hh:351
avro::Encoder::encodeInt
virtual void encodeInt(int32_t i)=0
Encodes a 32-bit int to the current stream.
avro::Decoder::decodeNull
virtual void decodeNull()=0
Decodes a null from the current stream.
avro::Encoder::encodeFixed
virtual void encodeFixed(const uint8_t *bytes, size_t len)=0
Encodes fixed length binary to the current stream.
Decoder.hh
avro::Encoder::encodeBytes
virtual void encodeBytes(const uint8_t *bytes, size_t len)=0
Encodes arbitrary binary data into the current stream as Avro "bytes" data type.
Encoder.hh
avro::codec_traits< double >::decode
static void decode(Decoder &d, double &dbl)
Decodes into a given value.
Definition: Specific.hh:164
avro::Decoder::decodeDouble
virtual double decodeDouble()=0
Decodes a double-precision floating point number from current stream.
avro::Decoder::decodeString
std::string decodeString()
Decodes a UTF-8 string from the current stream.
Definition: Decoder.hh:75
avro::codec_traits< double >::encode
static void encode(Encoder &e, double d)
Encodes a given value.
Definition: Specific.hh:157
avro::codec_traits< float >::encode
static void encode(Encoder &e, float f)
Encodes a given value.
Definition: Specific.hh:137
avro::Encoder::encodeFloat
virtual void encodeFloat(float f)=0
Encodes a single-precision floating point number to the current stream.
avro::Encoder::encodeBool
virtual void encodeBool(bool b)=0
Encodes a bool to the current stream.
avro::codec_traits< int32_t >::decode
static void decode(Decoder &d, int32_t &i)
Decodes into a given value.
Definition: Specific.hh:104
avro::codec_traits< std::conditional< avro::is_not_defined< bool_codec_traits >::value, std::vector< bool >::const_reference, void >::type >::encode
static void encode(Encoder &e, std::vector< bool >::const_reference b)
Encodes a given value.
Definition: Specific.hh:275
avro::Decoder::decodeBytes
std::vector< uint8_t > decodeBytes()
Decodes arbitrary binary data from the current stream.
Definition: Decoder.hh:90
avro::codec_traits< int64_t >::decode
static void decode(Decoder &d, int64_t &l)
Decodes into a given value.
Definition: Specific.hh:124
avro::codec_traits< std::array< uint8_t, N > >::encode
static void encode(Encoder &e, const std::array< uint8_t, N > &b)
Encodes a given value.
Definition: Specific.hh:217
avro::Encoder::encodeDouble
virtual void encodeDouble(double d)=0
Encodes a double-precision floating point number to the current stream.
avro::codec_traits< std::map< std::string, T > >::decode
static void decode(Decoder &d, std::map< std::string, T > &s)
Decodes into a given value.
Definition: Specific.hh:306
avro::Decoder::mapStart
virtual size_t mapStart()=0
Start decoding a map. Returns the number of entries in first chunk.