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
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
20 #ifndef _THRIFT_PROTOCOL_TCOMPACTPROTOCOL_H_
21 #define _THRIFT_PROTOCOL_TCOMPACTPROTOCOL_H_ 1
23 #include <thrift/protocol/TVirtualProtocol.h>
26 #include <boost/shared_ptr.hpp>
33 * C++ Implementation of the Compact Protocol as described in THRIFT-110
35 template <class Transport_
>
36 class TCompactProtocolT
: public TVirtualProtocol
<TCompactProtocolT
<Transport_
> > {
38 static const int8_t PROTOCOL_ID
= (int8_t)0x82u
;
39 static const int8_t VERSION_N
= 1;
40 static const int8_t VERSION_MASK
= 0x1f; // 0001 1111
43 static const int8_t TYPE_MASK
= (int8_t)0xE0u
; // 1110 0000
44 static const int8_t TYPE_BITS
= 0x07; // 0000 0111
45 static const int32_t TYPE_SHIFT_AMOUNT
= 5;
50 * (Writing) If we encounter a boolean field begin, save the TField here
51 * so it can have the value incorporated.
60 * (Reading) If we read a field header, and it's a boolean field, save
61 * the boolean value here so that readBool can use it.
69 * Used to keep track of the last field for the current and previous structs,
70 * so we can do the delta stuff.
73 std::stack
<int16_t> lastField_
;
77 TCompactProtocolT(boost::shared_ptr
<Transport_
> trans
)
78 : TVirtualProtocol
<TCompactProtocolT
<Transport_
> >(trans
),
85 booleanField_
.name
= NULL
;
86 boolValue_
.hasBoolValue
= false;
89 TCompactProtocolT(boost::shared_ptr
<Transport_
> trans
,
91 int32_t container_limit
)
92 : TVirtualProtocol
<TCompactProtocolT
<Transport_
> >(trans
),
95 string_limit_(string_limit
),
98 container_limit_(container_limit
) {
99 booleanField_
.name
= NULL
;
100 boolValue_
.hasBoolValue
= false;
103 ~TCompactProtocolT() { free(string_buf_
); }
109 virtual uint32_t writeMessageBegin(const std::string
& name
,
110 const TMessageType messageType
,
111 const int32_t seqid
);
113 uint32_t writeStructBegin(const char* name
);
115 uint32_t writeStructEnd();
117 uint32_t writeFieldBegin(const char* name
, const TType fieldType
, const int16_t fieldId
);
119 uint32_t writeFieldStop();
121 uint32_t writeListBegin(const TType elemType
, const uint32_t size
);
123 uint32_t writeSetBegin(const TType elemType
, const uint32_t size
);
125 virtual uint32_t writeMapBegin(const TType keyType
, const TType valType
, const uint32_t size
);
127 uint32_t writeBool(const bool value
);
129 uint32_t writeByte(const int8_t byte
);
131 uint32_t writeI16(const int16_t i16
);
133 uint32_t writeI32(const int32_t i32
);
135 uint32_t writeI64(const int64_t i64
);
137 uint32_t writeDouble(const double dub
);
139 uint32_t writeString(const std::string
& str
);
141 uint32_t writeBinary(const std::string
& str
);
144 * These methods are called by structs, but don't actually have any wired
147 virtual uint32_t writeMessageEnd() { return 0; }
148 uint32_t writeMapEnd() { return 0; }
149 uint32_t writeListEnd() { return 0; }
150 uint32_t writeSetEnd() { return 0; }
151 uint32_t writeFieldEnd() { return 0; }
154 int32_t writeFieldBeginInternal(const char* name
,
155 const TType fieldType
,
156 const int16_t fieldId
,
157 int8_t typeOverride
);
158 uint32_t writeCollectionBegin(const TType elemType
, int32_t size
);
159 uint32_t writeVarint32(uint32_t n
);
160 uint32_t writeVarint64(uint64_t n
);
161 uint64_t i64ToZigzag(const int64_t l
);
162 uint32_t i32ToZigzag(const int32_t n
);
163 inline int8_t getCompactType(const TType ttype
);
166 uint32_t readMessageBegin(std::string
& name
, TMessageType
& messageType
, int32_t& seqid
);
168 uint32_t readStructBegin(std::string
& name
);
170 uint32_t readStructEnd();
172 uint32_t readFieldBegin(std::string
& name
, TType
& fieldType
, int16_t& fieldId
);
174 uint32_t readMapBegin(TType
& keyType
, TType
& valType
, uint32_t& size
);
176 uint32_t readListBegin(TType
& elemType
, uint32_t& size
);
178 uint32_t readSetBegin(TType
& elemType
, uint32_t& size
);
180 uint32_t readBool(bool& value
);
181 // Provide the default readBool() implementation for std::vector<bool>
182 using TVirtualProtocol
<TCompactProtocolT
<Transport_
> >::readBool
;
184 uint32_t readByte(int8_t& byte
);
186 uint32_t readI16(int16_t& i16
);
188 uint32_t readI32(int32_t& i32
);
190 uint32_t readI64(int64_t& i64
);
192 uint32_t readDouble(double& dub
);
194 uint32_t readString(std::string
& str
);
196 uint32_t readBinary(std::string
& str
);
199 *These methods are here for the struct to call, but don't have any wire
202 uint32_t readMessageEnd() { return 0; }
203 uint32_t readFieldEnd() { return 0; }
204 uint32_t readMapEnd() { return 0; }
205 uint32_t readListEnd() { return 0; }
206 uint32_t readSetEnd() { return 0; }
209 uint32_t readVarint32(int32_t& i32
);
210 uint32_t readVarint64(int64_t& i64
);
211 int32_t zigzagToI32(uint32_t n
);
212 int64_t zigzagToI64(uint64_t n
);
213 TType
getTType(int8_t type
);
215 // Buffer for reading strings, save for the lifetime of the protocol to
216 // avoid memory churn allocating memory on every string read
217 int32_t string_limit_
;
218 uint8_t* string_buf_
;
219 int32_t string_buf_size_
;
220 int32_t container_limit_
;
223 typedef TCompactProtocolT
<TTransport
> TCompactProtocol
;
226 * Constructs compact protocol handlers
228 template <class Transport_
>
229 class TCompactProtocolFactoryT
: public TProtocolFactory
{
231 TCompactProtocolFactoryT() : string_limit_(0), container_limit_(0) {}
233 TCompactProtocolFactoryT(int32_t string_limit
, int32_t container_limit
)
234 : string_limit_(string_limit
), container_limit_(container_limit
) {}
236 virtual ~TCompactProtocolFactoryT() {}
238 void setStringSizeLimit(int32_t string_limit
) { string_limit_
= string_limit
; }
240 void setContainerSizeLimit(int32_t container_limit
) { container_limit_
= container_limit
; }
242 boost::shared_ptr
<TProtocol
> getProtocol(boost::shared_ptr
<TTransport
> trans
) {
243 boost::shared_ptr
<Transport_
> specific_trans
= boost::dynamic_pointer_cast
<Transport_
>(trans
);
245 if (specific_trans
) {
246 prot
= new TCompactProtocolT
<Transport_
>(specific_trans
, string_limit_
, container_limit_
);
248 prot
= new TCompactProtocol(trans
, string_limit_
, container_limit_
);
251 return boost::shared_ptr
<TProtocol
>(prot
);
255 int32_t string_limit_
;
256 int32_t container_limit_
;
259 typedef TCompactProtocolFactoryT
<TTransport
> TCompactProtocolFactory
;
262 } // apache::thrift::protocol
264 #include <thrift/protocol/TCompactProtocol.tcc>