Avro C++

Encoder.hh
Go to the documentation of this file.
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_Encoder_hh__
20 #define avro_Encoder_hh__
21
22 #include "Config.hh"
23 #include <cstdint>
24 #include <memory>
25 #include <string>
26 #include <vector>
27
28 #include "Stream.hh"
29 #include "ValidSchema.hh"
30
44
45 namespace avro {
46
52 class AVRO_DECL Encoder {
53 public:
54 virtual ~Encoder() = default;
58 virtual void init(OutputStream &os) = 0;
59
61 virtual void flush() = 0;
62
65 virtual int64_t byteCount() const = 0;
66
68 virtual void encodeNull() = 0;
69
71 virtual void encodeBool(bool b) = 0;
72
74 virtual void encodeInt(int32_t i) = 0;
75
77 virtual void encodeLong(int64_t l) = 0;
78
80 virtual void encodeFloat(float f) = 0;
81
83 virtual void encodeDouble(double d) = 0;
84
86 virtual void encodeString(const std::string &s) = 0;
87
94 virtual void encodeBytes(const uint8_t *bytes, size_t len) = 0;
95
101 void encodeBytes(const std::vector<uint8_t> &bytes) {
102 uint8_t b = 0;
103 encodeBytes(bytes.empty() ? &b : bytes.data(), bytes.size());
104 }
105
107 virtual void encodeFixed(const uint8_t *bytes, size_t len) = 0;
108
114 void encodeFixed(const std::vector<uint8_t> &bytes) {
115 encodeFixed(bytes.data(), bytes.size());
116 }
117
119 virtual void encodeEnum(size_t e) = 0;
120
122 virtual void arrayStart() = 0;
123
125 virtual void arrayEnd() = 0;
126
128 virtual void mapStart() = 0;
129
131 virtual void mapEnd() = 0;
132
135 virtual void setItemCount(size_t count) = 0;
136
138 virtual void startItem() = 0;
139
141 virtual void encodeUnionIndex(size_t e) = 0;
142 };
143
147 using EncoderPtr = std::shared_ptr<Encoder>;
148
152 AVRO_DECL EncoderPtr binaryEncoder();
153
158 AVRO_DECL EncoderPtr validatingEncoder(const ValidSchema &schema,
159 const EncoderPtr &base);
160
164 AVRO_DECL EncoderPtr jsonEncoder(const ValidSchema &schema);
165
169 AVRO_DECL EncoderPtr jsonPrettyEncoder(const ValidSchema &schema);
170
171 } // namespace avro
172
173 #endif
avro::OutputStream
A no-copy output stream.
Definition: Stream.hh:108
avro::binaryEncoder
AVRO_DECL EncoderPtr binaryEncoder()
Returns an encoder that can encode binary Avro standard.
avro::Encoder
The abstract base class for all Avro encoders.
Definition: Encoder.hh:52
avro::jsonPrettyEncoder
AVRO_DECL EncoderPtr jsonPrettyEncoder(const ValidSchema &schema)
Returns an encoder that encodes Avro standard for pretty printed JSON.
avro::Encoder::encodeFixed
void encodeFixed(const std::vector< uint8_t > &bytes)
Encodes an Avro data type Fixed.
Definition: Encoder.hh:114
avro::validatingEncoder
AVRO_DECL EncoderPtr validatingEncoder(const ValidSchema &schema, const EncoderPtr &base)
Returns an encoder that validates sequence of calls to an underlying Encoder against the given schema...
avro
A bunch of templates and specializations for encoding and decoding specific types.
Definition: AvroParse.hh:30
avro::EncoderPtr
std::shared_ptr< Encoder > EncoderPtr
Shared pointer to Encoder.
Definition: Encoder.hh:147
avro::ValidSchema
A ValidSchema is basically a non-mutable Schema that has passed some minimum of sanity checks.
Definition: ValidSchema.hh:40
avro::jsonEncoder
AVRO_DECL EncoderPtr jsonEncoder(const ValidSchema &schema)
Returns an encoder that encodes Avro standard for JSON.
avro::Encoder::encodeBytes
void encodeBytes(const std::vector< uint8_t > &bytes)
Encodes arbitrary binary data into the current stream as Avro "bytes" data type.
Definition: Encoder.hh:101