mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-06-23 22:04:26 -04:00
added new serialization code test example
This commit is contained in:
parent
44f297156e
commit
4f24b95b16
2 changed files with 377 additions and 0 deletions
78
libretroshare/src/serialization/serial_test.cc
Normal file
78
libretroshare/src/serialization/serial_test.cc
Normal file
|
@ -0,0 +1,78 @@
|
|||
// COMPILE_LINE: g++ -g serial_test.cc -I.. -o serial_test ../lib/libretroshare.a -lssl -lcrypto -lstdc++ -lpthread
|
||||
//
|
||||
//
|
||||
|
||||
#include "serializer.h"
|
||||
#include "util/rsmemory.h"
|
||||
#include "util/rsprint.h"
|
||||
#include "serialiser/rsserial.h"
|
||||
|
||||
static const uint16_t RS_SERVICE_TYPE_TEST = 0xffff;
|
||||
static const uint8_t RS_ITEM_SUBTYPE_TEST1 = 0x01 ;
|
||||
|
||||
class RsTestItem: public RsSerializable
|
||||
{
|
||||
public:
|
||||
RsTestItem() : RsSerializable(RS_PKT_VERSION_SERVICE,RS_SERVICE_TYPE_TEST,RS_ITEM_SUBTYPE_TEST1) {}
|
||||
|
||||
virtual void serial_process(RsSerializable::SerializeJob j, SerializeContext& ctx)
|
||||
{
|
||||
RsTypeSerializer<uint64_t> ().serial_process(j,ctx,ts) ;
|
||||
RsTypeSerializer<std::string>().serial_process(j,ctx,str) ;
|
||||
}
|
||||
|
||||
// derived from RsItem
|
||||
//
|
||||
virtual void clear() {}
|
||||
virtual std::ostream& print(std::ostream&,uint16_t indent) {}
|
||||
|
||||
|
||||
private:
|
||||
std::string str ;
|
||||
uint64_t ts ;
|
||||
};
|
||||
|
||||
class RsTestSerializer: public RsSerializer
|
||||
{
|
||||
public:
|
||||
|
||||
virtual RsSerializable *create_item(uint16_t service,uint8_t subtype)
|
||||
{
|
||||
switch(subtype)
|
||||
{
|
||||
case RS_ITEM_SUBTYPE_TEST1: return new RsTestItem();
|
||||
|
||||
default:
|
||||
return NULL ;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
int main(int argc,char *argv[])
|
||||
{
|
||||
try
|
||||
{
|
||||
RsTestItem t1 ;
|
||||
|
||||
uint32_t size = RsTestSerializer().size_item(&t1);
|
||||
|
||||
std::cerr << "t1.serial_size() = " << size << std::endl;
|
||||
|
||||
RsTemporaryMemory mem1(size);
|
||||
|
||||
RsTestSerializer().serialize_item(&t1,mem1,mem1.size()) ;
|
||||
|
||||
RsSerializable *t2 = RsTestSerializer().deserialize_item(mem1,mem1.size()) ;
|
||||
|
||||
std::cerr << "Serialized t1: " << RsUtil::BinToHex(mem1,mem1.size()) << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
catch(std::exception& e)
|
||||
{
|
||||
std::cerr << "Exception caught: " << e.what() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue