Avro C++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator
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  * http://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 <boost/noncopyable.hpp>
23 
24 #include "Config.hh"
25 #include "Writer.hh"
26 
27 namespace avro {
28 
31 
32 template<class Writer>
33 class Serializer : private boost::noncopyable
34 {
35 
36  public:
37 
39  explicit Serializer() :
40  writer_()
41  {}
42 
44  Serializer(const ValidSchema &schema) :
45  writer_(schema)
46  {}
47 
48  void writeNull() {
49  writer_.writeValue(Null());
50  }
51 
52  void writeBool(bool val) {
53  writer_.writeValue(val);
54  }
55 
56  void writeInt(int32_t val) {
57  writer_.writeValue(val);
58  }
59 
60  void writeLong(int64_t val) {
61  writer_.writeValue(val);
62  }
63 
64  void writeFloat(float val) {
65  writer_.writeValue(val);
66  }
67 
68  void writeDouble(double val) {
69  writer_.writeValue(val);
70  }
71 
72  void writeBytes(const void *val, size_t size) {
73  writer_.writeBytes(val);
74  }
75 
76  template <size_t N>
77  void writeFixed(const uint8_t (&val)[N]) {
78  writer_.writeFixed(val);
79  }
80 
81  template <size_t N>
82  void writeFixed(const boost::array<uint8_t, N> &val) {
83  writer_.writeFixed(val);
84  }
85 
86  void writeString(const std::string &val) {
87  writer_.writeValue(val);
88  }
89 
90  void writeRecord() {
91  writer_.writeRecord();
92  }
93 
94  void writeRecordEnd() {
95  writer_.writeRecordEnd();
96  }
97 
98  void writeArrayBlock(int64_t size) {
99  writer_.writeArrayBlock(size);
100  }
101 
102  void writeArrayEnd() {
103  writer_.writeArrayEnd();
104  }
105 
106  void writeMapBlock(int64_t size) {
107  writer_.writeMapBlock(size);
108  }
109 
110  void writeMapEnd() {
111  writer_.writeMapEnd();
112  }
113 
114  void writeUnion(int64_t choice) {
115  writer_.writeUnion(choice);
116  }
117 
118  void writeEnum(int64_t choice) {
119  writer_.writeEnum(choice);
120  }
121 
122  InputBuffer buffer() const {
123  return writer_.buffer();
124  }
125 
126  private:
127 
128  Writer writer_;
129 
130 };
131 
132 } // namespace avro
133 
134 #endif
A bunch of templates and specializations for encoding and decoding specific types.
Definition: AvroParse.hh:31
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:44
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:33
Serializer()
Constructor only works with Writer.
Definition: Serializer.hh:39