Avro C++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator
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  * 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_Encoder_hh__
20 #define avro_Encoder_hh__
21 
22 #include "Config.hh"
23 #include <stdint.h>
24 #include <string>
25 #include <vector>
26 
27 #include "ValidSchema.hh"
28 #include "Stream.hh"
29 
30 #include <boost/shared_ptr.hpp>
31 
45 
46 namespace avro {
47 
53 class AVRO_DECL Encoder {
54 public:
55  virtual ~Encoder() { };
59  virtual void init(OutputStream& os) = 0;
60 
62  virtual void flush() = 0;
63 
65  virtual void encodeNull() = 0;
66 
68  virtual void encodeBool(bool b) = 0;
69 
71  virtual void encodeInt(int32_t i) = 0;
72 
74  virtual void encodeLong(int64_t l) = 0;
75 
77  virtual void encodeFloat(float f) = 0;
78 
80  virtual void encodeDouble(double d) = 0;
81 
83  virtual void encodeString(const std::string& s) = 0;
84 
91  virtual void encodeBytes(const uint8_t *bytes, size_t len) = 0;
92 
98  void encodeBytes(const std::vector<uint8_t>& bytes) {
99  uint8_t b = 0;
100  encodeBytes(bytes.empty() ? &b : &bytes[0], bytes.size());
101  }
102 
104  virtual void encodeFixed(const uint8_t *bytes, size_t len) = 0;
105 
111  void encodeFixed(const std::vector<uint8_t>& bytes) {
112  encodeFixed(&bytes[0], bytes.size());
113  }
114 
116  virtual void encodeEnum(size_t e) = 0;
117 
119  virtual void arrayStart() = 0;
120 
122  virtual void arrayEnd() = 0;
123 
125  virtual void mapStart() = 0;
126 
128  virtual void mapEnd() = 0;
129 
132  virtual void setItemCount(size_t count) = 0;
133 
135  virtual void startItem() = 0;
136 
138  virtual void encodeUnionIndex(size_t e) = 0;
139 };
140 
144 typedef boost::shared_ptr<Encoder> EncoderPtr;
145 
149 AVRO_DECL EncoderPtr binaryEncoder();
150 
155 AVRO_DECL EncoderPtr validatingEncoder(const ValidSchema& schema,
156  const EncoderPtr& base);
157 
161 AVRO_DECL EncoderPtr jsonEncoder(const ValidSchema& schema);
162 
166 AVRO_DECL EncoderPtr jsonPrettyEncoder(const ValidSchema& schema);
167 
168 } // namespace avro
169 
170 #endif
AVRO_DECL EncoderPtr jsonPrettyEncoder(const ValidSchema &schema)
Returns an encoder that encodes Avro standard for pretty printed JSON.
void encodeFixed(const std::vector< uint8_t > &bytes)
Encodes an Avro data type Fixed.
Definition: Encoder.hh:111
void encodeBytes(const std::vector< uint8_t > &bytes)
Encodes aribtray binary data into tthe current stream as Avro "bytes" data type.
Definition: Encoder.hh:98
A bunch of templates and specializations for encoding and decoding specific types.
Definition: AvroParse.hh:31
AVRO_DECL EncoderPtr jsonEncoder(const ValidSchema &schema)
Returns an encoder that encodes Avro standard for JSON.
A no-copy output stream.
Definition: Stream.hh:81
A ValidSchema is basically a non-mutable Schema that has passed some minumum of sanity checks...
Definition: ValidSchema.hh:40
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...
boost::shared_ptr< Encoder > EncoderPtr
Shared pointer to Encoder.
Definition: Encoder.hh:144
AVRO_DECL EncoderPtr binaryEncoder()
Returns an encoder that can encode binary Avro standard.
The abstract base class for all Avro encoders.
Definition: Encoder.hh:53