From 691ed8de08ae779531df638cfed874c5ea37ba99 Mon Sep 17 00:00:00 2001 From: csoler Date: Mon, 10 Apr 2017 20:34:02 +0200 Subject: [PATCH] added new template for std::string with type id --- libretroshare/src/chat/rschatitems.cc | 50 +++++++------------ .../src/serialization/rstypeserializer.h | 33 +++++++++++- 2 files changed, 50 insertions(+), 33 deletions(-) diff --git a/libretroshare/src/chat/rschatitems.cc b/libretroshare/src/chat/rschatitems.cc index f7edf5601..a430b1119 100644 --- a/libretroshare/src/chat/rschatitems.cc +++ b/libretroshare/src/chat/rschatitems.cc @@ -64,11 +64,9 @@ RsItem *RsChatSerialiser::create_item(uint16_t service_id,uint8_t item_sub_id) void RsChatMsgItem::serial_process(RsItem::SerializeJob j,SerializeContext& ctx) { - RsTypeSerializer::TlvString_proxy tt(message,TLV_TYPE_STR_MSG) ; - RsTypeSerializer::serial_process(j,ctx,chatFlags,"chatflags") ; RsTypeSerializer::serial_process(j,ctx,sendTime,"sendTime") ; - RsTypeSerializer::serial_process(j,ctx,tt,"message") ; + RsTypeSerializer::serial_process(j,ctx,TLV_TYPE_STR_MSG,message,"message") ; } /*************************************************************************/ @@ -84,11 +82,9 @@ RsChatAvatarItem::~RsChatAvatarItem() void RsChatLobbyBouncingObject::serial_process(RsItem::SerializeJob j, SerializeContext& ctx, bool include_signature) { - RsTypeSerializer::TlvString_proxy tt(nick,TLV_TYPE_STR_NAME) ; - RsTypeSerializer::serial_process(j,ctx,lobby_id,"lobby_id") ; RsTypeSerializer::serial_process(j,ctx,msg_id ,"msg_id") ; - RsTypeSerializer::serial_process(j,ctx,tt ,"nick") ; + RsTypeSerializer::serial_process(j,ctx,TLV_TYPE_STR_NAME,nick,"nick") ; if(include_signature) RsTypeSerializer::serial_process(j,ctx,signature,"signature") ; @@ -116,13 +112,10 @@ template<> void RsTypeSerializer::serial_process(RsItem::SerializeJob j,Serializ { RsTypeSerializer::serial_process(j,ctx,info.id,"info.id") ; - TlvString_proxy tt1(info.name ,TLV_TYPE_STR_NAME) ; - TlvString_proxy tt2(info.topic,TLV_TYPE_STR_NAME) ; - - RsTypeSerializer::serial_process(j,ctx,tt1,"info.name") ; - RsTypeSerializer::serial_process(j,ctx,tt2,"info.topic") ; - RsTypeSerializer::serial_process(j,ctx,info.count,"info.count") ; - RsTypeSerializer::serial_process(j,ctx,info.flags,"info.flags") ; + RsTypeSerializer::serial_process (j,ctx,TLV_TYPE_STR_NAME,info.name, "info.name") ; + RsTypeSerializer::serial_process (j,ctx,TLV_TYPE_STR_NAME,info.topic,"info.topic") ; + RsTypeSerializer::serial_process(j,ctx, info.count,"info.count") ; + RsTypeSerializer::serial_process (j,ctx, info.flags,"info.flags") ; } void RsChatLobbyListItem::serial_process(RsItem::SerializeJob j,SerializeContext& ctx) @@ -132,20 +125,16 @@ void RsChatLobbyListItem::serial_process(RsItem::SerializeJob j,SerializeContext void RsChatLobbyEventItem::serial_process(RsItem::SerializeJob j,SerializeContext& ctx) { - RsTypeSerializer::TlvString_proxy tt(string1,TLV_TYPE_STR_NAME) ; - RsTypeSerializer::serial_process(j,ctx,event_type,"event_type") ; - RsTypeSerializer::serial_process (j,ctx,tt ,"string1") ; + RsTypeSerializer::serial_process (j,ctx,TLV_TYPE_STR_NAME,string1,"string1") ; RsTypeSerializer::serial_process(j,ctx,sendTime ,"sendTime") ; RsChatLobbyBouncingObject::serial_process(j,ctx,true) ; } void RsChatLobbyEventItem::serial_process_for_signature(RsItem::SerializeJob j,SerializeContext& ctx) { - RsTypeSerializer::TlvString_proxy tt(string1,TLV_TYPE_STR_NAME) ; - RsTypeSerializer::serial_process(j,ctx,event_type,"event_type") ; - RsTypeSerializer::serial_process (j,ctx,tt ,"string1") ; + RsTypeSerializer::serial_process (j,ctx,TLV_TYPE_STR_NAME,string1,"string1") ; RsTypeSerializer::serial_process(j,ctx,sendTime ,"sendTime") ; RsChatLobbyBouncingObject::serial_process(j,ctx,false) ; @@ -162,30 +151,27 @@ void RsChatLobbyConnectChallengeItem::serial_process(RsItem::SerializeJob j,Seri void RsChatLobbyInviteItem::serial_process(RsItem::SerializeJob j,SerializeContext& ctx) { - RsTypeSerializer::serial_process(j,ctx,lobby_id,"lobby_id") ; - RsTypeSerializer::TlvString_proxy s(lobby_name,TLV_TYPE_STR_NAME) ; - RsTypeSerializer::serial_process(j,ctx,s,"lobby_name") ; - RsTypeSerializer::serial_process(j,ctx,lobby_flags,"lobby_flags") ; + RsTypeSerializer::serial_process(j,ctx, lobby_id, "lobby_id") ; + RsTypeSerializer::serial_process (j,ctx,TLV_TYPE_STR_NAME,lobby_name, "lobby_name") ; + RsTypeSerializer::serial_process (j,ctx, lobby_flags,"lobby_flags") ; } void RsPrivateChatMsgConfigItem::serial_process(RsItem::SerializeJob j,SerializeContext& ctx) { uint32_t x=0 ; - RsTypeSerializer::TlvString_proxy s(message,TLV_TYPE_STR_MSG) ; - RsTypeSerializer::serial_process(j,ctx,x,"place holder value") ; - RsTypeSerializer::serial_process (j,ctx,configPeerId,"configPeerId") ; - RsTypeSerializer::serial_process(j,ctx,chatFlags,"chatFlags") ; - RsTypeSerializer::serial_process(j,ctx,sendTime,"sendTime") ; - RsTypeSerializer::serial_process (j,ctx,s,"message") ; - RsTypeSerializer::serial_process(j,ctx,recvTime,"recvTime") ; + RsTypeSerializer::serial_process(j,ctx, x, "place holder value") ; + RsTypeSerializer::serial_process (j,ctx, configPeerId,"configPeerId") ; + RsTypeSerializer::serial_process(j,ctx, chatFlags, "chatFlags") ; + RsTypeSerializer::serial_process(j,ctx, sendTime, "sendTime") ; + RsTypeSerializer::serial_process (j,ctx,TLV_TYPE_STR_MSG,message, "message") ; + RsTypeSerializer::serial_process(j,ctx, recvTime, "recvTime") ; } void RsChatStatusItem::serial_process(RsItem::SerializeJob j,SerializeContext& ctx) { RsTypeSerializer::serial_process(j,ctx,flags,"flags") ; - RsTypeSerializer::TlvString_proxy tt(status_string,TLV_TYPE_STR_MSG) ; - RsTypeSerializer::serial_process(j,ctx,tt,"status_string") ; + RsTypeSerializer::serial_process(j,ctx,TLV_TYPE_STR_MSG,status_string,"status_string") ; } void RsChatAvatarItem::serial_process(RsItem::SerializeJob j,SerializeContext& ctx) diff --git a/libretroshare/src/serialization/rstypeserializer.h b/libretroshare/src/serialization/rstypeserializer.h index f82365f82..878ec3e6b 100644 --- a/libretroshare/src/serialization/rstypeserializer.h +++ b/libretroshare/src/serialization/rstypeserializer.h @@ -25,7 +25,6 @@ class RsTypeSerializer public: // This type should be used to pass a parameter to drive the serialisation if needed. - typedef std::pair TlvString_proxy; typedef std::pair TlvMemBlock_proxy; //=================================================================================================// @@ -55,6 +54,33 @@ class RsTypeSerializer } } + //=================================================================================================// + // Generic types + type_id // + //=================================================================================================// + + template + static void serial_process(RsItem::SerializeJob j,SerializeContext& ctx,uint16_t type_id,T& member,const std::string& member_name) + { + switch(j) + { + case RsItem::SIZE_ESTIMATE: ctx.mOffset += serial_size(type_id,member) ; + break ; + + case RsItem::DESERIALIZE: ctx.mOk = ctx.mOk && deserialize(ctx.mData,ctx.mSize,ctx.mOffset,type_id,member) ; + break ; + + case RsItem::SERIALIZE: ctx.mOk = ctx.mOk && serialize(ctx.mData,ctx.mSize,ctx.mOffset,type_id,member) ; + break ; + + case RsItem::PRINT: + print_data(member_name,type_id,member); + break; + default: + ctx.mOk = false ; + throw std::runtime_error("Unknown serial job") ; + } + } + //=================================================================================================// // std::vector // //=================================================================================================// @@ -148,6 +174,11 @@ class RsTypeSerializer template static uint32_t serial_size(const T& /* member */); template static void print_data(const std::string& name,const T& /* member */); + template static bool serialize (uint8_t data[], uint32_t size, uint32_t &offset, uint16_t type_id,const T& member); + template static bool deserialize(const uint8_t data[], uint32_t size, uint32_t &offset,uint16_t type_id, T& member); + template static uint32_t serial_size(uint16_t type_id,const T& /* member */); + template static void print_data(const std::string& name,uint16_t type_id,const T& /* member */); + template static bool serialize (uint8_t data[], uint32_t size, uint32_t &offset, const t_RsGenericIdType& member); template static bool deserialize(const uint8_t data[], uint32_t size, uint32_t &offset, t_RsGenericIdType& member); template static uint32_t serial_size(const t_RsGenericIdType& /* member */);