THRIFT-922. cpp: Templatize binary and compact protocol
Convert TBinaryProtocol and TCompactProtocol to template classes, taking
the transport class as a template parameter. This allows them to make
non-virtual calls when using the template, improving serialization
performance.
git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@1005136 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/cpp/src/protocol/TBinaryProtocol.cpp b/lib/cpp/src/protocol/TBinaryProtocol.cpp
deleted file mode 100644
index 39c189c..0000000
--- a/lib/cpp/src/protocol/TBinaryProtocol.cpp
+++ /dev/null
@@ -1,403 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-#include "TBinaryProtocol.h"
-
-#include <limits>
-
-using std::string;
-
-namespace apache { namespace thrift { namespace protocol {
-
-uint32_t TBinaryProtocol::writeMessageBegin(const std::string& name,
- const TMessageType messageType,
- const int32_t seqid) {
- if (strict_write_) {
- int32_t version = (VERSION_1) | ((int32_t)messageType);
- uint32_t wsize = 0;
- wsize += writeI32(version);
- wsize += writeString(name);
- wsize += writeI32(seqid);
- return wsize;
- } else {
- uint32_t wsize = 0;
- wsize += writeString(name);
- wsize += writeByte((int8_t)messageType);
- wsize += writeI32(seqid);
- return wsize;
- }
-}
-
-uint32_t TBinaryProtocol::writeMessageEnd() {
- return 0;
-}
-
-uint32_t TBinaryProtocol::writeStructBegin(const char* name) {
- return 0;
-}
-
-uint32_t TBinaryProtocol::writeStructEnd() {
- return 0;
-}
-
-uint32_t TBinaryProtocol::writeFieldBegin(const char* name,
- const TType fieldType,
- const int16_t fieldId) {
- uint32_t wsize = 0;
- wsize += writeByte((int8_t)fieldType);
- wsize += writeI16(fieldId);
- return wsize;
-}
-
-uint32_t TBinaryProtocol::writeFieldEnd() {
- return 0;
-}
-
-uint32_t TBinaryProtocol::writeFieldStop() {
- return
- writeByte((int8_t)T_STOP);
-}
-
-uint32_t TBinaryProtocol::writeMapBegin(const TType keyType,
- const TType valType,
- const uint32_t size) {
- uint32_t wsize = 0;
- wsize += writeByte((int8_t)keyType);
- wsize += writeByte((int8_t)valType);
- wsize += writeI32((int32_t)size);
- return wsize;
-}
-
-uint32_t TBinaryProtocol::writeMapEnd() {
- return 0;
-}
-
-uint32_t TBinaryProtocol::writeListBegin(const TType elemType,
- const uint32_t size) {
- uint32_t wsize = 0;
- wsize += writeByte((int8_t) elemType);
- wsize += writeI32((int32_t)size);
- return wsize;
-}
-
-uint32_t TBinaryProtocol::writeListEnd() {
- return 0;
-}
-
-uint32_t TBinaryProtocol::writeSetBegin(const TType elemType,
- const uint32_t size) {
- uint32_t wsize = 0;
- wsize += writeByte((int8_t)elemType);
- wsize += writeI32((int32_t)size);
- return wsize;
-}
-
-uint32_t TBinaryProtocol::writeSetEnd() {
- return 0;
-}
-
-uint32_t TBinaryProtocol::writeBool(const bool value) {
- uint8_t tmp = value ? 1 : 0;
- trans_->write(&tmp, 1);
- return 1;
-}
-
-uint32_t TBinaryProtocol::writeByte(const int8_t byte) {
- trans_->write((uint8_t*)&byte, 1);
- return 1;
-}
-
-uint32_t TBinaryProtocol::writeI16(const int16_t i16) {
- int16_t net = (int16_t)htons(i16);
- trans_->write((uint8_t*)&net, 2);
- return 2;
-}
-
-uint32_t TBinaryProtocol::writeI32(const int32_t i32) {
- int32_t net = (int32_t)htonl(i32);
- trans_->write((uint8_t*)&net, 4);
- return 4;
-}
-
-uint32_t TBinaryProtocol::writeI64(const int64_t i64) {
- int64_t net = (int64_t)htonll(i64);
- trans_->write((uint8_t*)&net, 8);
- return 8;
-}
-
-uint32_t TBinaryProtocol::writeDouble(const double dub) {
- BOOST_STATIC_ASSERT(sizeof(double) == sizeof(uint64_t));
- BOOST_STATIC_ASSERT(std::numeric_limits<double>::is_iec559);
-
- uint64_t bits = bitwise_cast<uint64_t>(dub);
- bits = htonll(bits);
- trans_->write((uint8_t*)&bits, 8);
- return 8;
-}
-
-
-uint32_t TBinaryProtocol::writeString(const string& str) {
- uint32_t size = str.size();
- uint32_t result = writeI32((int32_t)size);
- if (size > 0) {
- trans_->write((uint8_t*)str.data(), size);
- }
- return result + size;
-}
-
-uint32_t TBinaryProtocol::writeBinary(const string& str) {
- return TBinaryProtocol::writeString(str);
-}
-
-/**
- * Reading functions
- */
-
-uint32_t TBinaryProtocol::readMessageBegin(std::string& name,
- TMessageType& messageType,
- int32_t& seqid) {
- uint32_t result = 0;
- int32_t sz;
- result += readI32(sz);
-
- if (sz < 0) {
- // Check for correct version number
- int32_t version = sz & VERSION_MASK;
- if (version != VERSION_1) {
- throw TProtocolException(TProtocolException::BAD_VERSION, "Bad version identifier");
- }
- messageType = (TMessageType)(sz & 0x000000ff);
- result += readString(name);
- result += readI32(seqid);
- } else {
- if (strict_read_) {
- throw TProtocolException(TProtocolException::BAD_VERSION, "No version identifier... old protocol client in strict mode?");
- } else {
- // Handle pre-versioned input
- int8_t type;
- result += readStringBody(name, sz);
- result += readByte(type);
- messageType = (TMessageType)type;
- result += readI32(seqid);
- }
- }
- return result;
-}
-
-uint32_t TBinaryProtocol::readMessageEnd() {
- return 0;
-}
-
-uint32_t TBinaryProtocol::readStructBegin(string& name) {
- name = "";
- return 0;
-}
-
-uint32_t TBinaryProtocol::readStructEnd() {
- return 0;
-}
-
-uint32_t TBinaryProtocol::readFieldBegin(string& name,
- TType& fieldType,
- int16_t& fieldId) {
- uint32_t result = 0;
- int8_t type;
- result += readByte(type);
- fieldType = (TType)type;
- if (fieldType == T_STOP) {
- fieldId = 0;
- return result;
- }
- result += readI16(fieldId);
- return result;
-}
-
-uint32_t TBinaryProtocol::readFieldEnd() {
- return 0;
-}
-
-uint32_t TBinaryProtocol::readMapBegin(TType& keyType,
- TType& valType,
- uint32_t& size) {
- int8_t k, v;
- uint32_t result = 0;
- int32_t sizei;
- result += readByte(k);
- keyType = (TType)k;
- result += readByte(v);
- valType = (TType)v;
- result += readI32(sizei);
- if (sizei < 0) {
- throw TProtocolException(TProtocolException::NEGATIVE_SIZE);
- } else if (container_limit_ && sizei > container_limit_) {
- throw TProtocolException(TProtocolException::SIZE_LIMIT);
- }
- size = (uint32_t)sizei;
- return result;
-}
-
-uint32_t TBinaryProtocol::readMapEnd() {
- return 0;
-}
-
-uint32_t TBinaryProtocol::readListBegin(TType& elemType,
- uint32_t& size) {
- int8_t e;
- uint32_t result = 0;
- int32_t sizei;
- result += readByte(e);
- elemType = (TType)e;
- result += readI32(sizei);
- if (sizei < 0) {
- throw TProtocolException(TProtocolException::NEGATIVE_SIZE);
- } else if (container_limit_ && sizei > container_limit_) {
- throw TProtocolException(TProtocolException::SIZE_LIMIT);
- }
- size = (uint32_t)sizei;
- return result;
-}
-
-uint32_t TBinaryProtocol::readListEnd() {
- return 0;
-}
-
-uint32_t TBinaryProtocol::readSetBegin(TType& elemType,
- uint32_t& size) {
- int8_t e;
- uint32_t result = 0;
- int32_t sizei;
- result += readByte(e);
- elemType = (TType)e;
- result += readI32(sizei);
- if (sizei < 0) {
- throw TProtocolException(TProtocolException::NEGATIVE_SIZE);
- } else if (container_limit_ && sizei > container_limit_) {
- throw TProtocolException(TProtocolException::SIZE_LIMIT);
- }
- size = (uint32_t)sizei;
- return result;
-}
-
-uint32_t TBinaryProtocol::readSetEnd() {
- return 0;
-}
-
-uint32_t TBinaryProtocol::readBool(bool& value) {
- uint8_t b[1];
- trans_->readAll(b, 1);
- value = *(int8_t*)b != 0;
- return 1;
-}
-
-uint32_t TBinaryProtocol::readByte(int8_t& byte) {
- uint8_t b[1];
- trans_->readAll(b, 1);
- byte = *(int8_t*)b;
- return 1;
-}
-
-uint32_t TBinaryProtocol::readI16(int16_t& i16) {
- uint8_t b[2];
- trans_->readAll(b, 2);
- i16 = *(int16_t*)b;
- i16 = (int16_t)ntohs(i16);
- return 2;
-}
-
-uint32_t TBinaryProtocol::readI32(int32_t& i32) {
- uint8_t b[4];
- trans_->readAll(b, 4);
- i32 = *(int32_t*)b;
- i32 = (int32_t)ntohl(i32);
- return 4;
-}
-
-uint32_t TBinaryProtocol::readI64(int64_t& i64) {
- uint8_t b[8];
- trans_->readAll(b, 8);
- i64 = *(int64_t*)b;
- i64 = (int64_t)ntohll(i64);
- return 8;
-}
-
-uint32_t TBinaryProtocol::readDouble(double& dub) {
- BOOST_STATIC_ASSERT(sizeof(double) == sizeof(uint64_t));
- BOOST_STATIC_ASSERT(std::numeric_limits<double>::is_iec559);
-
- uint64_t bits;
- uint8_t b[8];
- trans_->readAll(b, 8);
- bits = *(uint64_t*)b;
- bits = ntohll(bits);
- dub = bitwise_cast<double>(bits);
- return 8;
-}
-
-uint32_t TBinaryProtocol::readString(string& str) {
- uint32_t result;
- int32_t size;
- result = readI32(size);
- return result + readStringBody(str, size);
-}
-
-uint32_t TBinaryProtocol::readBinary(string& str) {
- return TBinaryProtocol::readString(str);
-}
-
-uint32_t TBinaryProtocol::readStringBody(string& str, int32_t size) {
- uint32_t result = 0;
-
- // Catch error cases
- if (size < 0) {
- throw TProtocolException(TProtocolException::NEGATIVE_SIZE);
- }
- if (string_limit_ > 0 && size > string_limit_) {
- throw TProtocolException(TProtocolException::SIZE_LIMIT);
- }
-
- // Catch empty string case
- if (size == 0) {
- str = "";
- return result;
- }
-
- // Try to borrow first
- const uint8_t* borrow_buf;
- uint32_t got = size;
- if ((borrow_buf = trans_->borrow(NULL, &got))) {
- str.assign((const char*)borrow_buf, size);
- trans_->consume(size);
- return size;
- }
-
- // Use the heap here to prevent stack overflow for v. large strings
- if (size > string_buf_size_ || string_buf_ == NULL) {
- void* new_string_buf = std::realloc(string_buf_, (uint32_t)size);
- if (new_string_buf == NULL) {
- throw TProtocolException(TProtocolException::UNKNOWN, "Out of memory in TBinaryProtocol::readString");
- }
- string_buf_ = (uint8_t*)new_string_buf;
- string_buf_size_ = size;
- }
- trans_->readAll(string_buf_, size);
- str = string((char*)string_buf_, size);
- return (uint32_t)size;
-}
-
-}}} // apache::thrift::protocol
diff --git a/lib/cpp/src/protocol/TBinaryProtocol.h b/lib/cpp/src/protocol/TBinaryProtocol.h
index 45c5842..ca45294 100644
--- a/lib/cpp/src/protocol/TBinaryProtocol.h
+++ b/lib/cpp/src/protocol/TBinaryProtocol.h
@@ -32,15 +32,18 @@
* binary format, essentially just spitting out the raw bytes.
*
*/
-class TBinaryProtocol : public TVirtualProtocol<TBinaryProtocol> {
+template <class Transport_>
+class TBinaryProtocolT
+ : public TVirtualProtocol< TBinaryProtocolT<Transport_> > {
protected:
static const int32_t VERSION_MASK = 0xffff0000;
static const int32_t VERSION_1 = 0x80010000;
// VERSION_2 (0x80020000) is taken by TDenseProtocol.
public:
- TBinaryProtocol(boost::shared_ptr<TTransport> trans) :
- TVirtualProtocol<TBinaryProtocol>(trans),
+ TBinaryProtocolT(boost::shared_ptr<Transport_> trans) :
+ TVirtualProtocol< TBinaryProtocolT<Transport_> >(trans),
+ trans_(trans.get()),
string_limit_(0),
container_limit_(0),
strict_read_(false),
@@ -48,12 +51,13 @@
string_buf_(NULL),
string_buf_size_(0) {}
- TBinaryProtocol(boost::shared_ptr<TTransport> trans,
- int32_t string_limit,
- int32_t container_limit,
- bool strict_read,
- bool strict_write) :
- TVirtualProtocol<TBinaryProtocol>(trans),
+ TBinaryProtocolT(boost::shared_ptr<Transport_> trans,
+ int32_t string_limit,
+ int32_t container_limit,
+ bool strict_read,
+ bool strict_write) :
+ TVirtualProtocol< TBinaryProtocolT<Transport_> >(trans),
+ trans_(trans.get()),
string_limit_(string_limit),
container_limit_(container_limit),
strict_read_(strict_read),
@@ -61,7 +65,7 @@
string_buf_(NULL),
string_buf_size_(0) {}
- ~TBinaryProtocol() {
+ ~TBinaryProtocolT() {
if (string_buf_ != NULL) {
std::free(string_buf_);
string_buf_size_ = 0;
@@ -85,113 +89,111 @@
* Writing functions.
*/
- uint32_t writeMessageBegin(const std::string& name,
- const TMessageType messageType,
- const int32_t seqid);
+ /*ol*/ uint32_t writeMessageBegin(const std::string& name,
+ const TMessageType messageType,
+ const int32_t seqid);
- uint32_t writeMessageEnd();
+ /*ol*/ uint32_t writeMessageEnd();
- uint32_t writeStructBegin(const char* name);
+ inline uint32_t writeStructBegin(const char* name);
- uint32_t writeStructEnd();
+ inline uint32_t writeStructEnd();
- uint32_t writeFieldBegin(const char* name,
- const TType fieldType,
- const int16_t fieldId);
+ inline uint32_t writeFieldBegin(const char* name,
+ const TType fieldType,
+ const int16_t fieldId);
- uint32_t writeFieldEnd();
+ inline uint32_t writeFieldEnd();
- uint32_t writeFieldStop();
+ inline uint32_t writeFieldStop();
- uint32_t writeMapBegin(const TType keyType,
- const TType valType,
- const uint32_t size);
+ inline uint32_t writeMapBegin(const TType keyType,
+ const TType valType,
+ const uint32_t size);
- uint32_t writeMapEnd();
+ inline uint32_t writeMapEnd();
- uint32_t writeListBegin(const TType elemType,
- const uint32_t size);
+ inline uint32_t writeListBegin(const TType elemType, const uint32_t size);
- uint32_t writeListEnd();
+ inline uint32_t writeListEnd();
- uint32_t writeSetBegin(const TType elemType,
- const uint32_t size);
+ inline uint32_t writeSetBegin(const TType elemType, const uint32_t size);
- uint32_t writeSetEnd();
+ inline uint32_t writeSetEnd();
- uint32_t writeBool(const bool value);
+ inline uint32_t writeBool(const bool value);
- uint32_t writeByte(const int8_t byte);
+ inline uint32_t writeByte(const int8_t byte);
- uint32_t writeI16(const int16_t i16);
+ inline uint32_t writeI16(const int16_t i16);
- uint32_t writeI32(const int32_t i32);
+ inline uint32_t writeI32(const int32_t i32);
- uint32_t writeI64(const int64_t i64);
+ inline uint32_t writeI64(const int64_t i64);
- uint32_t writeDouble(const double dub);
+ inline uint32_t writeDouble(const double dub);
- uint32_t writeString(const std::string& str);
+ inline uint32_t writeString(const std::string& str);
- uint32_t writeBinary(const std::string& str);
+ inline uint32_t writeBinary(const std::string& str);
/**
* Reading functions
*/
- uint32_t readMessageBegin(std::string& name,
- TMessageType& messageType,
- int32_t& seqid);
+ /*ol*/ uint32_t readMessageBegin(std::string& name,
+ TMessageType& messageType,
+ int32_t& seqid);
- uint32_t readMessageEnd();
+ /*ol*/ uint32_t readMessageEnd();
- uint32_t readStructBegin(std::string& name);
+ inline uint32_t readStructBegin(std::string& name);
- uint32_t readStructEnd();
+ inline uint32_t readStructEnd();
- uint32_t readFieldBegin(std::string& name,
- TType& fieldType,
- int16_t& fieldId);
+ inline uint32_t readFieldBegin(std::string& name,
+ TType& fieldType,
+ int16_t& fieldId);
- uint32_t readFieldEnd();
+ inline uint32_t readFieldEnd();
- uint32_t readMapBegin(TType& keyType,
- TType& valType,
- uint32_t& size);
+ inline uint32_t readMapBegin(TType& keyType,
+ TType& valType,
+ uint32_t& size);
- uint32_t readMapEnd();
+ inline uint32_t readMapEnd();
- uint32_t readListBegin(TType& elemType,
- uint32_t& size);
+ inline uint32_t readListBegin(TType& elemType, uint32_t& size);
- uint32_t readListEnd();
+ inline uint32_t readListEnd();
- uint32_t readSetBegin(TType& elemType,
- uint32_t& size);
+ inline uint32_t readSetBegin(TType& elemType, uint32_t& size);
- uint32_t readSetEnd();
+ inline uint32_t readSetEnd();
- uint32_t readBool(bool& value);
+ inline uint32_t readBool(bool& value);
- uint32_t readByte(int8_t& byte);
+ inline uint32_t readByte(int8_t& byte);
- uint32_t readI16(int16_t& i16);
+ inline uint32_t readI16(int16_t& i16);
- uint32_t readI32(int32_t& i32);
+ inline uint32_t readI32(int32_t& i32);
- uint32_t readI64(int64_t& i64);
+ inline uint32_t readI64(int64_t& i64);
- uint32_t readDouble(double& dub);
+ inline uint32_t readDouble(double& dub);
- uint32_t readString(std::string& str);
+ inline uint32_t readString(std::string& str);
- uint32_t readBinary(std::string& str);
+ inline uint32_t readBinary(std::string& str);
protected:
uint32_t readStringBody(std::string& str, int32_t sz);
+ Transport_* trans_;
+
int32_t string_limit_;
int32_t container_limit_;
@@ -206,24 +208,28 @@
};
+typedef TBinaryProtocolT<TTransport> TBinaryProtocol;
+
/**
* Constructs binary protocol handlers
*/
-class TBinaryProtocolFactory : public TProtocolFactory {
+template <class Transport_>
+class TBinaryProtocolFactoryT : public TProtocolFactory {
public:
- TBinaryProtocolFactory() :
+ TBinaryProtocolFactoryT() :
string_limit_(0),
container_limit_(0),
strict_read_(false),
strict_write_(true) {}
- TBinaryProtocolFactory(int32_t string_limit, int32_t container_limit, bool strict_read, bool strict_write) :
+ TBinaryProtocolFactoryT(int32_t string_limit, int32_t container_limit,
+ bool strict_read, bool strict_write) :
string_limit_(string_limit),
container_limit_(container_limit),
strict_read_(strict_read),
strict_write_(strict_write) {}
- virtual ~TBinaryProtocolFactory() {}
+ virtual ~TBinaryProtocolFactoryT() {}
void setStringSizeLimit(int32_t string_limit) {
string_limit_ = string_limit;
@@ -239,7 +245,19 @@
}
boost::shared_ptr<TProtocol> getProtocol(boost::shared_ptr<TTransport> trans) {
- return boost::shared_ptr<TProtocol>(new TBinaryProtocol(trans, string_limit_, container_limit_, strict_read_, strict_write_));
+ boost::shared_ptr<Transport_> specific_trans =
+ boost::dynamic_pointer_cast<Transport_>(trans);
+ TProtocol* prot;
+ if (specific_trans) {
+ prot = new TBinaryProtocolT<Transport_>(specific_trans, string_limit_,
+ container_limit_, strict_read_,
+ strict_write_);
+ } else {
+ prot = new TBinaryProtocol(trans, string_limit_, container_limit_,
+ strict_read_, strict_write_);
+ }
+
+ return boost::shared_ptr<TProtocol>(prot);
}
private:
@@ -250,6 +268,10 @@
};
+typedef TBinaryProtocolFactoryT<TTransport> TBinaryProtocolFactory;
+
}}} // apache::thrift::protocol
+#include "TBinaryProtocol.tcc"
+
#endif // #ifndef _THRIFT_PROTOCOL_TBINARYPROTOCOL_H_
diff --git a/lib/cpp/src/protocol/TBinaryProtocol.tcc b/lib/cpp/src/protocol/TBinaryProtocol.tcc
new file mode 100644
index 0000000..1433a4f
--- /dev/null
+++ b/lib/cpp/src/protocol/TBinaryProtocol.tcc
@@ -0,0 +1,451 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#ifndef _THRIFT_PROTOCOL_TBINARYPROTOCOL_TCC_
+#define _THRIFT_PROTOCOL_TBINARYPROTOCOL_TCC_ 1
+
+#include "TBinaryProtocol.h"
+
+#include <limits>
+
+
+namespace apache { namespace thrift { namespace protocol {
+
+template <class Transport_>
+uint32_t TBinaryProtocolT<Transport_>::writeMessageBegin(const std::string& name,
+ const TMessageType messageType,
+ const int32_t seqid) {
+ if (this->strict_write_) {
+ int32_t version = (VERSION_1) | ((int32_t)messageType);
+ uint32_t wsize = 0;
+ wsize += writeI32(version);
+ wsize += writeString(name);
+ wsize += writeI32(seqid);
+ return wsize;
+ } else {
+ uint32_t wsize = 0;
+ wsize += writeString(name);
+ wsize += writeByte((int8_t)messageType);
+ wsize += writeI32(seqid);
+ return wsize;
+ }
+}
+
+template <class Transport_>
+uint32_t TBinaryProtocolT<Transport_>::writeMessageEnd() {
+ return 0;
+}
+
+template <class Transport_>
+uint32_t TBinaryProtocolT<Transport_>::writeStructBegin(const char* name) {
+ return 0;
+}
+
+template <class Transport_>
+uint32_t TBinaryProtocolT<Transport_>::writeStructEnd() {
+ return 0;
+}
+
+template <class Transport_>
+uint32_t TBinaryProtocolT<Transport_>::writeFieldBegin(const char* name,
+ const TType fieldType,
+ const int16_t fieldId) {
+ uint32_t wsize = 0;
+ wsize += writeByte((int8_t)fieldType);
+ wsize += writeI16(fieldId);
+ return wsize;
+}
+
+template <class Transport_>
+uint32_t TBinaryProtocolT<Transport_>::writeFieldEnd() {
+ return 0;
+}
+
+template <class Transport_>
+uint32_t TBinaryProtocolT<Transport_>::writeFieldStop() {
+ return
+ writeByte((int8_t)T_STOP);
+}
+
+template <class Transport_>
+uint32_t TBinaryProtocolT<Transport_>::writeMapBegin(const TType keyType,
+ const TType valType,
+ const uint32_t size) {
+ uint32_t wsize = 0;
+ wsize += writeByte((int8_t)keyType);
+ wsize += writeByte((int8_t)valType);
+ wsize += writeI32((int32_t)size);
+ return wsize;
+}
+
+template <class Transport_>
+uint32_t TBinaryProtocolT<Transport_>::writeMapEnd() {
+ return 0;
+}
+
+template <class Transport_>
+uint32_t TBinaryProtocolT<Transport_>::writeListBegin(const TType elemType,
+ const uint32_t size) {
+ uint32_t wsize = 0;
+ wsize += writeByte((int8_t) elemType);
+ wsize += writeI32((int32_t)size);
+ return wsize;
+}
+
+template <class Transport_>
+uint32_t TBinaryProtocolT<Transport_>::writeListEnd() {
+ return 0;
+}
+
+template <class Transport_>
+uint32_t TBinaryProtocolT<Transport_>::writeSetBegin(const TType elemType,
+ const uint32_t size) {
+ uint32_t wsize = 0;
+ wsize += writeByte((int8_t)elemType);
+ wsize += writeI32((int32_t)size);
+ return wsize;
+}
+
+template <class Transport_>
+uint32_t TBinaryProtocolT<Transport_>::writeSetEnd() {
+ return 0;
+}
+
+template <class Transport_>
+uint32_t TBinaryProtocolT<Transport_>::writeBool(const bool value) {
+ uint8_t tmp = value ? 1 : 0;
+ this->trans_->write(&tmp, 1);
+ return 1;
+}
+
+template <class Transport_>
+uint32_t TBinaryProtocolT<Transport_>::writeByte(const int8_t byte) {
+ this->trans_->write((uint8_t*)&byte, 1);
+ return 1;
+}
+
+template <class Transport_>
+uint32_t TBinaryProtocolT<Transport_>::writeI16(const int16_t i16) {
+ int16_t net = (int16_t)htons(i16);
+ this->trans_->write((uint8_t*)&net, 2);
+ return 2;
+}
+
+template <class Transport_>
+uint32_t TBinaryProtocolT<Transport_>::writeI32(const int32_t i32) {
+ int32_t net = (int32_t)htonl(i32);
+ this->trans_->write((uint8_t*)&net, 4);
+ return 4;
+}
+
+template <class Transport_>
+uint32_t TBinaryProtocolT<Transport_>::writeI64(const int64_t i64) {
+ int64_t net = (int64_t)htonll(i64);
+ this->trans_->write((uint8_t*)&net, 8);
+ return 8;
+}
+
+template <class Transport_>
+uint32_t TBinaryProtocolT<Transport_>::writeDouble(const double dub) {
+ BOOST_STATIC_ASSERT(sizeof(double) == sizeof(uint64_t));
+ BOOST_STATIC_ASSERT(std::numeric_limits<double>::is_iec559);
+
+ uint64_t bits = bitwise_cast<uint64_t>(dub);
+ bits = htonll(bits);
+ this->trans_->write((uint8_t*)&bits, 8);
+ return 8;
+}
+
+
+template <class Transport_>
+uint32_t TBinaryProtocolT<Transport_>::writeString(const std::string& str) {
+ uint32_t size = str.size();
+ uint32_t result = writeI32((int32_t)size);
+ if (size > 0) {
+ this->trans_->write((uint8_t*)str.data(), size);
+ }
+ return result + size;
+}
+
+template <class Transport_>
+uint32_t TBinaryProtocolT<Transport_>::writeBinary(const std::string& str) {
+ return TBinaryProtocolT<Transport_>::writeString(str);
+}
+
+/**
+ * Reading functions
+ */
+
+template <class Transport_>
+uint32_t TBinaryProtocolT<Transport_>::readMessageBegin(std::string& name,
+ TMessageType& messageType,
+ int32_t& seqid) {
+ uint32_t result = 0;
+ int32_t sz;
+ result += readI32(sz);
+
+ if (sz < 0) {
+ // Check for correct version number
+ int32_t version = sz & VERSION_MASK;
+ if (version != VERSION_1) {
+ throw TProtocolException(TProtocolException::BAD_VERSION, "Bad version identifier");
+ }
+ messageType = (TMessageType)(sz & 0x000000ff);
+ result += readString(name);
+ result += readI32(seqid);
+ } else {
+ if (this->strict_read_) {
+ throw TProtocolException(TProtocolException::BAD_VERSION, "No version identifier... old protocol client in strict mode?");
+ } else {
+ // Handle pre-versioned input
+ int8_t type;
+ result += readStringBody(name, sz);
+ result += readByte(type);
+ messageType = (TMessageType)type;
+ result += readI32(seqid);
+ }
+ }
+ return result;
+}
+
+template <class Transport_>
+uint32_t TBinaryProtocolT<Transport_>::readMessageEnd() {
+ return 0;
+}
+
+template <class Transport_>
+uint32_t TBinaryProtocolT<Transport_>::readStructBegin(std::string& name) {
+ name = "";
+ return 0;
+}
+
+template <class Transport_>
+uint32_t TBinaryProtocolT<Transport_>::readStructEnd() {
+ return 0;
+}
+
+template <class Transport_>
+uint32_t TBinaryProtocolT<Transport_>::readFieldBegin(std::string& name,
+ TType& fieldType,
+ int16_t& fieldId) {
+ uint32_t result = 0;
+ int8_t type;
+ result += readByte(type);
+ fieldType = (TType)type;
+ if (fieldType == T_STOP) {
+ fieldId = 0;
+ return result;
+ }
+ result += readI16(fieldId);
+ return result;
+}
+
+template <class Transport_>
+uint32_t TBinaryProtocolT<Transport_>::readFieldEnd() {
+ return 0;
+}
+
+template <class Transport_>
+uint32_t TBinaryProtocolT<Transport_>::readMapBegin(TType& keyType,
+ TType& valType,
+ uint32_t& size) {
+ int8_t k, v;
+ uint32_t result = 0;
+ int32_t sizei;
+ result += readByte(k);
+ keyType = (TType)k;
+ result += readByte(v);
+ valType = (TType)v;
+ result += readI32(sizei);
+ if (sizei < 0) {
+ throw TProtocolException(TProtocolException::NEGATIVE_SIZE);
+ } else if (this->container_limit_ && sizei > this->container_limit_) {
+ throw TProtocolException(TProtocolException::SIZE_LIMIT);
+ }
+ size = (uint32_t)sizei;
+ return result;
+}
+
+template <class Transport_>
+uint32_t TBinaryProtocolT<Transport_>::readMapEnd() {
+ return 0;
+}
+
+template <class Transport_>
+uint32_t TBinaryProtocolT<Transport_>::readListBegin(TType& elemType,
+ uint32_t& size) {
+ int8_t e;
+ uint32_t result = 0;
+ int32_t sizei;
+ result += readByte(e);
+ elemType = (TType)e;
+ result += readI32(sizei);
+ if (sizei < 0) {
+ throw TProtocolException(TProtocolException::NEGATIVE_SIZE);
+ } else if (this->container_limit_ && sizei > this->container_limit_) {
+ throw TProtocolException(TProtocolException::SIZE_LIMIT);
+ }
+ size = (uint32_t)sizei;
+ return result;
+}
+
+template <class Transport_>
+uint32_t TBinaryProtocolT<Transport_>::readListEnd() {
+ return 0;
+}
+
+template <class Transport_>
+uint32_t TBinaryProtocolT<Transport_>::readSetBegin(TType& elemType,
+ uint32_t& size) {
+ int8_t e;
+ uint32_t result = 0;
+ int32_t sizei;
+ result += readByte(e);
+ elemType = (TType)e;
+ result += readI32(sizei);
+ if (sizei < 0) {
+ throw TProtocolException(TProtocolException::NEGATIVE_SIZE);
+ } else if (this->container_limit_ && sizei > this->container_limit_) {
+ throw TProtocolException(TProtocolException::SIZE_LIMIT);
+ }
+ size = (uint32_t)sizei;
+ return result;
+}
+
+template <class Transport_>
+uint32_t TBinaryProtocolT<Transport_>::readSetEnd() {
+ return 0;
+}
+
+template <class Transport_>
+uint32_t TBinaryProtocolT<Transport_>::readBool(bool& value) {
+ uint8_t b[1];
+ this->trans_->readAll(b, 1);
+ value = *(int8_t*)b != 0;
+ return 1;
+}
+
+template <class Transport_>
+uint32_t TBinaryProtocolT<Transport_>::readByte(int8_t& byte) {
+ uint8_t b[1];
+ this->trans_->readAll(b, 1);
+ byte = *(int8_t*)b;
+ return 1;
+}
+
+template <class Transport_>
+uint32_t TBinaryProtocolT<Transport_>::readI16(int16_t& i16) {
+ uint8_t b[2];
+ this->trans_->readAll(b, 2);
+ i16 = *(int16_t*)b;
+ i16 = (int16_t)ntohs(i16);
+ return 2;
+}
+
+template <class Transport_>
+uint32_t TBinaryProtocolT<Transport_>::readI32(int32_t& i32) {
+ uint8_t b[4];
+ this->trans_->readAll(b, 4);
+ i32 = *(int32_t*)b;
+ i32 = (int32_t)ntohl(i32);
+ return 4;
+}
+
+template <class Transport_>
+uint32_t TBinaryProtocolT<Transport_>::readI64(int64_t& i64) {
+ uint8_t b[8];
+ this->trans_->readAll(b, 8);
+ i64 = *(int64_t*)b;
+ i64 = (int64_t)ntohll(i64);
+ return 8;
+}
+
+template <class Transport_>
+uint32_t TBinaryProtocolT<Transport_>::readDouble(double& dub) {
+ BOOST_STATIC_ASSERT(sizeof(double) == sizeof(uint64_t));
+ BOOST_STATIC_ASSERT(std::numeric_limits<double>::is_iec559);
+
+ uint64_t bits;
+ uint8_t b[8];
+ this->trans_->readAll(b, 8);
+ bits = *(uint64_t*)b;
+ bits = ntohll(bits);
+ dub = bitwise_cast<double>(bits);
+ return 8;
+}
+
+template <class Transport_>
+uint32_t TBinaryProtocolT<Transport_>::readString(std::string& str) {
+ uint32_t result;
+ int32_t size;
+ result = readI32(size);
+ return result + readStringBody(str, size);
+}
+
+template <class Transport_>
+uint32_t TBinaryProtocolT<Transport_>::readBinary(std::string& str) {
+ return TBinaryProtocolT<Transport_>::readString(str);
+}
+
+template <class Transport_>
+uint32_t TBinaryProtocolT<Transport_>::readStringBody(std::string& str,
+ int32_t size) {
+ uint32_t result = 0;
+
+ // Catch error cases
+ if (size < 0) {
+ throw TProtocolException(TProtocolException::NEGATIVE_SIZE);
+ }
+ if (this->string_limit_ > 0 && size > this->string_limit_) {
+ throw TProtocolException(TProtocolException::SIZE_LIMIT);
+ }
+
+ // Catch empty string case
+ if (size == 0) {
+ str = "";
+ return result;
+ }
+
+ // Try to borrow first
+ const uint8_t* borrow_buf;
+ uint32_t got = size;
+ if ((borrow_buf = this->trans_->borrow(NULL, &got))) {
+ str.assign((const char*)borrow_buf, size);
+ this->trans_->consume(size);
+ return size;
+ }
+
+ // Use the heap here to prevent stack overflow for v. large strings
+ if (size > this->string_buf_size_ || this->string_buf_ == NULL) {
+ void* new_string_buf = std::realloc(this->string_buf_, (uint32_t)size);
+ if (new_string_buf == NULL) {
+ throw TProtocolException(TProtocolException::UNKNOWN,
+ "Out of memory in TBinaryProtocolT::readString");
+ }
+ this->string_buf_ = (uint8_t*)new_string_buf;
+ this->string_buf_size_ = size;
+ }
+ this->trans_->readAll(this->string_buf_, size);
+ str = std::string((char*)this->string_buf_, size);
+ return (uint32_t)size;
+}
+
+}}} // apache::thrift::protocol
+
+#endif // #ifndef _THRIFT_PROTOCOL_TBINARYPROTOCOL_TCC_
diff --git a/lib/cpp/src/protocol/TCompactProtocol.h b/lib/cpp/src/protocol/TCompactProtocol.h
index 77c4454..2150cde 100644
--- a/lib/cpp/src/protocol/TCompactProtocol.h
+++ b/lib/cpp/src/protocol/TCompactProtocol.h
@@ -30,7 +30,9 @@
/**
* C++ Implementation of the Compact Protocol as described in THRIFT-110
*/
-class TCompactProtocol : public TVirtualProtocol<TCompactProtocol> {
+template <class Transport_>
+class TCompactProtocolT
+ : public TVirtualProtocol< TCompactProtocolT<Transport_> > {
protected:
static const int8_t PROTOCOL_ID = 0x82;
@@ -39,6 +41,8 @@
static const int8_t TYPE_MASK = 0xE0; // 1110 0000
static const int32_t TYPE_SHIFT_AMOUNT = 5;
+ Transport_* trans_;
+
/**
* (Writing) If we encounter a boolean field begin, save the TField here
* so it can have the value incorporated.
@@ -66,27 +70,10 @@
std::stack<int16_t> lastField_;
int16_t lastFieldId_;
- enum Types {
- CT_STOP = 0x00,
- CT_BOOLEAN_TRUE = 0x01,
- CT_BOOLEAN_FALSE = 0x02,
- CT_BYTE = 0x03,
- CT_I16 = 0x04,
- CT_I32 = 0x05,
- CT_I64 = 0x06,
- CT_DOUBLE = 0x07,
- CT_BINARY = 0x08,
- CT_LIST = 0x09,
- CT_SET = 0x0A,
- CT_MAP = 0x0B,
- CT_STRUCT = 0x0C,
- };
-
- static const int8_t TTypeToCType[16];
-
public:
- TCompactProtocol(boost::shared_ptr<TTransport> trans) :
- TVirtualProtocol<TCompactProtocol>(trans),
+ TCompactProtocolT(boost::shared_ptr<Transport_> trans) :
+ TVirtualProtocol< TCompactProtocolT<Transport_> >(trans),
+ trans_(trans.get()),
lastFieldId_(0),
string_limit_(0),
string_buf_(NULL),
@@ -96,10 +83,11 @@
boolValue_.hasBoolValue = false;
}
- TCompactProtocol(boost::shared_ptr<TTransport> trans,
- int32_t string_limit,
- int32_t container_limit) :
- TVirtualProtocol<TCompactProtocol>(trans),
+ TCompactProtocolT(boost::shared_ptr<Transport_> trans,
+ int32_t string_limit,
+ int32_t container_limit) :
+ TVirtualProtocol< TCompactProtocolT<Transport_> >(trans),
+ trans_(trans.get()),
lastFieldId_(0),
string_limit_(string_limit),
string_buf_(NULL),
@@ -109,7 +97,7 @@
boolValue_.hasBoolValue = false;
}
- ~TCompactProtocol() {
+ ~TCompactProtocolT() {
free(string_buf_);
}
@@ -244,20 +232,23 @@
int32_t container_limit_;
};
+typedef TCompactProtocolT<TTransport> TCompactProtocol;
+
/**
* Constructs compact protocol handlers
*/
-class TCompactProtocolFactory : public TProtocolFactory {
+template <class Transport_>
+class TCompactProtocolFactoryT : public TProtocolFactory {
public:
- TCompactProtocolFactory() :
+ TCompactProtocolFactoryT() :
string_limit_(0),
container_limit_(0) {}
- TCompactProtocolFactory(int32_t string_limit, int32_t container_limit) :
+ TCompactProtocolFactoryT(int32_t string_limit, int32_t container_limit) :
string_limit_(string_limit),
container_limit_(container_limit) {}
- virtual ~TCompactProtocolFactory() {}
+ virtual ~TCompactProtocolFactoryT() {}
void setStringSizeLimit(int32_t string_limit) {
string_limit_ = string_limit;
@@ -268,7 +259,17 @@
}
boost::shared_ptr<TProtocol> getProtocol(boost::shared_ptr<TTransport> trans) {
- return boost::shared_ptr<TProtocol>(new TCompactProtocol(trans, string_limit_, container_limit_));
+ boost::shared_ptr<Transport_> specific_trans =
+ boost::dynamic_pointer_cast<Transport_>(trans);
+ TProtocol* prot;
+ if (specific_trans) {
+ prot = new TCompactProtocolT<Transport_>(specific_trans, string_limit_,
+ container_limit_);
+ } else {
+ prot = new TCompactProtocol(trans, string_limit_, container_limit_);
+ }
+
+ return boost::shared_ptr<TProtocol>(prot);
}
private:
@@ -277,6 +278,10 @@
};
+typedef TCompactProtocolFactoryT<TTransport> TCompactProtocolFactory;
+
}}} // apache::thrift::protocol
+#include "TCompactProtocol.tcc"
+
#endif
diff --git a/lib/cpp/src/protocol/TCompactProtocol.cpp b/lib/cpp/src/protocol/TCompactProtocol.tcc
similarity index 67%
rename from lib/cpp/src/protocol/TCompactProtocol.cpp
rename to lib/cpp/src/protocol/TCompactProtocol.tcc
index ce2ee54..2448146 100644
--- a/lib/cpp/src/protocol/TCompactProtocol.cpp
+++ b/lib/cpp/src/protocol/TCompactProtocol.tcc
@@ -16,10 +16,9 @@
* specific language governing permissions and limitations
* under the License.
*/
+#ifndef _THRIFT_PROTOCOL_TCOMPACTPROTOCOL_TCC_
+#define _THRIFT_PROTOCOL_TCOMPACTPROTOCOL_TCC_ 1
-#include "TCompactProtocol.h"
-
-#include <config.h>
#include <limits>
/*
@@ -33,7 +32,7 @@
# error "Unable to determine the behavior of a signed right shift"
#endif
#if SIGNED_RIGHT_SHIFT_IS != ARITHMETIC_RIGHT_SHIFT
-# error "TCompactProtocol currenly only works if a signed right shift is arithmetic"
+# error "TCompactProtocol currently only works if a signed right shift is arithmetic"
#endif
#ifdef __GNUC__
@@ -44,29 +43,51 @@
namespace apache { namespace thrift { namespace protocol {
-const int8_t TCompactProtocol::TTypeToCType[16] = {
- CT_STOP, // T_STOP
- 0, // unused
- CT_BOOLEAN_TRUE, // T_BOOL
- CT_BYTE, // T_BYTE
- CT_DOUBLE, // T_DOUBLE
- 0, // unused
- CT_I16, // T_I16
- 0, // unused
- CT_I32, // T_I32
- 0, // unused
- CT_I64, // T_I64
- CT_BINARY, // T_STRING
- CT_STRUCT, // T_STRUCT
- CT_MAP, // T_MAP
- CT_SET, // T_SET
- CT_LIST, // T_LIST
- };
+namespace detail { namespace compact {
+
+enum Types {
+ CT_STOP = 0x00,
+ CT_BOOLEAN_TRUE = 0x01,
+ CT_BOOLEAN_FALSE = 0x02,
+ CT_BYTE = 0x03,
+ CT_I16 = 0x04,
+ CT_I32 = 0x05,
+ CT_I64 = 0x06,
+ CT_DOUBLE = 0x07,
+ CT_BINARY = 0x08,
+ CT_LIST = 0x09,
+ CT_SET = 0x0A,
+ CT_MAP = 0x0B,
+ CT_STRUCT = 0x0C,
+};
+
+const int8_t TTypeToCType[16] = {
+ CT_STOP, // T_STOP
+ 0, // unused
+ CT_BOOLEAN_TRUE, // T_BOOL
+ CT_BYTE, // T_BYTE
+ CT_DOUBLE, // T_DOUBLE
+ 0, // unused
+ CT_I16, // T_I16
+ 0, // unused
+ CT_I32, // T_I32
+ 0, // unused
+ CT_I64, // T_I64
+ CT_BINARY, // T_STRING
+ CT_STRUCT, // T_STRUCT
+ CT_MAP, // T_MAP
+ CT_SET, // T_SET
+ CT_LIST, // T_LIST
+};
+
+}} // end detail::compact namespace
-uint32_t TCompactProtocol::writeMessageBegin(const std::string& name,
- const TMessageType messageType,
- const int32_t seqid) {
+template <class Transport_>
+uint32_t TCompactProtocolT<Transport_>::writeMessageBegin(
+ const std::string& name,
+ const TMessageType messageType,
+ const int32_t seqid) {
uint32_t wsize = 0;
wsize += writeByte(PROTOCOL_ID);
wsize += writeByte((VERSION_N & VERSION_MASK) | (((int32_t)messageType << TYPE_SHIFT_AMOUNT) & TYPE_MASK));
@@ -81,9 +102,10 @@
* then the field id will be encoded in the 4 MSB as a delta. Otherwise, the
* field id will follow the type header as a zigzag varint.
*/
-uint32_t TCompactProtocol::writeFieldBegin(const char* name,
- const TType fieldType,
- const int16_t fieldId) {
+template <class Transport_>
+uint32_t TCompactProtocolT<Transport_>::writeFieldBegin(const char* name,
+ const TType fieldType,
+ const int16_t fieldId) {
if (fieldType == T_BOOL) {
booleanField_.name = name;
booleanField_.fieldType = fieldType;
@@ -97,7 +119,8 @@
/**
* Write the STOP symbol so we know there are no more fields in this struct.
*/
-uint32_t TCompactProtocol::writeFieldStop() {
+template <class Transport_>
+uint32_t TCompactProtocolT<Transport_>::writeFieldStop() {
return writeByte(T_STOP);
}
@@ -106,7 +129,8 @@
* use it as an opportunity to put special placeholder markers on the field
* stack so we can get the field id deltas correct.
*/
-uint32_t TCompactProtocol::writeStructBegin(const char* name) {
+template <class Transport_>
+uint32_t TCompactProtocolT<Transport_>::writeStructBegin(const char* name) {
lastField_.push(lastFieldId_);
lastFieldId_ = 0;
return 0;
@@ -117,7 +141,8 @@
* this as an opportunity to pop the last field from the current struct off
* of the field stack.
*/
-uint32_t TCompactProtocol::writeStructEnd() {
+template <class Transport_>
+uint32_t TCompactProtocolT<Transport_>::writeStructEnd() {
lastFieldId_ = lastField_.top();
lastField_.pop();
return 0;
@@ -126,16 +151,18 @@
/**
* Write a List header.
*/
-uint32_t TCompactProtocol::writeListBegin(const TType elemType,
- const uint32_t size) {
+template <class Transport_>
+uint32_t TCompactProtocolT<Transport_>::writeListBegin(const TType elemType,
+ const uint32_t size) {
return writeCollectionBegin(elemType, size);
}
/**
* Write a set header.
*/
-uint32_t TCompactProtocol::writeSetBegin(const TType elemType,
- const uint32_t size) {
+template <class Transport_>
+uint32_t TCompactProtocolT<Transport_>::writeSetBegin(const TType elemType,
+ const uint32_t size) {
return writeCollectionBegin(elemType, size);
}
@@ -143,9 +170,10 @@
* Write a map header. If the map is empty, omit the key and value type
* headers, as we don't need any additional information to skip it.
*/
-uint32_t TCompactProtocol::writeMapBegin(const TType keyType,
- const TType valType,
- const uint32_t size) {
+template <class Transport_>
+uint32_t TCompactProtocolT<Transport_>::writeMapBegin(const TType keyType,
+ const TType valType,
+ const uint32_t size) {
uint32_t wsize = 0;
if (size == 0) {
@@ -163,7 +191,8 @@
* right type header is for the value and then write the field header.
* Otherwise, write a single byte.
*/
-uint32_t TCompactProtocol::writeBool(const bool value) {
+template <class Transport_>
+uint32_t TCompactProtocolT<Transport_>::writeBool(const bool value) {
uint32_t wsize = 0;
if (booleanField_.name != NULL) {
@@ -171,16 +200,19 @@
wsize += writeFieldBeginInternal(booleanField_.name,
booleanField_.fieldType,
booleanField_.fieldId,
- value ? CT_BOOLEAN_TRUE : CT_BOOLEAN_FALSE);
+ value ? detail::compact::CT_BOOLEAN_TRUE :
+ detail::compact::CT_BOOLEAN_FALSE);
booleanField_.name = NULL;
} else {
// we're not part of a field, so just write the value
- wsize += writeByte(value ? CT_BOOLEAN_TRUE : CT_BOOLEAN_FALSE);
+ wsize += writeByte(value ? detail::compact::CT_BOOLEAN_TRUE :
+ detail::compact::CT_BOOLEAN_FALSE);
}
return wsize;
}
-uint32_t TCompactProtocol::writeByte(const int8_t byte) {
+template <class Transport_>
+uint32_t TCompactProtocolT<Transport_>::writeByte(const int8_t byte) {
trans_->write((uint8_t*)&byte, 1);
return 1;
}
@@ -188,28 +220,32 @@
/**
* Write an i16 as a zigzag varint.
*/
-uint32_t TCompactProtocol::writeI16(const int16_t i16) {
+template <class Transport_>
+uint32_t TCompactProtocolT<Transport_>::writeI16(const int16_t i16) {
return writeVarint32(i32ToZigzag(i16));
}
/**
* Write an i32 as a zigzag varint.
*/
-uint32_t TCompactProtocol::writeI32(const int32_t i32) {
+template <class Transport_>
+uint32_t TCompactProtocolT<Transport_>::writeI32(const int32_t i32) {
return writeVarint32(i32ToZigzag(i32));
}
/**
* Write an i64 as a zigzag varint.
*/
-uint32_t TCompactProtocol::writeI64(const int64_t i64) {
+template <class Transport_>
+uint32_t TCompactProtocolT<Transport_>::writeI64(const int64_t i64) {
return writeVarint64(i64ToZigzag(i64));
}
/**
* Write a double to the wire as 8 bytes.
*/
-uint32_t TCompactProtocol::writeDouble(const double dub) {
+template <class Transport_>
+uint32_t TCompactProtocolT<Transport_>::writeDouble(const double dub) {
BOOST_STATIC_ASSERT(sizeof(double) == sizeof(uint64_t));
BOOST_STATIC_ASSERT(std::numeric_limits<double>::is_iec559);
@@ -222,11 +258,13 @@
/**
* Write a string to the wire with a varint size preceeding.
*/
-uint32_t TCompactProtocol::writeString(const std::string& str) {
+template <class Transport_>
+uint32_t TCompactProtocolT<Transport_>::writeString(const std::string& str) {
return writeBinary(str);
}
-uint32_t TCompactProtocol::writeBinary(const std::string& str) {
+template <class Transport_>
+uint32_t TCompactProtocolT<Transport_>::writeBinary(const std::string& str) {
uint32_t ssize = str.size();
uint32_t wsize = writeVarint32(ssize) + ssize;
trans_->write((uint8_t*)str.data(), ssize);
@@ -242,10 +280,12 @@
* 'type override' of the type header. This is used specifically in the
* boolean field case.
*/
-int32_t TCompactProtocol::writeFieldBeginInternal(const char* name,
- const TType fieldType,
- const int16_t fieldId,
- int8_t typeOverride) {
+template <class Transport_>
+int32_t TCompactProtocolT<Transport_>::writeFieldBeginInternal(
+ const char* name,
+ const TType fieldType,
+ const int16_t fieldId,
+ int8_t typeOverride) {
uint32_t wsize = 0;
// if there's a type override, use that.
@@ -269,7 +309,9 @@
* Abstract method for writing the start of lists and sets. List and sets on
* the wire differ only by the type indicator.
*/
-uint32_t TCompactProtocol::writeCollectionBegin(int8_t elemType, int32_t size) {
+template <class Transport_>
+uint32_t TCompactProtocolT<Transport_>::writeCollectionBegin(int8_t elemType,
+ int32_t size) {
uint32_t wsize = 0;
if (size <= 14) {
wsize += writeByte(size << 4 | getCompactType(elemType));
@@ -283,7 +325,8 @@
/**
* Write an i32 as a varint. Results in 1-5 bytes on the wire.
*/
-uint32_t TCompactProtocol::writeVarint32(uint32_t n) {
+template <class Transport_>
+uint32_t TCompactProtocolT<Transport_>::writeVarint32(uint32_t n) {
uint8_t buf[5];
uint32_t wsize = 0;
@@ -303,7 +346,8 @@
/**
* Write an i64 as a varint. Results in 1-10 bytes on the wire.
*/
-uint32_t TCompactProtocol::writeVarint64(uint64_t n) {
+template <class Transport_>
+uint32_t TCompactProtocolT<Transport_>::writeVarint64(uint64_t n) {
uint8_t buf[10];
uint32_t wsize = 0;
@@ -324,7 +368,8 @@
* Convert l into a zigzag long. This allows negative numbers to be
* represented compactly as a varint.
*/
-uint64_t TCompactProtocol::i64ToZigzag(const int64_t l) {
+template <class Transport_>
+uint64_t TCompactProtocolT<Transport_>::i64ToZigzag(const int64_t l) {
return (l << 1) ^ (l >> 63);
}
@@ -332,15 +377,17 @@
* Convert n into a zigzag int. This allows negative numbers to be
* represented compactly as a varint.
*/
-uint32_t TCompactProtocol::i32ToZigzag(const int32_t n) {
+template <class Transport_>
+uint32_t TCompactProtocolT<Transport_>::i32ToZigzag(const int32_t n) {
return (n << 1) ^ (n >> 31);
}
/**
- * Given a TType value, find the appropriate TCompactProtocol.Type value
+ * Given a TType value, find the appropriate detail::compact::Types value
*/
-int8_t TCompactProtocol::getCompactType(int8_t ttype) {
- return TTypeToCType[ttype];
+template <class Transport_>
+int8_t TCompactProtocolT<Transport_>::getCompactType(int8_t ttype) {
+ return detail::compact::TTypeToCType[ttype];
}
//
@@ -350,9 +397,11 @@
/**
* Read a message header.
*/
-uint32_t TCompactProtocol::readMessageBegin(std::string& name,
- TMessageType& messageType,
- int32_t& seqid) {
+template <class Transport_>
+uint32_t TCompactProtocolT<Transport_>::readMessageBegin(
+ std::string& name,
+ TMessageType& messageType,
+ int32_t& seqid) {
uint32_t rsize = 0;
int8_t protocolId;
int8_t versionAndType;
@@ -380,7 +429,8 @@
* Read a struct begin. There's nothing on the wire for this, but it is our
* opportunity to push a new struct begin marker on the field stack.
*/
-uint32_t TCompactProtocol::readStructBegin(std::string& name) {
+template <class Transport_>
+uint32_t TCompactProtocolT<Transport_>::readStructBegin(std::string& name) {
name = "";
lastField_.push(lastFieldId_);
lastFieldId_ = 0;
@@ -391,7 +441,8 @@
* Doesn't actually consume any wire data, just removes the last field for
* this struct from the field stack.
*/
-uint32_t TCompactProtocol::readStructEnd() {
+template <class Transport_>
+uint32_t TCompactProtocolT<Transport_>::readStructEnd() {
lastFieldId_ = lastField_.top();
lastField_.pop();
return 0;
@@ -400,9 +451,10 @@
/**
* Read a field header off the wire.
*/
-uint32_t TCompactProtocol::readFieldBegin(std::string& name,
- TType& fieldType,
- int16_t& fieldId) {
+template <class Transport_>
+uint32_t TCompactProtocolT<Transport_>::readFieldBegin(std::string& name,
+ TType& fieldType,
+ int16_t& fieldId) {
uint32_t rsize = 0;
int8_t byte;
int8_t type;
@@ -428,10 +480,12 @@
fieldType = getTType(type);
// if this happens to be a boolean field, the value is encoded in the type
- if (type == CT_BOOLEAN_TRUE || type == CT_BOOLEAN_FALSE) {
+ if (type == detail::compact::CT_BOOLEAN_TRUE ||
+ type == detail::compact::CT_BOOLEAN_FALSE) {
// save the boolean value in a special instance variable.
boolValue_.hasBoolValue = true;
- boolValue_.boolValue = (type == CT_BOOLEAN_TRUE ? true : false);
+ boolValue_.boolValue =
+ (type == detail::compact::CT_BOOLEAN_TRUE ? true : false);
}
// push the new field onto the field stack so we can keep the deltas going.
@@ -444,9 +498,10 @@
* and value type. This means that 0-length maps will yield TMaps without the
* "correct" types.
*/
-uint32_t TCompactProtocol::readMapBegin(TType& keyType,
- TType& valType,
- uint32_t& size) {
+template <class Transport_>
+uint32_t TCompactProtocolT<Transport_>::readMapBegin(TType& keyType,
+ TType& valType,
+ uint32_t& size) {
uint32_t rsize = 0;
int8_t kvType = 0;
int32_t msize = 0;
@@ -474,8 +529,9 @@
* of the element type header will be 0xF, and a varint will follow with the
* true size.
*/
-uint32_t TCompactProtocol::readListBegin(TType& elemType,
- uint32_t& size) {
+template <class Transport_>
+uint32_t TCompactProtocolT<Transport_>::readListBegin(TType& elemType,
+ uint32_t& size) {
int8_t size_and_type;
uint32_t rsize = 0;
int32_t lsize;
@@ -505,8 +561,9 @@
* of the element type header will be 0xF, and a varint will follow with the
* true size.
*/
-uint32_t TCompactProtocol::readSetBegin(TType& elemType,
- uint32_t& size) {
+template <class Transport_>
+uint32_t TCompactProtocolT<Transport_>::readSetBegin(TType& elemType,
+ uint32_t& size) {
return readListBegin(elemType, size);
}
@@ -515,7 +572,8 @@
* already have been read during readFieldBegin, so we'll just consume the
* pre-stored value. Otherwise, read a byte.
*/
-uint32_t TCompactProtocol::readBool(bool& value) {
+template <class Transport_>
+uint32_t TCompactProtocolT<Transport_>::readBool(bool& value) {
if (boolValue_.hasBoolValue == true) {
value = boolValue_.boolValue;
boolValue_.hasBoolValue = false;
@@ -523,7 +581,7 @@
} else {
int8_t val;
readByte(val);
- value = (val == CT_BOOLEAN_TRUE);
+ value = (val == detail::compact::CT_BOOLEAN_TRUE);
return 1;
}
}
@@ -531,7 +589,8 @@
/**
* Read a single byte off the wire. Nothing interesting here.
*/
-uint32_t TCompactProtocol::readByte(int8_t& byte) {
+template <class Transport_>
+uint32_t TCompactProtocolT<Transport_>::readByte(int8_t& byte) {
uint8_t b[1];
trans_->readAll(b, 1);
byte = *(int8_t*)b;
@@ -541,7 +600,8 @@
/**
* Read an i16 from the wire as a zigzag varint.
*/
-uint32_t TCompactProtocol::readI16(int16_t& i16) {
+template <class Transport_>
+uint32_t TCompactProtocolT<Transport_>::readI16(int16_t& i16) {
int32_t value;
uint32_t rsize = readVarint32(value);
i16 = (int16_t)zigzagToI32(value);
@@ -551,7 +611,8 @@
/**
* Read an i32 from the wire as a zigzag varint.
*/
-uint32_t TCompactProtocol::readI32(int32_t& i32) {
+template <class Transport_>
+uint32_t TCompactProtocolT<Transport_>::readI32(int32_t& i32) {
int32_t value;
uint32_t rsize = readVarint32(value);
i32 = zigzagToI32(value);
@@ -561,7 +622,8 @@
/**
* Read an i64 from the wire as a zigzag varint.
*/
-uint32_t TCompactProtocol::readI64(int64_t& i64) {
+template <class Transport_>
+uint32_t TCompactProtocolT<Transport_>::readI64(int64_t& i64) {
int64_t value;
uint32_t rsize = readVarint64(value);
i64 = zigzagToI64(value);
@@ -571,7 +633,8 @@
/**
* No magic here - just read a double off the wire.
*/
-uint32_t TCompactProtocol::readDouble(double& dub) {
+template <class Transport_>
+uint32_t TCompactProtocolT<Transport_>::readDouble(double& dub) {
BOOST_STATIC_ASSERT(sizeof(double) == sizeof(uint64_t));
BOOST_STATIC_ASSERT(std::numeric_limits<double>::is_iec559);
@@ -584,14 +647,16 @@
return 8;
}
-uint32_t TCompactProtocol::readString(std::string& str) {
+template <class Transport_>
+uint32_t TCompactProtocolT<Transport_>::readString(std::string& str) {
return readBinary(str);
}
/**
* Read a byte[] from the wire.
*/
-uint32_t TCompactProtocol::readBinary(std::string& str) {
+template <class Transport_>
+uint32_t TCompactProtocolT<Transport_>::readBinary(std::string& str) {
int32_t rsize = 0;
int32_t size;
@@ -629,7 +694,8 @@
* Read an i32 from the wire as a varint. The MSB of each byte is set
* if there is another byte to follow. This can read up to 5 bytes.
*/
-uint32_t TCompactProtocol::readVarint32(int32_t& i32) {
+template <class Transport_>
+uint32_t TCompactProtocolT<Transport_>::readVarint32(int32_t& i32) {
int64_t val;
uint32_t rsize = readVarint64(val);
i32 = (int32_t)val;
@@ -640,7 +706,8 @@
* Read an i64 from the wire as a proper varint. The MSB of each byte is set
* if there is another byte to follow. This can read up to 10 bytes.
*/
-uint32_t TCompactProtocol::readVarint64(int64_t& i64) {
+template <class Transport_>
+uint32_t TCompactProtocolT<Transport_>::readVarint64(int64_t& i64) {
uint32_t rsize = 0;
uint64_t val = 0;
int shift = 0;
@@ -689,43 +756,46 @@
/**
* Convert from zigzag int to int.
*/
-int32_t TCompactProtocol::zigzagToI32(uint32_t n) {
+template <class Transport_>
+int32_t TCompactProtocolT<Transport_>::zigzagToI32(uint32_t n) {
return (n >> 1) ^ -(n & 1);
}
/**
* Convert from zigzag long to long.
*/
-int64_t TCompactProtocol::zigzagToI64(uint64_t n) {
+template <class Transport_>
+int64_t TCompactProtocolT<Transport_>::zigzagToI64(uint64_t n) {
return (n >> 1) ^ -(n & 1);
}
-TType TCompactProtocol::getTType(int8_t type) {
+template <class Transport_>
+TType TCompactProtocolT<Transport_>::getTType(int8_t type) {
switch (type) {
case T_STOP:
return T_STOP;
- case CT_BOOLEAN_FALSE:
- case CT_BOOLEAN_TRUE:
+ case detail::compact::CT_BOOLEAN_FALSE:
+ case detail::compact::CT_BOOLEAN_TRUE:
return T_BOOL;
- case CT_BYTE:
+ case detail::compact::CT_BYTE:
return T_BYTE;
- case CT_I16:
+ case detail::compact::CT_I16:
return T_I16;
- case CT_I32:
+ case detail::compact::CT_I32:
return T_I32;
- case CT_I64:
+ case detail::compact::CT_I64:
return T_I64;
- case CT_DOUBLE:
+ case detail::compact::CT_DOUBLE:
return T_DOUBLE;
- case CT_BINARY:
+ case detail::compact::CT_BINARY:
return T_STRING;
- case CT_LIST:
+ case detail::compact::CT_LIST:
return T_LIST;
- case CT_SET:
+ case detail::compact::CT_SET:
return T_SET;
- case CT_MAP:
+ case detail::compact::CT_MAP:
return T_MAP;
- case CT_STRUCT:
+ case detail::compact::CT_STRUCT:
return T_STRUCT;
default:
throw TException("don't know what type: " + type);
@@ -734,3 +804,5 @@
}
}}} // apache::thrift::protocol
+
+#endif // _THRIFT_PROTOCOL_TCOMPACTPROTOCOL_TCC_
diff --git a/lib/cpp/src/protocol/TDebugProtocol.h b/lib/cpp/src/protocol/TDebugProtocol.h
index 1efcbd0..3f7877c 100644
--- a/lib/cpp/src/protocol/TDebugProtocol.h
+++ b/lib/cpp/src/protocol/TDebugProtocol.h
@@ -59,6 +59,7 @@
public:
TDebugProtocol(boost::shared_ptr<TTransport> trans)
: TVirtualProtocol<TDebugProtocol>(trans)
+ , trans_(trans.get())
, string_limit_(DEFAULT_STRING_LIMIT)
, string_prefix_size_(DEFAULT_STRING_PREFIX_SIZE)
{
@@ -140,6 +141,8 @@
static std::string fieldTypeName(TType type);
+ TTransport* trans_;
+
int32_t string_limit_;
int32_t string_prefix_size_;
diff --git a/lib/cpp/src/protocol/TJSONProtocol.cpp b/lib/cpp/src/protocol/TJSONProtocol.cpp
index ed2f518..9859c0f 100644
--- a/lib/cpp/src/protocol/TJSONProtocol.cpp
+++ b/lib/cpp/src/protocol/TJSONProtocol.cpp
@@ -358,6 +358,7 @@
TJSONProtocol::TJSONProtocol(boost::shared_ptr<TTransport> ptrans) :
TVirtualProtocol<TJSONProtocol>(ptrans),
+ trans_(ptrans.get()),
context_(new TJSONContext()),
reader_(*ptrans) {
}
diff --git a/lib/cpp/src/protocol/TJSONProtocol.h b/lib/cpp/src/protocol/TJSONProtocol.h
index cd42f5e..b3a6667 100644
--- a/lib/cpp/src/protocol/TJSONProtocol.h
+++ b/lib/cpp/src/protocol/TJSONProtocol.h
@@ -291,6 +291,7 @@
};
private:
+ TTransport* trans_;
std::stack<boost::shared_ptr<TJSONContext> > contexts_;
boost::shared_ptr<TJSONContext> context_;
diff --git a/lib/cpp/src/protocol/TProtocol.h b/lib/cpp/src/protocol/TProtocol.h
index 6bf7e3b..4b05de2 100644
--- a/lib/cpp/src/protocol/TProtocol.h
+++ b/lib/cpp/src/protocol/TProtocol.h
@@ -645,11 +645,9 @@
protected:
TProtocol(boost::shared_ptr<TTransport> ptrans):
ptrans_(ptrans) {
- trans_ = ptrans.get();
}
boost::shared_ptr<TTransport> ptrans_;
- TTransport* trans_;
private:
TProtocol() {}