2 thrift -cpp ThriftTest.thrift
3 g++ -Wall -g -I../lib/cpp/src -I/usr/local/include/boost-1_33_1 \
4 TMemoryBufferTest.cpp gen-cpp/ThriftTest_types.cpp \
5 ../lib/cpp/.libs/libthrift.a -o TMemoryBufferTest
12 #include <transport/TTransportUtils.h>
13 #include <protocol/TBinaryProtocol.h>
14 #include "gen-cpp/ThriftTest_types.h"
17 int main(int argc
, char** argv
) {
19 using facebook::thrift::transport::TMemoryBuffer
;
20 using facebook::thrift::protocol::TBinaryProtocol
;
21 using boost::shared_ptr
;
23 shared_ptr
<TMemoryBuffer
> strBuffer(new TMemoryBuffer());
24 shared_ptr
<TBinaryProtocol
> binaryProtcol(new TBinaryProtocol(strBuffer
));
26 thrift::test::Xtruct a
;
29 a
.string_thing
="holla back a";
31 a
.write(binaryProtcol
.get());
32 std::string serialized
= strBuffer
->getBufferAsString();
34 shared_ptr
<TMemoryBuffer
> strBuffer2(new TMemoryBuffer());
35 shared_ptr
<TBinaryProtocol
> binaryProtcol2(new TBinaryProtocol(strBuffer2
));
37 strBuffer2
->resetFromString(serialized
);
38 thrift::test::Xtruct a2
;
39 a2
.read(binaryProtcol2
.get());
45 using facebook::thrift::transport::TMemoryBuffer
;
50 string
* str1
= new string("abcd1234");
51 const char* data1
= str1
->data();
52 TMemoryBuffer
buf(*str1
, true);
54 string
* str2
= new string("plsreuse");
55 bool obj_reuse
= (str1
== str2
);
56 bool dat_reuse
= (data1
== str2
->data());
57 cout
<< "Object reuse: " << obj_reuse
<< " Data reuse: " << dat_reuse
58 << ((obj_reuse
&& dat_reuse
) ? " YAY!" : "") << endl
;
61 string str3
= "wxyz", str4
= "6789";
62 buf
.readAppendToString(str3
, 4);
63 buf
.readAppendToString(str4
, INT_MAX
);
65 assert(str3
== "wxyzabcd");
66 assert(str4
== "67891234");