Added read/write I16, U16 and Bool methods to TProtocol
Modified code generation to define structs and io methods for function argument lists and server process call implementations
git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@664749 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/cpp/src/protocol/TBinaryProtocol.cc b/lib/cpp/src/protocol/TBinaryProtocol.cc
index 624965b..f1e4ec9 100644
--- a/lib/cpp/src/protocol/TBinaryProtocol.cc
+++ b/lib/cpp/src/protocol/TBinaryProtocol.cc
@@ -28,10 +28,10 @@
uint32_t TBinaryProtocol::writeFieldBegin(shared_ptr<TTransport> out,
const string& name,
const TType fieldType,
- const uint16_t fieldId) const {
+ const int16_t fieldId) const {
return
writeByte(out, (uint8_t)fieldType) +
- writeI32(out, (int32_t)fieldId);
+ writeI16(out, fieldId);
}
uint32_t TBinaryProtocol::writeFieldEnd(shared_ptr<TTransport> out) const {
@@ -81,24 +81,31 @@
return 0;
}
+uint32_t TBinaryProtocol::writeBool(shared_ptr<TTransport> out,
+ const bool value) const {
+ uint8_t tmp = value ? 1 : 0;
+ out->write(&tmp, 1);
+ return 1;
+}
+
uint32_t TBinaryProtocol::writeByte(shared_ptr<TTransport> out,
const uint8_t byte) const {
out->write(&byte, 1);
return 1;
}
-uint32_t TBinaryProtocol::writeU32(shared_ptr<TTransport> out,
- const uint32_t u32) const {
- uint32_t net = (uint32_t)htonl(u32);
- out->write((uint8_t*)&net, 4);
- return 4;
+uint32_t TBinaryProtocol::writeU16(shared_ptr<TTransport> out,
+ const uint16_t u16) const {
+ uint16_t net = (uint16_t)htons(u16);
+ out->write((uint8_t*)&net, 2);
+ return 2;
}
-uint32_t TBinaryProtocol::writeI32(shared_ptr<TTransport> out,
- const int32_t i32) const {
- int32_t net = (int32_t)htonl(i32);
- out->write((uint8_t*)&net, 4);
- return 4;
+uint32_t TBinaryProtocol::writeI16(shared_ptr<TTransport> out,
+ const int16_t i16) const {
+ int16_t net = (int16_t)htons(i16);
+ out->write((uint8_t*)&net, 2);
+ return 2;
}
uint32_t TBinaryProtocol::writeU64(shared_ptr<TTransport> out,
@@ -155,7 +162,7 @@
uint32_t TBinaryProtocol::readFieldBegin(shared_ptr<TTransport> in,
string& name,
TType& fieldType,
- uint16_t& fieldId) const {
+ int16_t& fieldId) const {
uint32_t result = 0;
uint8_t type;
result += readByte(in, type);
@@ -164,9 +171,7 @@
fieldId = 0;
return result;
}
- int32_t id;
- result += readI32(in, id);
- fieldId = (uint16_t)id;
+ result += readI16(in, fieldId);
return result;
}
@@ -222,6 +227,14 @@
return 0;
}
+uint32_t TBinaryProtocol::readBool(shared_ptr<TTransport> in,
+ bool& value) const {
+ uint8_t b[1];
+ in->readAll(b, 1);
+ value = *(uint8_t*)b != 0;
+ return 1;
+}
+
uint32_t TBinaryProtocol::readByte(shared_ptr<TTransport> in,
uint8_t& byte) const {
uint8_t b[1];
@@ -230,6 +243,24 @@
return 1;
}
+uint32_t TBinaryProtocol::readU16(shared_ptr<TTransport> in,
+ uint16_t& u16) const {
+ uint8_t b[2];
+ in->readAll(b, 2);
+ u16 = *(uint16_t*)b;
+ u16 = (uint16_t)ntohs(u16);
+ return 2;
+}
+
+uint32_t TBinaryProtocol::readI16(shared_ptr<TTransport> in,
+ int16_t& i16) const {
+ uint8_t b[2];
+ in->readAll(b, 2);
+ i16 = *(int16_t*)b;
+ i16 = (int16_t)ntohs(i16);
+ return 2;
+}
+
uint32_t TBinaryProtocol::readU32(shared_ptr<TTransport> in,
uint32_t& u32) const {
uint8_t b[4];
diff --git a/lib/cpp/src/protocol/TBinaryProtocol.h b/lib/cpp/src/protocol/TBinaryProtocol.h
index db7960f..89f4d4a 100644
--- a/lib/cpp/src/protocol/TBinaryProtocol.h
+++ b/lib/cpp/src/protocol/TBinaryProtocol.h
@@ -15,8 +15,8 @@
*
* @author Mark Slee <mcslee@facebook.com>
*/
-class TBinaryProtocol : public TProtocol {
- public:
+ class TBinaryProtocol : public TProtocol {
+ public:
TBinaryProtocol() {}
~TBinaryProtocol() {}
@@ -28,120 +28,137 @@
const TMessageType messageType,
const uint32_t seqid) const;
- virtual uint32_t writeMessageEnd (shared_ptr<TTransport> out) const;
+ virtual uint32_t writeMessageEnd(shared_ptr<TTransport> out) const;
- uint32_t writeStructBegin (shared_ptr<TTransport> out,
- const std::string& name) const;
+ uint32_t writeStructBegin(shared_ptr<TTransport> out,
+ const std::string& name) const;
- uint32_t writeStructEnd (shared_ptr<TTransport> out) const;
+ uint32_t writeStructEnd(shared_ptr<TTransport> out) const;
- uint32_t writeFieldBegin (shared_ptr<TTransport> out,
- const std::string& name,
- const TType fieldType,
- const uint16_t fieldId) const;
+ uint32_t writeFieldBegin(shared_ptr<TTransport> out,
+ const std::string& name,
+ const TType fieldType,
+ const int16_t fieldId) const;
- uint32_t writeFieldEnd (shared_ptr<TTransport> out) const;
+ uint32_t writeFieldEnd(shared_ptr<TTransport> out) const;
- uint32_t writeFieldStop (shared_ptr<TTransport> out) const;
+ uint32_t writeFieldStop(shared_ptr<TTransport> out) const;
- uint32_t writeMapBegin (shared_ptr<TTransport> out,
- const TType keyType,
- const TType valType,
- const int32_t size) const;
+ uint32_t writeMapBegin(shared_ptr<TTransport> out,
+ const TType keyType,
+ const TType valType,
+ const int32_t size) const;
- uint32_t writeMapEnd (shared_ptr<TTransport> out) const;
+ uint32_t writeMapEnd(shared_ptr<TTransport> out) const;
- uint32_t writeListBegin (shared_ptr<TTransport> out,
- const TType elemType,
- const int32_t size) const;
+ uint32_t writeListBegin(shared_ptr<TTransport> out,
+ const TType elemType,
+ const int32_t size) const;
- uint32_t writeListEnd (shared_ptr<TTransport> out) const;
+ uint32_t writeListEnd(shared_ptr<TTransport> out) const;
- uint32_t writeSetBegin (shared_ptr<TTransport> out,
- const TType elemType,
- const int32_t size) const;
+ uint32_t writeSetBegin(shared_ptr<TTransport> out,
+ const TType elemType,
+ const int32_t size) const;
- uint32_t writeSetEnd (shared_ptr<TTransport> out) const;
+ uint32_t writeSetEnd(shared_ptr<TTransport> out) const;
- uint32_t writeByte (shared_ptr<TTransport> out,
- const uint8_t byte) const;
+ uint32_t writeBool(shared_ptr<TTransport> out,
+ const bool value) const;
- uint32_t writeU32 (shared_ptr<TTransport> out,
- const uint32_t u32) const;
+ uint32_t writeByte(shared_ptr<TTransport> out,
+ const uint8_t byte) const;
- uint32_t writeI32 (shared_ptr<TTransport> out,
- const int32_t i32) const;
+ uint32_t writeU16(shared_ptr<TTransport> out,
+ const uint16_t u16) const;
- uint32_t writeU64 (shared_ptr<TTransport> out,
- const uint64_t u64) const;
+ uint32_t writeI16(shared_ptr<TTransport> out,
+ const int16_t i16) const;
- uint32_t writeI64 (shared_ptr<TTransport> out,
- const int64_t i64) const;
+ uint32_t writeU32(shared_ptr<TTransport> out,
+ const uint32_t u32) const;
- uint32_t writeString (shared_ptr<TTransport> out,
- const std::string& str) const;
+ uint32_t writeI32(shared_ptr<TTransport> out,
+ const int32_t i32) const;
+
+ uint32_t writeU64(shared_ptr<TTransport> out,
+ const uint64_t u64) const;
+
+ uint32_t writeI64(shared_ptr<TTransport> out,
+ const int64_t i64) const;
+
+ uint32_t writeString(shared_ptr<TTransport> out,
+ const std::string& str) const;
/**
* Reading functions
*/
- uint32_t readMessasgeBegin (shared_ptr<TTransport> in,
- TMessageType& messageType,
- uint32_t& seqid) const;
+ uint32_t readMessasgeBegin(shared_ptr<TTransport> in,
+ TMessageType& messageType,
+ uint32_t& seqid) const;
- uint32_t readMessageEnd (shared_ptr<TTransport> in) const;
+ uint32_t readMessageEnd(shared_ptr<TTransport> in) const;
- uint32_t readStructBegin (shared_ptr<TTransport> in,
- std::string& name) const;
+ uint32_t readStructBegin(shared_ptr<TTransport> in,
+ std::string& name) const;
- uint32_t readStructEnd (shared_ptr<TTransport> in) const;
+ uint32_t readStructEnd(shared_ptr<TTransport> in) const;
- uint32_t readFieldBegin (shared_ptr<TTransport> in,
- std::string& name,
- TType& fieldType,
- uint16_t& fieldId) const;
+ uint32_t readFieldBegin(shared_ptr<TTransport> in,
+ std::string& name,
+ TType& fieldType,
+ int16_t& fieldId) const;
- uint32_t readFieldEnd (shared_ptr<TTransport> in) const;
+ uint32_t readFieldEnd(shared_ptr<TTransport> in) const;
- uint32_t readMapBegin (shared_ptr<TTransport> in,
- TType& keyType,
- TType& valType,
- int32_t& size) const;
+ uint32_t readMapBegin(shared_ptr<TTransport> in,
+ TType& keyType,
+ TType& valType,
+ int32_t& size) const;
- uint32_t readMapEnd (shared_ptr<TTransport> in) const;
+ uint32_t readMapEnd(shared_ptr<TTransport> in) const;
- uint32_t readListBegin (shared_ptr<TTransport> in,
- TType& elemType,
- int32_t& size) const;
+ uint32_t readListBegin(shared_ptr<TTransport> in,
+ TType& elemType,
+ int32_t& size) const;
- uint32_t readListEnd (shared_ptr<TTransport> in) const;
+ uint32_t readListEnd(shared_ptr<TTransport> in) const;
- uint32_t readSetBegin (shared_ptr<TTransport> in,
- TType& elemType,
- int32_t& size) const;
+ uint32_t readSetBegin(shared_ptr<TTransport> in,
+ TType& elemType,
+ int32_t& size) const;
- uint32_t readSetEnd (shared_ptr<TTransport> in) const;
+ uint32_t readSetEnd(shared_ptr<TTransport> in) const;
- uint32_t readByte (shared_ptr<TTransport> in,
- uint8_t& byte) const;
+ uint32_t readBool(shared_ptr<TTransport> in,
+ bool& value) const;
- uint32_t readU32 (shared_ptr<TTransport> in,
- uint32_t& u32) const;
+ uint32_t readByte(shared_ptr<TTransport> in,
+ uint8_t& byte) const;
- uint32_t readI32 (shared_ptr<TTransport> in,
- int32_t& i32) const;
+ uint32_t readU16(shared_ptr<TTransport> in,
+ uint16_t& u16) const;
- uint32_t readU64 (shared_ptr<TTransport> in,
- uint64_t& u64) const;
+ uint32_t readI16(shared_ptr<TTransport> in,
+ int16_t& i16) const;
- uint32_t readI64 (shared_ptr<TTransport> in,
- int64_t& i64) const;
+ uint32_t readU32(shared_ptr<TTransport> in,
+ uint32_t& u32) const;
- uint32_t readString (shared_ptr<TTransport> in,
- std::string& str) const;
+ uint32_t readI32(shared_ptr<TTransport> in,
+ int32_t& i32) const;
+ uint32_t readU64(shared_ptr<TTransport> in,
+ uint64_t& u64) const;
+
+ uint32_t readI64(shared_ptr<TTransport> in,
+ int64_t& i64) const;
+
+ uint32_t readString(shared_ptr<TTransport> in,
+ std::string& str) const;
};
}}} // facebook::thrift::protocol
diff --git a/lib/cpp/src/protocol/TProtocol.h b/lib/cpp/src/protocol/TProtocol.h
index aa67e9c..8c89ba5 100644
--- a/lib/cpp/src/protocol/TProtocol.h
+++ b/lib/cpp/src/protocol/TProtocol.h
@@ -77,122 +77,140 @@
* Writing functions.
*/
- virtual uint32_t writeMessageBegin (shared_ptr<TTransport> out,
- const TMessageType messageType,
- const uint32_t seqid) const = 0;
+ virtual uint32_t writeMessageBegin(shared_ptr<TTransport> out,
+ const TMessageType messageType,
+ const uint32_t seqid) const = 0;
- virtual uint32_t writeMessageEnd (shared_ptr<TTransport> out) const = 0;
+ virtual uint32_t writeMessageEnd(shared_ptr<TTransport> out) const = 0;
- virtual uint32_t writeStructBegin (shared_ptr<TTransport> out,
- const std::string& name) const = 0;
+ virtual uint32_t writeStructBegin(shared_ptr<TTransport> out,
+ const std::string& name) const = 0;
+
+ virtual uint32_t writeStructEnd(shared_ptr<TTransport> out) const = 0;
+
+ virtual uint32_t writeFieldBegin(shared_ptr<TTransport> out,
+ const std::string& name,
+ const TType fieldType,
+ const int16_t fieldId) const = 0;
- virtual uint32_t writeStructEnd (shared_ptr<TTransport> out) const = 0;
+ virtual uint32_t writeFieldEnd(shared_ptr<TTransport> out) const = 0;
- virtual uint32_t writeFieldBegin (shared_ptr<TTransport> out,
- const std::string& name,
- const TType fieldType,
- const uint16_t fieldId) const = 0;
-
- virtual uint32_t writeFieldEnd (shared_ptr<TTransport> out) const = 0;
-
- virtual uint32_t writeFieldStop (shared_ptr<TTransport> out) const = 0;
+ virtual uint32_t writeFieldStop(shared_ptr<TTransport> out) const = 0;
- virtual uint32_t writeMapBegin (shared_ptr<TTransport> out,
- const TType keyType,
- const TType valType,
- const int32_t size) const = 0;
+ virtual uint32_t writeMapBegin(shared_ptr<TTransport> out,
+ const TType keyType,
+ const TType valType,
+ const int32_t size) const = 0;
- virtual uint32_t writeMapEnd (shared_ptr<TTransport> out) const = 0;
+ virtual uint32_t writeMapEnd(shared_ptr<TTransport> out) const = 0;
+
+ virtual uint32_t writeListBegin(shared_ptr<TTransport> out,
+ const TType elemType,
+ const int32_t size) const = 0;
- virtual uint32_t writeListBegin (shared_ptr<TTransport> out,
- const TType elemType,
- const int32_t size) const = 0;
+ virtual uint32_t writeListEnd(shared_ptr<TTransport> out) const = 0;
- virtual uint32_t writeListEnd (shared_ptr<TTransport> out) const = 0;
+ virtual uint32_t writeSetBegin(shared_ptr<TTransport> out,
+ const TType elemType,
+ const int32_t size) const = 0;
- virtual uint32_t writeSetBegin (shared_ptr<TTransport> out,
- const TType elemType,
- const int32_t size) const = 0;
+ virtual uint32_t writeSetEnd(shared_ptr<TTransport> out) const = 0;
- virtual uint32_t writeSetEnd (shared_ptr<TTransport> out) const = 0;
+ virtual uint32_t writeBool(shared_ptr<TTransport> out,
+ const bool value) const = 0;
- virtual uint32_t writeByte (shared_ptr<TTransport> out,
- const uint8_t byte) const = 0;
+ virtual uint32_t writeByte(shared_ptr<TTransport> out,
+ const uint8_t byte) const = 0;
- virtual uint32_t writeU32 (shared_ptr<TTransport> out,
- const uint32_t u32) const = 0;
+ virtual uint32_t writeI16(shared_ptr<TTransport> out,
+ const int16_t i16) const = 0;
- virtual uint32_t writeI32 (shared_ptr<TTransport> out,
- const int32_t i32) const = 0;
+ virtual uint32_t writeU16(shared_ptr<TTransport> out,
+ const uint16_t u16) const = 0;
- virtual uint32_t writeU64 (shared_ptr<TTransport> out,
- const uint64_t u64) const = 0;
+ virtual uint32_t writeU32(shared_ptr<TTransport> out,
+ const uint32_t u32) const = 0;
+
+ virtual uint32_t writeI32(shared_ptr<TTransport> out,
+ const int32_t i32) const = 0;
- virtual uint32_t writeI64 (shared_ptr<TTransport> out,
- const int64_t i64) const = 0;
+ virtual uint32_t writeU64(shared_ptr<TTransport> out,
+ const uint64_t u64) const = 0;
- virtual uint32_t writeString (shared_ptr<TTransport> out,
- const std::string& str) const = 0;
+ virtual uint32_t writeI64(shared_ptr<TTransport> out,
+ const int64_t i64) const = 0;
+
+ virtual uint32_t writeString(shared_ptr<TTransport> out,
+ const std::string& str) const = 0;
/**
* Reading functions
*/
- virtual uint32_t readMessasgeBegin (shared_ptr<TTransport> in,
- TMessageType& messageType,
- uint32_t& seqid) const = 0;
-
- virtual uint32_t readMessageEnd (shared_ptr<TTransport> in) const = 0;
-
- virtual uint32_t readStructBegin (shared_ptr<TTransport> in,
- std::string& name) const = 0;
-
- virtual uint32_t readStructEnd (shared_ptr<TTransport> in) const = 0;
-
- virtual uint32_t readFieldBegin (shared_ptr<TTransport> in,
- std::string& name,
- TType& fieldType,
- uint16_t& fieldId) const = 0;
+ virtual uint32_t readMessasgeBegin(shared_ptr<TTransport> in,
+ TMessageType& messageType,
+ uint32_t& seqid) const = 0;
- virtual uint32_t readFieldEnd (shared_ptr<TTransport> in) const = 0;
+ virtual uint32_t readMessageEnd(shared_ptr<TTransport> in) const = 0;
+
+ virtual uint32_t readStructBegin(shared_ptr<TTransport> in,
+ std::string& name) const = 0;
+
+ virtual uint32_t readStructEnd(shared_ptr<TTransport> in) const = 0;
+
+ virtual uint32_t readFieldBegin(shared_ptr<TTransport> in,
+ std::string& name,
+ TType& fieldType,
+ int16_t& fieldId) const = 0;
+
+ virtual uint32_t readFieldEnd(shared_ptr<TTransport> in) const = 0;
- virtual uint32_t readMapBegin (shared_ptr<TTransport> in,
- TType& keyType,
- TType& valType,
- int32_t& size) const = 0;
+ virtual uint32_t readMapBegin(shared_ptr<TTransport> in,
+ TType& keyType,
+ TType& valType,
+ int32_t& size) const = 0;
- virtual uint32_t readMapEnd (shared_ptr<TTransport> in) const = 0;
+ virtual uint32_t readMapEnd(shared_ptr<TTransport> in) const = 0;
- virtual uint32_t readListBegin (shared_ptr<TTransport> in,
- TType& elemType,
- int32_t& size) const = 0;
+ virtual uint32_t readListBegin(shared_ptr<TTransport> in,
+ TType& elemType,
+ int32_t& size) const = 0;
- virtual uint32_t readListEnd (shared_ptr<TTransport> in) const = 0;
+ virtual uint32_t readListEnd(shared_ptr<TTransport> in) const = 0;
- virtual uint32_t readSetBegin (shared_ptr<TTransport> in,
- TType& elemType,
- int32_t& size) const = 0;
+ virtual uint32_t readSetBegin(shared_ptr<TTransport> in,
+ TType& elemType,
+ int32_t& size) const = 0;
- virtual uint32_t readSetEnd (shared_ptr<TTransport> in) const = 0;
+ virtual uint32_t readSetEnd(shared_ptr<TTransport> in) const = 0;
- virtual uint32_t readByte (shared_ptr<TTransport> in,
- uint8_t& byte) const = 0;
+ virtual uint32_t readBool(shared_ptr<TTransport> in,
+ bool& value) const = 0;
- virtual uint32_t readU32 (shared_ptr<TTransport> in,
- uint32_t& u32) const = 0;
+ virtual uint32_t readByte(shared_ptr<TTransport> in,
+ uint8_t& byte) const = 0;
- virtual uint32_t readI32 (shared_ptr<TTransport> in,
- int32_t& i32) const = 0;
+ virtual uint32_t readU16(shared_ptr<TTransport> in,
+ uint16_t& u16) const = 0;
- virtual uint32_t readU64 (shared_ptr<TTransport> in,
- uint64_t& u64) const = 0;
+ virtual uint32_t readI16(shared_ptr<TTransport> in,
+ int16_t& i16) const = 0;
- virtual uint32_t readI64 (shared_ptr<TTransport> in,
- int64_t& i64) const = 0;
+ virtual uint32_t readU32(shared_ptr<TTransport> in,
+ uint32_t& u32) const = 0;
- virtual uint32_t readString (shared_ptr<TTransport> in,
- std::string& str) const = 0;
+ virtual uint32_t readI32(shared_ptr<TTransport> in,
+ int32_t& i32) const = 0;
+
+ virtual uint32_t readU64(shared_ptr<TTransport> in,
+ uint64_t& u64) const = 0;
+
+ virtual uint32_t readI64(shared_ptr<TTransport> in,
+ int64_t& i64) const = 0;
+
+ virtual uint32_t readString(shared_ptr<TTransport> in,
+ std::string& str) const = 0;
/**
* Method to arbitrarily skip over data.
@@ -233,7 +251,7 @@
{
uint32_t result = 0;
std::string name;
- uint16_t fid;
+ int16_t fid;
TType ftype;
result += readStructBegin(in, name);
while (true) {