Avro C++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator
Writer.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_Writer_hh__
20 #define avro_Writer_hh__
21 
22 #include <boost/noncopyable.hpp>
23 
24 #include "Config.hh"
25 #include "buffer/Buffer.hh"
26 #include "Zigzag.hh"
27 #include "Types.hh"
28 #include "Validator.hh"
29 
30 namespace avro {
31 
33 
34 template<class ValidatorType>
35 class WriterImpl : private boost::noncopyable
36 {
37 
38  public:
39 
40  WriterImpl() {}
41 
42  explicit WriterImpl(const ValidSchema &schema) :
43  validator_(schema)
44  {}
45 
46  void writeValue(const Null &) {
47  validator_.checkTypeExpected(AVRO_NULL);
48  }
49 
50  void writeValue(bool val) {
51  validator_.checkTypeExpected(AVRO_BOOL);
52  int8_t byte = (val != 0);
53  buffer_.writeTo(byte);
54  }
55 
56  void writeValue(int32_t val) {
57  validator_.checkTypeExpected(AVRO_INT);
58  boost::array<uint8_t, 5> bytes;
59  size_t size = encodeInt32(val, bytes);
60  buffer_.writeTo(reinterpret_cast<const char *>(bytes.data()), size);
61  }
62 
63  void writeValue(int64_t val) {
64  validator_.checkTypeExpected(AVRO_LONG);
65  putLong(val);
66  }
67 
68  void writeValue(float val) {
69  validator_.checkTypeExpected(AVRO_FLOAT);
70  union {
71  float f;
72  int32_t i;
73  } v;
74 
75  v.f = val;
76  buffer_.writeTo(v.i);
77  }
78 
79  void writeValue(double val) {
80  validator_.checkTypeExpected(AVRO_DOUBLE);
81  union {
82  double d;
83  int64_t i;
84  } v;
85 
86  v.d = val;
87  buffer_.writeTo(v.i);
88  }
89 
90  void writeValue(const std::string &val) {
91  validator_.checkTypeExpected(AVRO_STRING);
92  putBytes(val.c_str(), val.size());
93  }
94 
95  void writeBytes(const void *val, size_t size) {
96  validator_.checkTypeExpected(AVRO_BYTES);
97  putBytes(val, size);
98  }
99 
100  template <size_t N>
101  void writeFixed(const uint8_t (&val)[N]) {
102  validator_.checkFixedSizeExpected(N);
103  buffer_.writeTo(reinterpret_cast<const char *>(val), N);
104  }
105 
106  template <size_t N>
107  void writeFixed(const boost::array<uint8_t, N> &val) {
108  validator_.checkFixedSizeExpected(val.size());
109  buffer_.writeTo(reinterpret_cast<const char *>(val.data()), val.size());
110  }
111 
112  void writeRecord() {
113  validator_.checkTypeExpected(AVRO_RECORD);
114  validator_.checkTypeExpected(AVRO_LONG);
115  validator_.setCount(1);
116  }
117 
118  void writeRecordEnd() {
119  validator_.checkTypeExpected(AVRO_RECORD);
120  validator_.checkTypeExpected(AVRO_LONG);
121  validator_.setCount(0);
122  }
123 
124  void writeArrayBlock(int64_t size) {
125  validator_.checkTypeExpected(AVRO_ARRAY);
126  writeCount(size);
127  }
128 
129  void writeArrayEnd() {
130  writeArrayBlock(0);
131  }
132 
133  void writeMapBlock(int64_t size) {
134  validator_.checkTypeExpected(AVRO_MAP);
135  writeCount(size);
136  }
137 
138  void writeMapEnd() {
139  writeMapBlock(0);
140  }
141 
142  void writeUnion(int64_t choice) {
143  validator_.checkTypeExpected(AVRO_UNION);
144  writeCount(choice);
145  }
146 
147  void writeEnum(int64_t choice) {
148  validator_.checkTypeExpected(AVRO_ENUM);
149  writeCount(choice);
150  }
151 
152  InputBuffer buffer() const {
153  return buffer_;
154  }
155 
156  private:
157 
158  void putLong(int64_t val) {
159  boost::array<uint8_t, 10> bytes;
160  size_t size = encodeInt64(val, bytes);
161  buffer_.writeTo(reinterpret_cast<const char *>(bytes.data()), size);
162  }
163 
164  void putBytes(const void *val, size_t size) {
165  putLong(size);
166  buffer_.writeTo(reinterpret_cast<const char *>(val), size);
167  }
168 
169  void writeCount(int64_t count) {
170  validator_.checkTypeExpected(AVRO_LONG);
171  validator_.setCount(count);
172  putLong(count);
173  }
174 
175  ValidatorType validator_;
176  OutputBuffer buffer_;
177 
178 };
179 
182 
183 } // namespace avro
184 
185 #endif
Definition: Types.hh:37
Definition: Types.hh:45
Definition: Types.hh:40
A bunch of templates and specializations for encoding and decoding specific types.
Definition: AvroParse.hh:31
Definition: Types.hh:43
Definition: Types.hh:39
Definition: Types.hh:33
Definition: Types.hh:44
Class for writing avro data to a stream.
Definition: Writer.hh:35
Definition: Types.hh:35
Functions for encoding and decoding integers with zigzag compression.
Definition: Types.hh:34
Definition: Types.hh:38
define a type to identify Null in template functions
Definition: Types.hh:102
A ValidSchema is basically a non-mutable Schema that has passed some minumum of sanity checks...
Definition: ValidSchema.hh:40
Definition: Types.hh:42
Definition: Types.hh:46
Definition: Types.hh:36