From: yunfang Date: Sun, 19 Aug 2007 22:18:38 +0000 (+0000) Subject: [adding TStringBuffer for serialization/deserialization from a string] X-Git-Tag: 0.2.0~1263 X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=1ec47d4395be54319598aa5cd74ee8d44246b335;p=common%2Fthrift.git [adding TStringBuffer for serialization/deserialization from a string] Summary: /** * A string buffer is a tranpsort that simply reads from and writes to a * string. Anytime you call write on it, the data is serialized * into the underlying buffer, you can call getString() to get the serialized * string. Before you call read, you should call resetString(data) to set the * underlying buffer, you can then call read to get the * de-serialized data structure. * * The string buffer is inherited from the memory buffer * Thus, buffers are allocated using C constructs malloc,realloc, and the size * doubles as necessary. */ Reviewed by: aditya Test Plan: int main(int argc, char** argv) { shared_ptr strBuffer(new TStringBuffer()); shared_ptr binaryProtcol(new TBinaryProtocol(strBuffer)); testStruct a; a.i1 = 10; a.i2 = 30; a.s1 = string("holla back a"); a.write(binaryProtcol.get()); string serialized = strBuffer->getString(); shared_ptr strBuffer2(new TStringBuffer()); shared_ptr binaryProtcol2(new TBinaryProtocol(strBuffer2)); strBuffer2->resetString(serialized); testStruct a2; a2.read(binaryProtcol2.get()); if (a == a2) { printf("serialization working\n"); } else { printf("serialization not working\n"); } } git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@665209 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/lib/cpp/src/transport/TTransportUtils.h b/lib/cpp/src/transport/TTransportUtils.h index 3db22e7c..102e848f 100644 --- a/lib/cpp/src/transport/TTransportUtils.h +++ b/lib/cpp/src/transport/TTransportUtils.h @@ -8,6 +8,7 @@ #define _THRIFT_TRANSPORT_TTRANSPORTUTILS_H_ 1 #include +#include #include #include @@ -330,24 +331,82 @@ class TMemoryBuffer : public TTransport { return wPos_ - rPos_; } - private: + protected: // Data buffer uint8_t* buffer_; // Allocated buffer size uint32_t bufferSize_; + // Is this object the owner of the buffer? + bool owner_; + + private: // Where the write is at uint32_t wPos_; // Where the reader is at uint32_t rPos_; - // Is this object the owner of the buffer? - bool owner_; }; +/** + * A string buffer is a tranpsort that simply reads from and writes to a + * string. Anytime you call write on it, the data is serialized + * into the underlying buffer, you can call getString() to get the serialized + * string. Before you call read, you should call resetString(data) to set the + * underlying buffer, you can then call read to get the + * de-serialized data structure. + * + * The string buffer is inherited from the memory buffer + * Thus, buffers are allocated using C constructs malloc,realloc, and the size + * doubles as necessary. + * + * @author Yun-Fang Juan + */ + +class TStringBuffer : public TMemoryBuffer { + public: + TStringBuffer() : TMemoryBuffer() {}; + + TStringBuffer(const std::string& inputString) : TMemoryBuffer() { + resetString(inputString); + }; + + //destructor. basically the same as TMemoryBuffer + ~TStringBuffer() { + if (owner_) { + if (buffer_ != NULL) { + free(buffer_); + buffer_ = NULL; + } + } + } + + //from buffer to string + std::string getString() { + std::stringstream ss; + ss.write((char*)buffer_, bufferSize_); + return ss.str(); + }; + + //from string to buffer + void resetString(const std::string& inputString) { + char* data; + std::stringstream ss; + bufferSize_ = inputString.size(); + + ss.str(inputString); + data = (char*)malloc(bufferSize_); + ss.read(data, bufferSize_); + + resetBuffer((uint8_t*)data, bufferSize_); + //set the owner_ to true so the buffer_ will be freed later on + owner_ = true; + }; +}; + /** * TPipedTransport. This transport allows piping of a request from one * transport to another either when readEnd() or writeEnd(). The typicAL