[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<TStringBuffer> strBuffer(new TStringBuffer());
  shared_ptr<TBinaryProtocol> 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<TStringBuffer> strBuffer2(new TStringBuffer());
  shared_ptr<TBinaryProtocol> 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
This commit is contained in:
yunfang 2007-08-19 22:18:38 +00:00
parent 94112d6994
commit 1ec47d4395

View File

@ -8,6 +8,7 @@
#define _THRIFT_TRANSPORT_TTRANSPORTUTILS_H_ 1
#include <string>
#include <sstream>
#include <transport/TTransport.h>
#include <transport/TFileTransport.h>
@ -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 <yunfang@facebook.com>
*/
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