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