Avro C++

Serializer.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_Serializer_hh__
20 #define avro_Serializer_hh__
21
22 #include <array>
23 #include <boost/noncopyable.hpp>
24
25 #include "Config.hh"
26 #include "Writer.hh"
27
28 namespace avro {
29
32
33 template<class Writer>
34 class Serializer : private boost::noncopyable {
35
36 public:
38 explicit Serializer() : writer_() {}
39
41 explicit Serializer(const ValidSchema &schema) : writer_(schema) {}
42
43 void writeNull() {
44 writer_.writeValue(Null());
45 }
46
47 void writeBool(bool val) {
48 writer_.writeValue(val);
49 }
50
51 void writeInt(int32_t val) {
52 writer_.writeValue(val);
53 }
54
55 void writeLong(int64_t val) {
56 writer_.writeValue(val);
57 }
58
59 void writeFloat(float val) {
60 writer_.writeValue(val);
61 }
62
63 void writeDouble(double val) {
64 writer_.writeValue(val);
65 }
66
67 void writeBytes(const void *val, size_t size) {
68 writer_.writeBytes(val, size);
69 }
70
71 template<size_t N>
72 void writeFixed(const uint8_t (&val)[N]) {
73 writer_.writeFixed(val);
74 }
75
76 template<size_t N>
77 void writeFixed(const std::array<uint8_t, N> &val) {
78 writer_.writeFixed(val);
79 }
80
81 void writeString(const std::string &val) {
82 writer_.writeValue(val);
83 }
84
85 void writeRecord() {
86 writer_.writeRecord();
87 }
88
89 void writeRecordEnd() {
90 writer_.writeRecordEnd();
91 }
92
93 void writeArrayBlock(int64_t size) {
94 writer_.writeArrayBlock(size);
95 }
96
97 void writeArrayEnd() {
98 writer_.writeArrayEnd();
99 }
100
101 void writeMapBlock(int64_t size) {
102 writer_.writeMapBlock(size);
103 }
104
105 void writeMapEnd() {
106 writer_.writeMapEnd();
107 }
108
109 void writeUnion(int64_t choice) {
110 writer_.writeUnion(choice);
111 }
112
113 void writeEnum(int64_t choice) {
114 writer_.writeEnum(choice);
115 }
116
117 InputBuffer buffer() const {
118 return writer_.buffer();
119 }
120
121 private:
122 Writer writer_;
123 };
124
125 } // namespace avro
126
127 #endif
avro::Serializer
Class that wraps a Writer or ValidatingWriter with an interface that uses explicit write* names inste...
Definition: Serializer.hh:34
avro::Serializer::Serializer
Serializer()
Constructor only works with Writer.
Definition: Serializer.hh:38
avro
A bunch of templates and specializations for encoding and decoding specific types.
Definition: AvroParse.hh:30
avro::ValidSchema
A ValidSchema is basically a non-mutable Schema that has passed some minimum of sanity checks.
Definition: ValidSchema.hh:40
avro::Null
define a type to represent Avro Null in template functions
Definition: Types.hh:101
avro::Serializer::Serializer
Serializer(const ValidSchema &schema)
Constructor only works with ValidatingWriter.
Definition: Serializer.hh:41