mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-06-29 08:37:36 -04:00
removed RsSerializable and merged it into RsItem
This commit is contained in:
parent
1bc4fe5f28
commit
b52071d0c9
4 changed files with 49 additions and 27 deletions
|
@ -5,7 +5,9 @@
|
|||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include "rsserializable.h"
|
||||
#include "serialiser/rsserial.h"
|
||||
|
||||
#define SERIALIZE_ERROR() std::cerr << __PRETTY_FUNCTION__ << " : "
|
||||
|
||||
class SerializeContext
|
||||
{
|
||||
|
@ -20,22 +22,24 @@ class SerializeContext
|
|||
bool mOk ;
|
||||
};
|
||||
|
||||
class RsSerializer
|
||||
class RsSerializer: public RsSerialType
|
||||
{
|
||||
public:
|
||||
RsSerializer(uint16_t service_id) : RsSerialType(service_id) {}
|
||||
|
||||
/*! create_item
|
||||
* should be overloaded to create the correct type of item depending on the data
|
||||
*/
|
||||
virtual RsSerializable *create_item(uint16_t service, uint8_t item_sub_id)
|
||||
virtual RsItem *create_item(uint16_t service, uint8_t item_sub_id)
|
||||
{
|
||||
return NULL ;
|
||||
}
|
||||
|
||||
RsSerializable *deserialize_item(const uint8_t *data,uint32_t size)
|
||||
RsItem *deserialise(const uint8_t *data,uint32_t size)
|
||||
{
|
||||
uint32_t rstype = getRsItemId(const_cast<void*>((const void*)data)) ;
|
||||
|
||||
RsSerializable *item = create_item(getRsItemService(rstype),getRsItemSubType(rstype)) ;
|
||||
RsItem *item = create_item(getRsItemService(rstype),getRsItemSubType(rstype)) ;
|
||||
|
||||
if(!item)
|
||||
{
|
||||
|
@ -46,7 +50,7 @@ class RsSerializer
|
|||
SerializeContext ctx(const_cast<uint8_t*>(data),size);
|
||||
ctx.mOffset = 8 ;
|
||||
|
||||
item->serial_process(RsSerializable::DESERIALIZE, ctx) ;
|
||||
item->serial_process(RsItem::DESERIALIZE, ctx) ;
|
||||
|
||||
if(ctx.mOk)
|
||||
return item ;
|
||||
|
@ -55,16 +59,16 @@ class RsSerializer
|
|||
return NULL ;
|
||||
}
|
||||
|
||||
bool serialize_item(const RsSerializable *item,uint8_t *const data,uint32_t size)
|
||||
bool serialise(RsItem *item,uint8_t *const data,uint32_t size)
|
||||
{
|
||||
SerializeContext ctx(data,0);
|
||||
|
||||
uint32_t tlvsize = size_item(item) ;
|
||||
uint32_t tlvsize = this->size(item) ;
|
||||
|
||||
if(tlvsize > size)
|
||||
throw std::runtime_error("Cannot serialise: not enough room.") ;
|
||||
|
||||
if(!setRsItemHeader(data, tlvsize, const_cast<RsSerializable*>(item)->PacketId(), tlvsize))
|
||||
if(!setRsItemHeader(data, tlvsize, item->PacketId(), tlvsize))
|
||||
{
|
||||
std::cerr << "RsSerializer::serialise_item(): ERROR. Not enough size!" << std::endl;
|
||||
return false ;
|
||||
|
@ -72,7 +76,7 @@ class RsSerializer
|
|||
ctx.mOffset = 8;
|
||||
ctx.mSize = tlvsize;
|
||||
|
||||
const_cast<RsSerializable*>(item)->serial_process(RsSerializable::SERIALIZE,ctx) ;
|
||||
item->serial_process(RsItem::SERIALIZE,ctx) ;
|
||||
|
||||
if(ctx.mSize != ctx.mOffset)
|
||||
{
|
||||
|
@ -82,12 +86,12 @@ class RsSerializer
|
|||
return true ;
|
||||
}
|
||||
|
||||
uint32_t size_item(const RsSerializable *item)
|
||||
uint32_t size(RsItem *item)
|
||||
{
|
||||
SerializeContext ctx(NULL,0);
|
||||
|
||||
ctx.mSize = 8 ; // header size
|
||||
const_cast<RsSerializable*>(item)->serial_process(RsSerializable::SIZE_ESTIMATE, ctx) ;
|
||||
item->serial_process(RsItem::SIZE_ESTIMATE, ctx) ;
|
||||
|
||||
return ctx.mSize ;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue